Search code examples
cpointersmemorymemory-managementfree

How to free pointer's space in memory?


I have question about pointers in C. Each pointer has 4 bytes in memory (address). When I call malloc() it only allocates memory and put it's address to the pointer, also free() only frees the memory that pointer is pointing to. But how can I delete pointer (4 bytes) in memory that I no longer need ? Isn't there a memory leak ?


Solution

  • The pointer itself is a regular variable, which means that when it goes out of scope those 4 bytes allocated for it will automatically be freed, just like any other variable you might have declared on that same scope.