The method of recycling the memory occupied by a variable depends on the storage type of the variable (storage class). For the structure you are talking about:
If it is defined within a function, it is called a local variable and is stored in the stack space. Its space will be released by itself after the function call ends.
If it is a global variable, it is stored in the DATA segment or BSS segment, and its space will always exist until the program ends.
If the space obtained by new or malloc is stored in HEAP (heap), unless it is manually delete or free, the space will be occupied until the end of the process.
For more information about storage types, please refer to this blog: http://harttle.github.io/2015/07/22/memory-segment.html
Memory allocation in C language occurs in three places:
Heap
Stack
Register
The so-called 栈, 入栈, 出栈, and 栈溢出 refer to this. They are allocated in the function and released in the function. The stack space is relatively small and is allocated when mutating. For details, you can see how the function is called. This is where you allocated it when int a = 1024.
So-called堆, let’s talk about virtual memory first. You must have heard this sentence: 32位的系统最大寻址是4g, this addressing corresponds to the memory you allocated on the heap, do you understand? Under 32-bit, each process can allocate up to 4g. You don't have to worry about how the memory is allocated. You only need to ask the system for it. It is allocated here when you malloc.
The so-called 寄存器变量 is the variable modified by the volatileregister keyword. I have never used it. It seems to tell the register that this variable should be stored in a register as much as possible. Maybe you will encounter it when making games.
Now do you understand why the memory is leaked? The system gives you the ability to allocate 4g of memory. It does not mean that you must use up all 4g of memory. It means that you have the ability to allocate it. Remember to return it to the system after use. Therefore, malloc needs to be free, and new needs to delete (C++)
(It’s a long time ago and may be wrong, just refer to it)
The key is to see how your data space is allocated,
If it is allocated by the compiler, it doesn’t matter, it will be automatically recycled when it leaves the scope.
If you assign it manually using a function like malloc, it must be in the appropriate location free
I would like to correct a question, can the attributes of a structure be assigned NULL? Isn't it possible to assign NULL to the structure pointer?
To give a simple example, int i=NULL;//printf("%d",i); what will be displayed?
Deallocates the space previously allocated by malloc(), calloc() or realloc(). If ptr is null-pointer, the function does nothing.
Did you see that? If you are dynamically allocating memory space, then you need to call free to release the memory space.
On the contrary, if you obtain the memory space statically and assign NULL to the pointer of the structure, you just make the physical address it points to point to NULL. If the scope of the structure does not end, will the memory space be automatically released? This obviously does not comply with the life cycle of the variable.
Of course not. In the situation you mentioned, if you directly set the pointer to NULL, then the memory is still occupied by the structure entity, but because you set the pointer to NULL, there is no pointer pointing to this piece of memory to be used. If you lose the address of this memory, it will cause a memory leak. This memory will not be released until your program exits.
But if your program is running all the time, such as many servers that need to run 24/7, then the lost memory will never be released, which may cause your memory to slowly leak, and in the end you can only restart the system
According to what you said, the attribute of the structure can be assigned a value of NULL. Assume that this attribute is a pointer. You also say that this attribute is not free. Assume that the value of this attribute is a pointer value returned by malloc. In this case, assign this attribute to NULL, and the memory space allocated by malloc is still in the heap. If this attribute is the last pointer of this memory space, after setting the pointer to NULL, no one will reference this memory. space, then this memory space is leaked and can no longer be reclaimed until the process exits.
The method of recycling the memory occupied by a variable depends on the storage type of the variable (storage class). For the structure you are talking about:
new
ormalloc
is stored in HEAP (heap), unless it is manuallydelete
orfree
, the space will be occupied until the end of the process.For more information about storage types, please refer to this blog: http://harttle.github.io/2015/07/22/memory-segment.html
闷声发大财,这是坠吼滴!
, but lz this is a memory leak.Memory allocation in C language occurs in three places:
Heap
Stack
Register
The so-called
栈
,入栈
,出栈
, and栈溢出
refer to this. They are allocated in the function and released in the function. The stack space is relatively small and is allocated when mutating. For details, you can see how the function is called. This is where you allocated it when int a = 1024.So-called
堆
, let’s talk about virtual memory first. You must have heard this sentence:32位的系统最大寻址是4g
, this addressing corresponds to the memory you allocated on the heap, do you understand? Under 32-bit, each process can allocate up to 4g. You don't have to worry about how the memory is allocated. You only need to ask the system for it. It is allocated here when you malloc.The so-called
寄存器变量
is the variable modified by thevolatile
register
keyword. I have never used it. It seems to tell the register that this variable should be stored in a register as much as possible. Maybe you will encounter it when making games.Now do you understand why the memory is leaked? The system gives you the ability to allocate 4g of memory. It does not mean that you must use up all 4g of memory. It means that you have the ability to allocate it. Remember to return it to the system after use. Therefore, malloc needs to be free, and new needs to delete (C++)
(It’s a long time ago and may be wrong, just refer to it)
Yes, this area has not been reclaimed. It will not be released until the program ends.
The key is to see how your data space is allocated,
If it is allocated by the compiler, it doesn’t matter, it will be automatically recycled when it leaves the scope.
If you assign it manually using a function like
malloc
, it must be in the appropriate locationfree
I would like to correct a question, can the attributes of a structure be assigned NULL? Isn't it possible to assign NULL to the structure pointer?
To give a simple example, int i=NULL;//printf("%d",i); what will be displayed?
Deallocates the space previously allocated by malloc(), calloc() or realloc(). If ptr is null-pointer, the function does nothing.
Did you see that? If you are dynamically allocating memory space, then you need to call free to release the memory space.
On the contrary, if you obtain the memory space statically and assign NULL to the pointer of the structure, you just make the physical address it points to point to NULL. If the scope of the structure does not end, will the memory space be automatically released? This obviously does not comply with the life cycle of the variable.
If you allocate heap memory for the structure, you must free();
Of course not. In the situation you mentioned, if you directly set the pointer to NULL, then the memory is still occupied by the structure entity, but because you set the pointer to NULL, there is no pointer pointing to this piece of memory to be used. If you lose the address of this memory, it will cause a memory leak. This memory will not be released until your program exits.
But if your program is running all the time, such as many servers that need to run 24/7, then the lost memory will never be released, which may cause your memory to slowly leak, and in the end you can only restart the system
According to what you said, the attribute of the structure can be assigned a value of NULL. Assume that this attribute is a pointer. You also say that this attribute is not free. Assume that the value of this attribute is a pointer value returned by malloc. In this case, assign this attribute to NULL, and the memory space allocated by malloc is still in the heap. If this attribute is the last pointer of this memory space, after setting the pointer to NULL, no one will reference this memory. space, then this memory space is leaked and can no longer be reclaimed until the process exits.
The answer on the first floor is more detailed, you should read more about the basics of C language