Quote:
if that were the case, then I don't think C would have needed to implement the free() function.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.
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:
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,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)....
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. :-)
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.Originally Posted by Char*Pntr
No, the contents of the string might be there somewhere. The string no longer exists, and the memory may have been overwritten.Originally Posted by Char*Pntr
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.