简体   繁体   English

了解C内存分配和释放

[英]Understanding C Memory Allocation and Deallocation

I have been recently trying to learn how to program in the C programming language. 我最近一直在尝试学习如何使用C编程语言进行编程。

I am currently having trouble understanding how memory is deallocated by free() in C. 我目前在理解如何用C中的free()释放内存方面遇到麻烦。

What does it mean to free or release the memory? 释放或释放内存是什么意思?

For instance, if I have the following pointer: 例如,如果我有以下指针:

int *p = malloc(sizeof(int));

When I deallocate it using free(p) , what does it do? 当我使用free(p)释放它时,它有什么作用? Does it somehow flag it as " deallocated ", so the application may use it for new allocations? 是否以某种方式将其标记为“已释放 ”,以便应用程序可以将其用于新分配?

Does it deallocates only the pointer address, or the address being pointed is also deallocated too? 它是仅释放指针地址,还是所指向的地址也被释放?

I would do some experiments myself to better understand this, but I am so newbie in the subject that I don't know even how to debug a C program yet (I'm not using any IDE). 我将自己做一些实验以更好地理解这一点,但是我在这个主题上是个新手,以至于我什至都不知道如何调试C程序(我没有使用任何IDE)。

Also, what if int *p is actually a pointer to an array of int ? 另外,如果int *p实际上是指向int数组的指针怎么办?

If I call free(p) , does it deallocate the whole array or only the element it is pointing to? 如果我调用free(p) ,它会释放整个数组还是仅释放它所指向的元素?

I'm so eager to finally understand this, I would very much appreciate any help! 我非常渴望最终了解这一点,非常感谢您的帮助!

What does it mean to free or release the memory? 释放或释放内存是什么意思?

It means that you're done with the memory and are ready to give it back to the memory allocator. 这意味着您已经完成了内存的准备,可以将其还给内存分配器了。

When I deallocate it using free(p), what does it do? 当我使用free(p)释放它时,它有什么作用?

The specifics are implementation dependent, but for a typical allocator it puts the block back on the free list. 具体细节取决于实现,但是对于典型的分配器,它将块放回到空闲列表中。 The allocator maintains a list of blocks that are available for use. 分配器维护可使用的块列表。 When you ask for a chunk of memory (by calling malloc() or similar) the allocator finds an appropriate block in the list of free blocks, removes it (so it's no longer available), and gives you a pointer to the block. 当您请求一块内存(通过调用malloc()或类似方法)时,分配器会在空闲块列表中找到一个合适的块,将其删除(因此不再可用),并为您提供指向该块的指针。 When you call free() , the process is reversed -- the block is put back on the free list and thereby becomes available to be allocated again. 当您调用free() ,过程将反向进行-将该块放回到空闲列表中,从而可以再次分配它。

Importantly, once you call free() on a pointer, you must not dereference that pointer again. 重要的是,一旦对指针调用free() ,就不能再次取消对该指针的引用。 A common source of memory-related errors is using a pointer after it has been freed. 内存相关错误的常见来源是在释放指针后使用指针。 For that reason, some consider it a helpful practice to set a pointer to nil immediately after freeing it. 因此,有人认为释放指针后立即将指针设置为nil是一种有益的做法。 Similarly, you should avoid calling free() on a pointer that you didn't originally get from the allocator (eg don't free a pointer to a local variable), and it's never a good idea to call free() twice on the same pointer. 同样,您应该避免在最初不是从分配器获得的指针上调用free() (例如,不要释放指向局部变量的指针),并且在该指针上两次调用free()永远不是一个好主意。相同的指针。

Does it deallocates only the pointer address, or the address being pointed is also deallocated too? 它是仅释放指针地址,还是所指向的地址也被释放?

When you request a block of memory from the allocator, you specify the size of the block you want. 当您从分配器请求一个内存块时,您可以指定所需的内存块大小。 The allocator keeps track of the size of the block so that when you free the block, it knows both the starting address and the block size. 分配器跟踪块的大小,以便当您释放块时,它既知道起始地址,又知道块的大小。 When you call free(p) , the block that p points to is deallocated; 当您调用free(p)p指向的块被释放; nothing happens to the pointer p itself. 指针p本身没有任何反应。

Also, what if int *p is actually a pointer to an array of int? 另外,如果int * p实际上是指向int数组的指针怎么办?

An array in C is a contiguous block of memory, so a pointer to the first element of the array is also a pointer to the entire block. C语言中的数组是连续的内存块,因此指向数组第一个元素的指针也是指向整个块的指针。 Freeing that block will properly deallocate the entire array. 释放该块将适当地重新分配整个数组。

I'm so eager to finally understand this, I would very much appreciate any help! 我非常渴望最终了解这一点,非常感谢您的帮助!

There are a number of good pages about memory allocation in C that you should read for a much more detailed understanding. 关于C语言中的内存分配,有很多不错的页面,您应该阅读这些页面以获得更详细的了解。 One place you could start is with the GNU C Library manual section on memory allocation . 您可以从GNU C库手册中有关内存分配的章节开始

As alluded to above and in the other answers, the actual behavior of the allocator depends on the implementation. 正如上面和其他答案中提到的那样,分配器的实际行为取决于实现。 Your code shouldn't have any particular expectations about how memory allocation works beyond what's documented in the standard library, ie call malloc() , calloc() , etc. to get a block of memory, and call free() to give it back when you're done so that it can be reused. 除了标准库中记录的内容之外,您的代码对内存分配的工作方式没有任何特别的期望,即调用malloc()calloc()等来获取内存块,然后调用free()来返回内存完成后可以重复使用。

malloc and free do whatever they want. mallocfree做任何他们想做的事情。 Their expected behaviour is that malloc allocates a block of desired size in dynamic memory and returns a pointer to it. 他们的预期行为是malloc在动态内存中分配所需大小的块并返回指向它的指针。 free must be able to receive one such pointer and correctly deallocate the block. free必须能够接收一个这样的指针并正确地重新分配该块。 How they keep track of the block size is irrelevant. 他们如何跟踪块大小无关紧要。

Is int *p a pointer to an array of int s ? int *p是指向int s数组的指针吗? Maybe. 也许。 If you allocated sufficient space for several int s, yes. 如果您为几个int分配了足够的空间,则可以。

There is a fixed and limited amount of memory in your computer, and everybody wants some. 您的计算机中的内存是固定且有限的,每个人都想要一些。 The Operating system is charged with the task of assigning ownership to pieces of memory and keeping track of it all to assure that no one messes with anyone else's. 操作系统负责将所有权分配给内存块并对其进行跟踪,以确保没有人与其他人发生混乱。

When you ask for memory with malloc() , you're asking the system (the C runtime and the OS) to give you the address of a block of memory that is now yours . 当您使用malloc()请求内存时,您是在要求系统(C运行时和OS)为您提供现在属于的内存块的地址。 You are free to write to it and read from it at will, and the system promises that no one else will mess with it while you own it. 您可以随意对其进行写和读,而且系统保证在您拥有它的过程中,没有其他人会弄乱它。 When you de-allocate it with free() , nothing happens to the memory itself, it's just no longer yours. 当您使用free()取消分配它时,内存本身什么都没有发生,而是不再属于您。 What happens to it is none of your business. 发生什么事与您无关。 The system may keep it around for future allocations, it may give it to some other process. 系统可以保留它以备将来分配,也可以将其交给其他过程。

The details of how this happens vary from one system to another, but they really don't concern the programmer (unless you're the one writing the code for malloc / free ). 如何发生这种情况的细节在一个系统与另一个系统之间有所不同,但是它们确实与程序员无关(除非您是为malloc / free编写代码的人)。 Just use the memory while it's yours, and keep your hands off while it's not. 只需在内存中使用它,而在内存不使用时不要动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM