About malloc and free

struct mem_control_block 
{ 
    int is_available;     // This is a mark 
    int size;             // This is the size of the actual space 
};
void free(void *ptr) 
{
     struct mem_control_block *free;
     free = ptr - sizeof(struct mem_control_block);
     free->is_available = 1;
     return;
}

Free just turns the space into available free space, and there is no initialization, so every time malloc is used to allocate space, it is initialized, generally memset (), such as memset (buf, 0, 100);

You can also use calloc instead of malloc to allocate memory, calloc function will automatically initialize the applied memory.

Guess you like

Origin www.cnblogs.com/zealfish/p/12690364.html