Jump to content
  • Advertisement

Archived

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

NuffSaid

Is this a Memory Leak?

This topic is 6417 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

  

char *GetSomeString(void)
{
char *str = "Hello World"; //initialise str to Hello World

str = "Stuff";//point str to another string


return str;
}

int main(void)
{
printf("%s", GetSomeString());

return 0;

}

  
My question is does that above code produce a memory leak? In the function GetSomeString, the pointer is initialised to point to the string Hello World. Then on the line below, it is made to point to Stuff. What happens to the memory that was allocated to hold Hello World? is there a potential memory leak here? Thanx

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
There should not be any memory leak there. If you do not allocate an object with "new" or "malloc" or something like this, you also not need to explicitly destroy it.

Share this post


Link to post
Share on other sites
It won''t leak but your program will crash. You should not be using raw memory pointers as strings. You should either allocate a buffer for the string or use the STL string class

Share this post


Link to post
Share on other sites
This program will not crash..
when you return str, it will point to the text string (static text)...

Share this post


Link to post
Share on other sites
No reason for the program to crash. However:

  
char *GetSomeString(void)
{
char buffer [80];
strcpy (buffer, "Stuff");
return buffer;
}

main(void)
{
printf("%s", GetSomeString());
return 0;
}


Now that is a different story. =)

Tim

Share this post


Link to post
Share on other sites
The program will not produce a memory leak. The reason is that you are using a static string which is allocated and deallocated by the compiler.

-René Petersen

Share this post


Link to post
Share on other sites
I know it doesn''t crash (Why should it?). I just wanted to know whether there was a memory leak.

Thanks guys, for your replies. That helped a lot.

Share this post


Link to post
Share on other sites
It won''t crash in your sample now but it''s leads to code that will crash if you use the return value in other ways other than a const char *.

Share this post


Link to post
Share on other sites
I''m reading strostrup''s book, and is says that returning string literals is ok because they''re allocated statically when the program is started.

return "abcdefg"; 

won''t crash,
niether would
char *s = "abcdefg";
return s;


but what TimSmith said would because it''s allocated on the function''s stack space, which is deleted when the function returns.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I have no name that you may call me. I am merely Succinct.
~Succinct Demos Online~

"Hey, where''d that display list rotate off to now?"
-(Drop me a line here)-

Share this post


Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!