Suppose I have a pointer *p which pointer to 10 memory blocks on the heap. Now, instead of free() it, I clear it manually with NULL (or ‘\0’) like this:
for (int i = 0; i < length; ++i{
p[i] = '\0';
}
Is this considered a memory block is freed when every bit is cleared to zero? Or, I have to use free() to tell the operating system that the memory block is really freed and is usable (I guess)?
The OS has data structures in the heap that keeps track of what’s allocated and where. Using malloc and free updates these structures, you cannot do it manually. You are leaking memory if you just zero the memory contents.