Click here to Skip to main content
15,888,454 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: NMAKE can't find include file in subfolder Pin
Alan Balkany24-Feb-12 5:37
Alan Balkany24-Feb-12 5:37 
Questions it safe to pass CComVariant declared locally ot other function Pin
ptr_Electron22-Feb-12 3:55
ptr_Electron22-Feb-12 3:55 
AnswerRe: s it safe to pass CComVariant declared locally ot other function Pin
Jochen Arndt22-Feb-12 4:39
professionalJochen Arndt22-Feb-12 4:39 
QuestionHow to check CComVariant.boolval is valid and extract bool value from it Pin
ptr_Electron22-Feb-12 3:51
ptr_Electron22-Feb-12 3:51 
AnswerRe: How to check CComVariant.boolval is valid and extract bool value from it Pin
bjorn_ht22-Feb-12 4:44
bjorn_ht22-Feb-12 4:44 
AnswerRe: How to check CComVariant.boolval is valid and extract bool value from it Pin
Jochen Arndt22-Feb-12 4:48
professionalJochen Arndt22-Feb-12 4:48 
Questionstandard c_str() problem in C++ Pin
Anitesh Kumar21-Feb-12 17:45
Anitesh Kumar21-Feb-12 17:45 
AnswerRe: standard c_str() problem in C++ PinPopular
Emilio Garavaglia21-Feb-12 20:10
Emilio Garavaglia21-Feb-12 20:10 
The result of an expression is temporary, and it is destroyed an the and of the expression evaluation.

In your first case, you are getting a temporary string as a result of s1+s2, whose buffer pointer is saved in s. After that, the expression finish, the temporary is destroyed, and so it is its buffer and s is left dangling. You had been lucky in having printed nothing. accessing a deleted buffer can even result in a crash.

In your second example, the temporary string resulting from s1+s2 is kept alive until the expression it belongs ( cout<<(s1+s2).c_str()<<endl; ) is evaluated. Hence its buffer (renturned from c_str()) is still there at the time its pointer is given to cout.

Your first sample works correctly if you retain the string:
C#
void SetStr(string& s1, string& s2)
{
  string ss; //the place to store  the result
  ss = s1+s2; //the result of s1+s2 is moved to ss,  that survives the expression itself
  const char* s = ss.c_str(); //ss buffer pointer obtained 
  cout << s << endl; //your original cout
}


2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:



modified 22-Feb-12 14:48pm.

GeneralRe: standard c_str() problem in C++ Pin
CPallini22-Feb-12 0:18
mveCPallini22-Feb-12 0:18 
GeneralRe: standard c_str() problem in C++ Pin
Eytukan22-Feb-12 0:52
Eytukan22-Feb-12 0:52 
QuestionBest way to check if a thread is still active c++ win32 Pin
jkirkerx21-Feb-12 11:35
professionaljkirkerx21-Feb-12 11:35 
QuestionRe: Best way to check if a thread is still active c++ win32 Pin
Randor 21-Feb-12 14:27
professional Randor 21-Feb-12 14:27 
AnswerRe: Best way to check if a thread is still active c++ win32 Pin
jkirkerx21-Feb-12 15:18
professionaljkirkerx21-Feb-12 15:18 
GeneralRe: Best way to check if a thread is still active c++ win32 Pin
Randor 21-Feb-12 16:55
professional Randor 21-Feb-12 16:55 
GeneralRe: Best way to check if a thread is still active c++ win32 Pin
jkirkerx21-Feb-12 17:05
professionaljkirkerx21-Feb-12 17:05 
AnswerRe: Best way to check if a thread is still active c++ win32 Pin
Chuck O'Toole21-Feb-12 14:29
Chuck O'Toole21-Feb-12 14:29 
GeneralRe: Best way to check if a thread is still active c++ win32 Pin
jkirkerx21-Feb-12 15:28
professionaljkirkerx21-Feb-12 15:28 
AnswerRe: Best way to check if a thread is still active c++ win32 Pin
Chuck O'Toole21-Feb-12 16:12
Chuck O'Toole21-Feb-12 16:12 
AnswerComplete code for CreateProcess and loop Pin
jkirkerx21-Feb-12 15:26
professionaljkirkerx21-Feb-12 15:26 
AnswerRe: Complete code for CreateProcess and loop Pin
Chuck O'Toole21-Feb-12 16:22
Chuck O'Toole21-Feb-12 16:22 
AnswerRe: Complete code for CreateProcess and loop Pin
Chuck O'Toole21-Feb-12 16:34
Chuck O'Toole21-Feb-12 16:34 
GeneralRe: Complete code for CreateProcess and loop Pin
jkirkerx21-Feb-12 16:50
professionaljkirkerx21-Feb-12 16:50 
GeneralRe: Complete code for CreateProcess and loop Pin
Randor 21-Feb-12 17:08
professional Randor 21-Feb-12 17:08 
GeneralRe: Complete code for CreateProcess and loop Pin
jkirkerx21-Feb-12 18:18
professionaljkirkerx21-Feb-12 18:18 
GeneralRe: Complete code for CreateProcess and loop Pin
jkirkerx22-Feb-12 6:58
professionaljkirkerx22-Feb-12 6:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.