为什么内存不是从 malloc 清零? [英] Why memory isn't zero out from malloc?

查看:101
本文介绍了为什么内存不是从 malloc 清零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在书中读到:

动态内存分配器维护进程的虚拟区域内存称为堆.细节因系统而异,但不失一般性,我们将假设 堆是一个区域在未初始化之后立即开始的需求零内存bss 区域并向上增长(朝向更高的地址).

A dynamic memory allocator maintains an area of a process's virtual memory known as the heap. Details vary from system to system, but without loss of generality, we will assume that the heap is an area of demand-zero memory that begins immediately after the uninitialized bss area and grows upward (toward higher addresses).

所以,我很困惑为什么堆中的内存没有用零初始化.更准确地说,我的意思是 malloc 返回的一块内存.

So, I am confused why memory from heap isn't initialized with zero. To be more precise I mean a chunk of memory returned by malloc.

推荐答案

本书描述的是内存分配如何工作的一个例子.这是一个非常常见的例子,但有些平台的工作方式不同.它描述了一个具有虚拟内存的多任务平台.

What this book is describing is an example of how memory allocation can work. It's a very common example, but there are platforms that work differently. It describes a multitasking platform with virtual memory.

多任务平台上的内存分配有两个方面:首先任务从操作系统接收一些内存;然后任务本身管理自己的内存.您引用的段落描述了第一步.

There are two aspects to memory allocation on a multitasking platform: first the task receives some memory from the operating system; then the task itself manages its own memory. The paragraph you cited describes the first step.

在这个典型的平台上,当任务要求操作系统提供更多内存时(通过对内核进行系统调用,例如使用 brk 系统调用在传统 Unix 系统上),操作系统的内核找到一些未被任何其他任务使用的物理内存,将其标记在其内部数据结构正在使用中,并在称为 BSS 或堆的连续虚拟地址段的末尾在任务的内存映射中引用它们.此外,此内存已清零,以便任务不会读取其他任务留下的一些数据.

On this typical platform, when the task asks the operating system for more memory (by making a system call to the kernel, e.g. with the brk system call on traditional Unix systems), the kernel of the operating system finds some physical memory that isn't used by any other task, marks it in its internal data structures as being in use, and references them in the task's memory map at the end of a consecutive segment of virtual addresses called BSS or heap. Furthermore this memory is zeroed out so that the task won't read some data left over by another task.

malloc 函数通过分配堆区域的块来工作.它完全在任务的堆内工作.如果堆已满,它只会进行系统调用以获得更多内存.在大多数平台上,为了性能,malloc 函数不会覆盖它分配的内存.因此,当一块内存在从操作系统获得后第一次使用时,它会碰巧被清零.但是如果内存已经在任务内部使用了,因为它是用malloc获得的,然后用free释放,并被另一个malloc重用,那么内存将包含任务第一次放在那里的任何内容.

The malloc function works by allocating chunks of the heap area. It works entirely inside the task's heap. It only makes a system call to obtain more memory if the heap is full. On most platforms, the malloc function does not overwrite the memory that it allocates, for performance. So when a block of memory is used for the first time after it's been obtained from the operating system, it'll happen to be zeroed out. But if the memory has already been used inside the task because it's been obtained with malloc, then freed with free, and reused by another malloc, then the memory will contain whatever the task put there the first time around.

这篇关于为什么内存不是从 malloc 清零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆