Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
面试问的问题:
while(1) { void * a = malloc(1024*1024); }
2 gb这个循环将在ram 和ram上运行多少次8 gb?
2 gb
8 gb
我说无限循环是因为即使内存已满,也没有终止条件。他不同意。我现在不知道。请帮忙。
它应该无限期地运行。在大多数平台上,当没有更多可用内存时,malloc()将返回 0,因此循环将继续运行,而不会更改分配的内存量。Linux 允许内存过度使用,以便malloc()调用继续添加到虚拟内存中。当用于管理内存的数据开始引起问题时,该进程最终可能会被 OOM Killer 杀死malloc()(这不会是因为您尝试使用分配的内存本身,因为代码不使用它),但 Linux 不是'没有规定为问题中的平台。
malloc()