Linked Questions
16 questions linked to/from Is it safe to use the std::string::c_str() of a temporary string?
37 votes
2 answers
4k views
Lifetime of temporaries [duplicate]
The following code works fine, but why is this correct code? Why is the "c_str()" pointer of the temporary returned by foo() valid? I thought, that this temporary is already destroyed when bar() is ...
7 votes
3 answers
24k views
is there issue will stringstream.str().c_str()? [duplicate]
For Code: stringstream ss("012345678901234567890123456789012345678901234567890123456789"); some articles said it is wrong for followed usage due to ss.str return temp object and will destructered ...
5 votes
2 answers
353 views
Does this code causes UB? [duplicate]
I have checkd gcc and clang and both does not generate any warnings. I suppose that lifetime of temporary from foo() will be prolonged untill the end of full expression which is where semicolon in bar ...
5 votes
1 answer
149 views
Temporary in a function call: UB? [duplicate]
Consider the following code, based on this answer: #include <iostream> #include <sstream> class StringBuilder { public: template <typename T> inline StringBuilder &operator&...
0 votes
0 answers
60 views
Two different char pointers end up with the same value [duplicate]
I'm assuming this is undefined behavior, but I'm not exactly sure why? When I compile the above code with g++ 4.4.7 and execute it: #include <iostream> #include <string> using namespace ...
224 votes
9 answers
578k views
How to use printf with std::string
My understanding is that string is a member of the std namespace, so why does the following occur? #include <iostream> int main() { using namespace std; string myString = "Press ENTER ...
-2 votes
3 answers
3k views
Weird output when converting from string to const char* in c++ [duplicate]
Consider the MWE below, std::string return_string() { return "this is a string" } int main() { const char *y = return_string().c_str(); std::string str = return_string(); const char *...
0 votes
2 answers
172 views
Getting a (const char*) representation of an integer in a one-liner
I'm working on a software using a home-made report library which takes only (const char *) type as an argument : ReportInfo(const char* information); ReportError(const char* error); [...] As I'm ...
2 votes
1 answer
759 views
Boost.Program_Options: Why does options_description_easy_init::operator() not have an overload for std::string?
Consider this MCVE: #include <boost/program_options.hpp> #include <iostream> #include <map> namespace po = boost::program_options; using namespace std; po::options_description ...
2 votes
1 answer
632 views
Arrays allocated at same address Cython + Numpy
I came across some funny memory behavior working with numpy + cython, while trying to get data from a numpy array as a C array, to use in a GIL-free function. I've taken look at both the cython and ...
0 votes
1 answer
331 views
MFC - Display message
I'm trying to display a simple message within my first MFC application. Strangely, the first sample doesn't work, instead the second one works correctly. auto text = std::to_wstring(1).c_str(); ...
1 vote
2 answers
376 views
String argument value disappearing in call to class constructor, wiped by constructor of a member?
I was getting intermittently weird results with running stat() (perror(): "Bad address") on a filename that comes from a commandline argument in my project: // mz.h class MzImage { private: ...
-1 votes
2 answers
135 views
C++ function doesn't return anything more than 15 characters [duplicate]
I have a C++ function that is supposed to repeat a string a certain amount of times. What I've seen is that when the resulting string is more than 15 characters long, the function does not return ...
0 votes
2 answers
143 views
Trying to print an escape character x number of times
So I want to do something like printf("%s", '\t'*3); Im just wondering if there is a way to print something like that without looping
0 votes
1 answer
106 views
C++ string conversion inside vs outside printf
I'm extracting text from a PDF using Poppler and used the following code to print the text: for (std::vector<poppler::text_box>::iterator it = currpg.begin(); it != currpg.end(); ++it) { ...