How to free up pointer space in memory?

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

+8
c memory-management pointers memory free
source share
5 answers

The pointer itself is a regular variable, which means that when it goes out of scope, these 4 bytes allocated for it will be automatically freed, like any other variable that you could declare in the same scope.

+12
source share

Typically, a pointer variable has the duration of automatic storage, that is, on the stack, so the memory it occupies will be restored and reused after the variable goes out of scope.

+7
source share

Typically, the pointer will be held in the stack variable. Or traced back through the allocated heap pointers to the stack variable. The stack is statically allocated and freed when the process terminates. So nothing leaks out.

+3
source share

A pointer is just a normal variable and has the same lifetime as a regular variable. I am a local non-static variable, its lifetime ends when it goes beyond. If it is a variable with a static storage duration, its lifespan ends when the program ends. If it is in dynamically allocated memory, its service life ends when this memory is freed, and for the future C1x standard, if it is a local thread variable, its lifetime expires when the thread ends.

+2
source share

in a nutshell .. you cannot free the location of the pointer or memory with any code or command .. you may need to make an ideal code program ... with acute memory consumption ... I get it. but you can't .. thanks ..

-one
source share

All Articles