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 ?
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.
The pointer variable itself typically has automatic storage duration, i.e. it's on the stack, so the memory occupied by it will be reclaimed and reused once the variable goes out of scope.
Typically the pointer will be held in a stack variable. Or traced back through heap allocated pointers to a stack variable. The stack is statically allocated and freed when the process terminates. Thus nothing is leaked.
A pointer is just a normal variable, and has the same lifetime a normal variable would have in its place. I it's a local non-static variable, its lifetime ends when it goes out of scope. If it's a variable with static storage duration, its lifetime ends when the program ends. If it's located on dynamically allocated memory, its lifetime ends when that memory is freed, and for the future C1x standard, if it's a thread-local variable, its lifetime ends when the thread ends.
in an nut shell..you can not free up a pointer location or memory by any code or command..for you might be wanting to make a perfect program of codes..with acute memory consumption..i get it ..but you cant.. thanks..