Thread: How to deallocate space with free.

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > If you are working on complex code and want a confirmation that free was called you can do this...
    Did you test it?
    All you managed was to make your local variable (ptr) NULL, not the value of MyMem in main()
    Good catch!

    My bad.

  2. #17
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Cool

    Quote:
    Code:
    The point is that the data may still exist in
    memory, even after you free it - but that memory is
    (hopefully), listed back in the pool of available memory.
    The operating system may use it in a micro-second, or it may
    not use it for years on end, if it's just idling away.
    It's entirely up to the OS.
    if that were the case, then I don't think C would have needed to implement the free() function.
    Also please note: I'm trying to look at the string while still inside main() i.e. before main exits.
    But even if I tried after main() completes, the string should still be there somewhere.

    Quote:
    Code:
    Could you post what you've tried? Because of course it still exists in memory until you free it. But how did you try to find it back?
    The normal way is to keep a pointer to the allocated memory (in fact, that's the only way except maybe in the case of exploits that sometimes use  ....(snip)....
    I found out today while reading my C book, that in another 2 chapters, the topic will be "Advanced uses of pointers." My previous attempt to retrieve the string,
    was used only after reading an earlier chapter called, "Introduction to pointers."

    I will hold off on posting my attempt until I read the Advanced Pointers stuff. And then after searching the FAQ's on this website. :-)

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Char*Pntr
    if that were the case, then I don't think C would have needed to implement the free() function.
    I do not understand your reasoning. You are effectively saying that memory leaks are fine. Your statement could make sense in specific cases where one can rely on the OS to reclaim the memory when the program terminates, or when garbage collection or RAII is used, but in general failing to use free() (or an equivalent function) in C when you should can lead to you running out of memory when there is memory that could have been used, but cannot be used because it is allocated yet there is no way to access it.

    Quote Originally Posted by Char*Pntr
    Also please note: I'm trying to look at the string while still inside main() i.e. before main exits.
    But even if I tried after main() completes, the string should still be there somewhere.
    No, the contents of the string might be there somewhere. The string no longer exists, and the memory may have been overwritten.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When your program allocates memory, that memory is removed from the "book" of available memory, by the operating system. Like a single copy of a book, that has been checked out of the library - it's not available for anyone else to check out.

    When your program free's that memory, that memory is put back into the "book" of available memory, which the operating system keeps. Now the book has been checked back into the library, and it's made available to others who want to check it out.

    From a programmer's point of view, it's just that simple. All the details "under the hood", belong to the compiler and operating system programmer's, only.

    Depending on the program, and on the operating system, the program MAY take the entire programs memory space, and put it back in the available "book", when your program ends. Because it helps stop memory from being lost to the OS.

    Whether your data in memory is strings, numbers, arrays, structs, or pointers, DOES NOT MATTER.

    You seem to have fixed preconceived idea's on this matter. May I suggest you ditch them? You won't learn much that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-18-2010, 07:25 AM
  2. free space on unknow device
    By hallo007 in forum C Programming
    Replies: 1
    Last Post: 12-27-2009, 07:16 PM
  3. Unreal Development Kit & Unity3d: Free (as in beer)
    By Mad_guy in forum General Discussions
    Replies: 0
    Last Post: 11-05-2009, 01:40 PM
  4. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  5. Why should I use the Free Space (heap)
    By BlackSlash12 in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2007, 06:57 PM