Archived

This topic is now archived and is closed to further replies.

Pointer returning problem

This topic is 5377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Usualy returning pointer isn''t big deal but there is shometing funky what messups returning. If u could check the code below please.
  
LPCTSTR GetFilename() 
{
 OPENFILENAME browsedfile;
// code 

// ...

	if(!GetOpenFileName(&browsedfile)) return 0;
         // function returns succes and all the variables are correct but browsedfile.lpstrFileTitle is odd... even it holds the right string


        // pointer seems to be ok

	cout<<browsedfile.lpstrFileTitle<<endl;
        // string was printed succesfully

 
 // if this code wasn''t comment returned pointer would be good so it looks like GetOpenFilename() does shometing wierd to this variable?

 // browsedfile.lpstrFileTitle = "A test string";


 // returns invalid pointer

 return browsedfile.lpstrFileTitle;
}
  
So does anyone have any idea what might cause this? Did u see the problem?

Share this post


Link to post
Share on other sites
You''re returning a pointer, but the data it points to is being deallocated when browsedfile goes out of scope at the end of the function.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try :

LPCTSTR GetFilename(LPCTSTR lpstrFileTitle) {
OPENFILENAME browsedfile;// code // ...
if(!GetOpenFileName(&browsedfile))
return 0;
cout<<browsedfile.lpstrFileTitle<<endl;
lpstrFileTitle = browsedfile.lpstrFileTitle;
}

Thus pass in a pointer which will receive the file title on exit of the function. It is like Dobbs said, the browsedfile goes out of scope and end of method.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Try :

LPCTSTR GetFilename(LPCTSTR lpstrFileTitle) {
OPENFILENAME browsedfile;// code // ...
if(!GetOpenFileName(&browsedfile))
return 0;
cout<lpstrFileTitle = browsedfile.lpstrFileTitle;
}

Thus pass in a pointer which will receive the file title on exit of the function. It is like Dobbs said, the browsedfile goes out of scope and end of method.




Ok thanks Il do that. Just one thing is unclear. If the struct goes out of scope why doin this wont:
browsedfile.lpstrFileTitle = "A test string";
Now browsedfile.lpstrFileTitle wont go out of scope why?

Share this post


Link to post
Share on other sites
ok when you create an object (class/struct instance, or basic type) in a block like

... code
{
int blah;

}
label:

It exists until execution goes out of that blocks scope, i.e. blah will exist until execution passes out of that block (before execution reaches label). This object is created on the stack (in most implementations anyhow, I''m not sure what the standard actually requires). Also in the case:

... code
{
int blah;
{
label2:
std::cout << blah << std::endl;
}

}

... code

blah exists in the scope of label2, i.e. in the above code it is perfectly legal to use blah in the nested block.

In a function, the same thing occurs, locally declared objects (unless they are static) are created at their point of declaration and are destroyed at the end of their largest enclosing block.


void function ()
{
int blah; // blah is created here
callsomefunction();
blah++;
// blah is destroyed here (well it can be thought to)
}

in other words lpstrFileTitle is a part of browsedfile, you are actually createing this when you create browsedfile, and similarly lpstrFileTitle is destroyed when browsedfile is destroyed,
note: that the pointer is destroyed, not what it points to

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Try :

LPCTSTR GetFilename(LPCTSTR lpstrFileTitle) {
OPENFILENAME browsedfile;// code // ...
if(!GetOpenFileName(&browsedfile))
return 0;
cout<lpstrFileTitle = browsedfile.lpstrFileTitle;
}

Thus pass in a pointer which will receive the file title on exit of the function. It is like Dobbs said, the browsedfile goes out of scope and end of method.



Could''t get that code working. probably becose even I would pass a new pointer to the filetitle it doesn''t exists anymore in the memory, umm right?

Share this post


Link to post
Share on other sites
quote:
Original post by _Stinger
ok when you create an object (class/struct instance, or basic type) in a block like

... code
{
int blah;

}
label:

It exists until execution goes out of that blocks scope, i.e. blah will exist until execution passes out of that block (before execution reaches label). This object is created on the stack (in most implementations anyhow, I''m not sure what the standard actually requires). Also in the case:

... code
{
int blah;
{
label2:
std::cout << blah << std::endl;
}

}

... code

blah exists in the scope of label2, i.e. in the above code it is perfectly legal to use blah in the nested block.

In a function, the same thing occurs, locally declared objects (unless they are static) are created at their point of declaration and are destroyed at the end of their largest enclosing block.


void function ()
{
int blah; // blah is created here
callsomefunction();
blah++;
// blah is destroyed here (well it can be thought to)
}

in other words lpstrFileTitle is a part of browsedfile, you are actually createing this when you create browsedfile, and similarly lpstrFileTitle is destroyed when browsedfile is destroyed,
note: that the pointer is destroyed, not what it points to


Thanks Stinger but I actualy know that thing what u just told. There is just shometing what confuses me on this one.
Is it really so that end of func only pointer is destroyed not the memory where it points? Acording to my knowledge that would mean a memory leak becose u cant use that memory anymore if u dont have pointer to it.

Share this post


Link to post
Share on other sites
You''re returning the pointer just fine, the problem is the data it points to is being deallocated by browsedfile''s destructor as it goes out of scope at the end of the function. One way to get around this is to allocate a new string on the heap, copy the file title into that new string, and return a pointer to it.

Share this post


Link to post
Share on other sites
quote:
Original post by Dobbs
You''re returning the pointer just fine, the problem is the data it points to is being deallocated by browsedfile''s destructor as it goes out of scope at the end of the function. One way to get around this is to allocate a new string on the heap, copy the file title into that new string, and return a pointer to it.


Ok thanks, I''l try that.

Share this post


Link to post
Share on other sites
Just don''t forget to deallocate that string somewhere. Probably in the function that''s calling GetFilename.

Another approach could be to use std::string objects and pass by reference or value.

Share this post


Link to post
Share on other sites
How about making the OPENFILENAME struct static?
You won''t have to deallocate anything afterwards, but you''d better make sure you''re done with the string before calling GetFilename() again (it will be overwritten).

Share this post


Link to post
Share on other sites