Click here to Skip to main content
16,009,728 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionDialog based on JPG advise Pin
Codling202015-Mar-12 6:32
Codling202015-Mar-12 6:32 
AnswerRe: Dialog based on JPG advise Pin
Richard MacCutchan15-Mar-12 6:59
mveRichard MacCutchan15-Mar-12 6:59 
AnswerRe: Dialog based on JPG advise Pin
_AnsHUMAN_ 15-Mar-12 18:36
_AnsHUMAN_ 15-Mar-12 18:36 
Questionneed small c++ projects with documentation Pin
johan david15-Mar-12 4:21
johan david15-Mar-12 4:21 
AnswerRe: need small c++ projects with documentation Pin
CPallini15-Mar-12 4:28
mveCPallini15-Mar-12 4:28 
GeneralReturn c_str() from function Pin
elelont215-Mar-12 3:44
elelont215-Mar-12 3:44 
GeneralRe: Return c_str() from function Pin
Erudite_Eric15-Mar-12 3:55
Erudite_Eric15-Mar-12 3:55 
GeneralRe: Return c_str() from function Pin
enhzflep15-Mar-12 3:59
enhzflep15-Mar-12 3:59 
Gday.

Looking at the tute you've linked to, one can see that yes indeed! A pointer is returned from a function. However, we can also see that the memory that is pointed to by this pointer is actually owned by main. - in so far as they are const char* strings.

I'm dubious about this example, since string constants are used. Had "three" & "one two three four" been assigned to variables, I'd be a lot happier about what I thought the compiler may do with them - as it stands, my understanding is that the compiler is free to do with these _temporary_ string constants as it pleases once they've been used. (once the function has been called)


You example, on the other hand - that does exhibit the problem you spoke of. As soon as the Convert function returns, the ss variable goes out of scope. At the same time, it loses ownership of the memory that it put a copy of the string into. While you still have a pointer to this memory, the ss object no longer owns it.

You should be able to demonstrate this quite easily.

1) assign the return value of the function to a variable
2) do a whole bunch of stuff involving memory allocation/deallocation
3) now try to print the string that you got earlier and stored a pointer to.
GeneralRe: Return c_str() from function Pin
CPallini15-Mar-12 4:03
mveCPallini15-Mar-12 4:03 
GeneralRe: Return c_str() from function Pin
elelont215-Mar-12 4:38
elelont215-Mar-12 4:38 
GeneralRe: Return c_str() from function Pin
CPallini15-Mar-12 8:40
mveCPallini15-Mar-12 8:40 
GeneralRe: Return c_str() from function Pin
Erudite_Eric15-Mar-12 22:11
Erudite_Eric15-Mar-12 22:11 
GeneralRe: Return c_str() from function Pin
CPallini15-Mar-12 22:24
mveCPallini15-Mar-12 22:24 
GeneralRe: Return c_str() from function Pin
elelont216-Mar-12 3:09
elelont216-Mar-12 3:09 
GeneralRe: Return c_str() from function Pin
CPallini16-Mar-12 3:12
mveCPallini16-Mar-12 3:12 
GeneralRe: Return c_str() from function Pin
elelont216-Mar-12 4:01
elelont216-Mar-12 4:01 
GeneralRe: Return c_str() from function Pin
CPallini16-Mar-12 4:14
mveCPallini16-Mar-12 4:14 
GeneralRe: Return c_str() from function Pin
elelont216-Mar-12 4:19
elelont216-Mar-12 4:19 
GeneralRe: Return c_str() from function Pin
CPallini16-Mar-12 4:26
mveCPallini16-Mar-12 4:26 
GeneralRe: Return c_str() from function Pin
elelont218-Mar-12 21:27
elelont218-Mar-12 21:27 
GeneralRe: Return c_str() from function Pin
CPallini19-Mar-12 3:04
mveCPallini19-Mar-12 3:04 
GeneralRe: Return c_str() from function Pin
Erudite_Eric16-Mar-12 10:13
Erudite_Eric16-Mar-12 10:13 
GeneralRe: Return c_str() from function Pin
CPallini16-Mar-12 10:37
mveCPallini16-Mar-12 10:37 
QuestionSet subitem image on virtual listview Pin
_Flaviu15-Mar-12 2:35
_Flaviu15-Mar-12 2:35 
AnswerRe: Set subitem image on virtual listview Pin
Richard MacCutchan15-Mar-12 3:23
mveRichard MacCutchan15-Mar-12 3:23 

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.