简体   繁体   English

关于释放C中的内存分配

[英]Regarding releasing memory allocation in C

I have a multiple choice question about memory allocation C, but I'm sure which one is the right answer. 关于内存分配C,我有一个选择题,但我确定哪个是正确的答案。

  1. Which of the following statments about releasing memory allocation is false? 以下有关释放内存分配的哪些陈述是错误的?

    a. 一种。 It is an error to dereference a pointer to allocated memory after the memory has been released. 释放内存后,取消对分配的内存的指针的引用是错误的。

    b. b。 It is an error to free memory with a pointer to other than the first element of an allocated array. 使用指向已分配数组的第一个元素以外的指针的指针free内存是错误的。

    c. C。 Memory should be freed as soon as it is no longer needed. 不再需要内存时,应立即释放它。

    d. d。 Only one call to free is necessary to release an entire array allocated with calloc . 只需释放一次free即可释放分配有calloc的整个数组。

    e. e。 To ensure that it is released, allocated memory should be freed before the program ends. 为了确保释放它,应该在程序结束之前释放分配的内存。

They all look right to me :( ! Any idea? 他们都对我看起来不错:( !!有什么想法吗?

Thanks 谢谢

The answer is e. 答案是e。 When a process ends, the operating system will release all the resources it currently holds, including allocated memory. 当进程结束时,操作系统将释放其当前拥有的所有资源,包括分配的内存。

Therefore there is no need to free memory before ending a program. 因此,在结束程序之前无需释放内存。

As given by the other answers, e is the answer. 正如其他答案所给出的那样, e是答案。 The interesting option however is c which is highly subjective. 然而,有趣的选项是c ,它是非常主观的。 There's not necessarily any reason to free memory as soon as possible, and it's favorable to delay this in certain circumstances. 不一定有任何理由尽快释放内存,因此在某些情况下延迟这样做是有利的。

For example, if you know that a certain memory block will be surely re-allocated soon after it's freed, it may cost less to simply retain it in a pointer, rather than freeing it. 例如,如果您知道某个内存块在释放后一定会很快被重新分配,那么只需将其保留在指针中而不是释放它,可能会花费更少。 It would hopefully be clear in each case whether having such a retained pointer makes sense and fits within the architecture of the code (code smell!). 希望在每种情况下都清楚拥有这样一个保留的指针是否有意义并适合代码的体系结构(代码气味!)。 Sometimes a minor performance loss wins over having less readable code -- use your judgment and measure if unsure! 有时,较小的性能损失会胜于拥有较少可读的代码-如果不确定,请运用您的判断力并进行衡量!

a. 一种。 True: It is just a case of dangling pointer. 正确:这只是指针悬空的情况。

b. b。 True: The pointer passed to free() must point to the head address of the array. True:传递给free()的指针必须指向数组的头地址。

c. C。 Subjective and not always true, but not always false either. 主观的并不总是正确的,但也不总是错误的。

d. d。 True: Multiple freeing leads to undefined behavior. 正确:多次释放会导致未定义的行为。 Your program may crash, or it may launch a nuclear attack. 您的程序可能崩溃,或者可能发起核攻击。

e. e。 False: Memory allocated by malloc() and functions from its family, remains committed until they are de-allocated by means of free() . False:由malloc()及其家族中的函数分配的内存将保持提交状态,直到通过free()进行分配为止。 In general, using only standard library functions, a running C program has no way of knowing when allocated blocks of memory have been orphaned. 通常,仅使用标准库函数,正在运行的C程序无法得知何时孤立了所分配的内存块。 However, an operating system will reclaim all such memory when the process terminates. 但是,当进程终止时,操作系统将回收所有此类内存。

I think the option (e) is wrong. 我认为选项(e)是错误的。 Because when the program ends, all allocated memory is freed by the OS whether you do it manually or not. 因为程序结束时,无论您是否手动执行,操作系统都会释放所有分配的内存。 OS itself ensures that. 操作系统本身可以确保这一点。 No need to ensure it by freeing manually! 无需通过手动释放来确保它!

I think (e) is a right answer here, and everybody else is correct about it, as far as they go, but I think (c) is also a right answer. 我认为(e)在这里是正确的答案,就他们所知,其他所有人对此都是正确的,但我认为(c)也是正确的答案。

First, on some systems (embedded systems, perhaps), there might not be a guarantee that the OS will deallocate all memory. 首先,在某些系统(也许是嵌入式系统)上,可能无法保证OS会释放所有内存。 Admittedly not a common case these days, but then, so is C programming at all, it seems! 如今已经不常见了,但是看来C编程也是如此!

But more importantly, if you admit that the OS is going to deallocate all of your memory en masse anyway (e), why 'should' memory be freed as soon as it is no longer needed? 但是更重要的是, 如果您承认操作系统无论如何都会重新分配所有内存(e),为什么“一经使用”的内存就应该在不再需要时被释放? Unless you would have needed the memory earlier, isn't that just extra code, which takes time to run and can have bugs itself? 除非您早些时候需要内存,否则这不只是多余的代码,这需要一些时间才能运行,并且本身可能存在错误? (If I had a dollar for every deallocation bug I've found...) What if it's just 2 bytes, and it'd take more code to free it than memory you're freeing? (如果我为发现的每个释放错误都付出了一个美元……)如果它只有2个字节,并且要释放的内存比释放的内存还要多,该怎么办?

After having written many programs (and many more bugs), I think that memory should be released when it's convenient and can be done robustly, unless there's a good reason (and "I have a ton of memory allocated" can be a very good reason!). 在编写了许多程序(以及更多的bug)之后,我认为应该在方便的时候释放内存,并且可以可靠地完成,除非有充分的理由(并且“我分配了很多内存”可能是一个很好的理由) !)。 That's the point of making the OS free your pages when the program exits, after all. 毕竟,这就是使OS在程序退出时释放页面的目的。 That's also the point of GCs: object lifetimes are hard. 这也是GC的重点:对象生存期很困难。

"As soon as it is no longer needed" is for 1970's computers and standardized textbooks. “不再需要它”适用于1970年代的计算机和标准化教科书。

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

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