The free store Vs Memory Leaks
Results 1 to 4 of 4

Thread: The free store Vs Memory Leaks

  1. #1

    The free store Vs Memory Leaks

    Acording to my book there are 5 areas of memory available to a C++ program:

    Global name space, The free store, Registers, Code space, The stack.

    Global namespace is used for global variables, The stack is used for local variables in functions, Code space is to store the program code, and Registers of cource do the obvious.

    So this leaves the free store. This is what confuses me:

    Acording to my book:

    The problem with local variables is that they don't persist: when a function returns, the local variables are thown away
    Thats understandable and the way it should be..

    The advantage to the free store is that the memory you reserve remains available untill you explicitly free it...If you reserve memory on the free store while in a function, the memory is still available when the function returns
    Great..

    The advantage of accessing memory this way, rather then useing global variables, is that only functions with access to the pointer have access to the data.
    And this is very understandable but then....

    Remember that the pointer itself--as oposed to the memory to which it points--is a local variable. When the function in which it is declared returns, that pointer goes out of scope and is lost
    Acording to them if I forget to free the memory before the function returns I will have a memory leak, which is understandble, since the memory was not freed and the pointer to it was lost, you may no longer access that memory and it takes up space, thus less memory available for other data.

    Here is the part I don't understand:

    I dont understand that if the pointer is lost at the end of a function, then why is it a advantage that the free store remains available after the function returns when we no longer have a pointer to it.

    What I mean is origionaly it was quoted that "the memory is still available when the function returns" which since the pointer itself was a local variable and was lost, and now we dont have access to that data that was in the free store, although it is still there, we no longer have a handle for it and may not access it, thus whatever I stored there is invisible now....

    Can someone explain why they are contradicting themself??
    test

  2. #2
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,535
    <well just awakend and not quite clear>

    Becouse you can pass on the pointer while exiting the function.
    You can still access the memory from the new formed pointer.

    </well>
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  3. #3
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    (OK, I have to dig my old memory back when I was a CS student. Now I only do some casual Perl and shell scripting. So I hope I can recall it correctly...)
    Can someone explain why they are contradicting themself??
    They don't.

    If you declare a local pointer variable to reserve memory, the pointer goes out of scope and is lost when the function returns. If you still need the reference to the reserved memory (and you haven't freed it), you can either
    (1) use a global pointer variable, or
    (2) pass the pointer value (as the function's return value) to other variable

    Remember to free it somewhere when you no longer need it.

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  4. #4
    Thank you, I feel embarased now since it was such a simple answer. I kept reading it over and over and each time I could not figure out why. Thanks for explaining that to me.
    test

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides