Thread: Pointer String Leaks

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    Pointer String Leaks

    I'm curious how code such as

    Code:
    const char * szSomeString = "Hello World";
    will fair against memory leaks? This syntax allocates some memory on the heap and returns a pointer to it correct? However I cannot free() it when I am done with it or it will error. What is the proper way to dispose of elements like this, or are they not a leak?

    Cheers

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    IIRC, that is allocated from the stack. You need to free things allocated with malloc() and any of its derivatives.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't free it, as CC mentioned. It will be returned to free memory, when the program stops.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Hmmm
    The pointer is allocated on the stack for sure, but the characters too? I had read those are written to static memory(the heap?) and then the pointer is returned. If the characters were copied onto the stack then how would this work?
    Code:
    char * Test()
    {
    	char * test = "Hi Friend";
    	return test;
    }
    
    ...
    
    printf ("%s\n", Test());

  5. #5
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by valaris View Post
    Hmmm
    The pointer is allocated on the stack for sure, but the characters too? I had read those are written to static memory(the heap?) and then the pointer is returned. If the characters were copied onto the stack then how would this work?
    Code:
    char * Test()
    {
    	char * test = "Hi Friend";
    	return test;
    }
    
    ...
    
    printf ("%s\n", Test());
    you'll probably get an undefined behavior if you do that.
    I suggest you do this
    Code:
    char *Test ()
    {
        char *test = "Hi Friend";
    
        return(strcpy(calloc(strlen(test) + 1, sizeof(char)), test));
    }

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Hmm well on my compiler it works...msvc...and i'm n ot sure why if the above descriptions are indeed correct.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    String literals are neither dynamically allocated, nor created on the stack. They are typically just part of the applications data segment that has been loaded into RAM (hence the reason they are really const).

    For all intents and purposes though you can ignore where they exist. They just exist somewhere for the lifetime of the program. Your code doesn't play any part in the string literal coming into or going out of existence.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by iMalc View Post
    String literals are neither dynamically allocated, nor created on the stack. They are typically just part of the applications data segment that has been loaded into RAM (hence the reason they are really const).

    For all intents and purposes though you can ignore where they exist. They just exist somewhere for the lifetime of the program. Your code doesn't play any part in the string literal coming into or going out of existence.
    Awesome! And thankyou. That makes much more sense...And would explain why I can return pointers to them that I create in a function and don't need to worry about their memory management.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Recently we had some similar discussion, and yes, string literals (anything "<stuff here>") are guaranteed to exist for the duration of the execution [1]. So there is no need to copy strings like cph suggests, for example.


    [1] Ok, not entirely true. In various modern operating systems, such as Linux or Windows, sometimes have "initializing" sections of code. These are removed at the beginning of execution. Obviously, strings stored in such initializing sections will be removed without the compilers knowledge - and as such, are undefined behaviour if they are used after the removal [most likely causing a crash, as we are talking about kernel memory that is removed from the address map, so it would be "not mapped into memory"]. The purpose of this is to allow the OS to remove code & data that is only used during startup. Since many kernel drivers and may have fairly large amounts of "initialization code", it is worth going through this extra step of removing that when the driver has been initialized.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    http://msdn.microsoft.com/en-us/magazine/cc301805.aspx

    So string literal data is usually placed in the .rdata section? (Read - Only Data). And basically global to the entire process?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by valaris View Post
    http://msdn.microsoft.com/en-us/magazine/cc301805.aspx

    So string literal data is usually placed in the .rdata section? (Read - Only Data). And basically global to the entire process?
    Correct. And because the section is read-only, any attempt o modify the data will fail.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 03:03 PM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM