c++ - C语言销毁一个结构体不free直接等于NULL可以吗
黄舟
黄舟 2017-04-17 12:04:40
0
9
900

各位大神 如果我一个结构体 的某个属性存了1000个数据 然后现在不想用了 我直接 把这个结构体的这个属性 == NULL 是不是就表示 这个结构体 又成了一个全新的结构体了 但是 我并没有free 那么是不是 堆内存里面 始终有一块区域没有被回收呢?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(9)
Peter_Zhu

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:

  1. 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.
  2. 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.
  3. 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

黄舟

闷声发大财,这是坠吼滴!, 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 the volatile 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 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.

左手右手慢动作

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template