Reputation: 1132
When we declare a string as:
char *x = "Hello";
What I have been knowing is that this string definition goes in the executable and thus is read only. Why does the compiler does not throws any error or there is no crash at runtime when I try to release the memory for this string as:
char *x = "Hello";
printf("String = %s\n", x);
free(x);
printf("String = %s\n", x);
And I get the string printed even after execution of free() statement. Is it like the free() interface works only on heap?
Upvotes: 1
Views: 55
Reputation: 33273
If you free()
memory that was not previously allocated the result is undefined. that means that the standard does not dictate what will happen.
Some compilers will insert code that throws a runtime exception and ends the program. Others may do nothing or even corrupt the memory.
Code that invokes undefined behaviour is almost always a bad thing.
To answer your question explicitly:
For practical purposes the answer is yes, free()
only works on the heap.
Note that the picky will point out that the heap is not a concept in the C standard; it is the practical solution for how most implementors handle dynamic memory allocation.
Upvotes: 5
Reputation: 47933
There are many different kinds of memory allocation. When we say that the compiler arranges to "allocate memory" for string constants such as "hello"
, we do not mean that the compiler used malloc
to do so.
malloc
is used for a particular kind of memory allocation: dynamic allocation (from the "heap"), by the programmer, at run-time. free
is used only to free memory allocated by malloc
(or, of course, realloc
). It does not make sense, it is not meaningful, to try to use free
to deallocate memory which was allocated in other ways.
In other words, as you suspected, yes, free
works only on the heap.
(Long ago, some systems had a function called donate()
, which let you add other scraps of memory to the heap, so that they were available to future calls to malloc
. This was used on systems where memory was very constrained, and where it was useful to recycle the memory used by string constants such as "hello"
, or even by no-longer-needed initialization functions. But that was a very long time ago, in the early days of C and Unix on the PDP-11. I haven't seen a donate()
function in the wild in a long time.)
Upvotes: 1
Reputation: 6063
free() in most implementations will just add the memory you hand over to it to a free list (a list of memory blocks available for allocation), without any further checks. On some compilers and OS, i would even allow you to hand over 1234 to free() (resulting in undefined behavior.)
Also, you seem to be assuming that free() would do anything to the contents of the memory you are freeing - No it doesn't. If you hand a piece of memory back to memory management, you are only telling it "can be re-used for other purposes" - Whether the values at that address are actually re-used depends on the memory manager.
C has been designed to be close to the hardware, assuming the programmer knows what he's doing. No safety net, no belts, no braces.
Upvotes: 0