`malloc()`刚刚分配的内存内容是什么?
What are the contents of the memory just allocated by `malloc()`?
我很好奇在malloc()
被用于分配内存space之后,指针究竟持有什么?联机帮助页告诉我 calloc()
将分配的内存 space 初始化为零。
The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
和
The calloc() function allocates memory for an array of nmemb elements of size bytes each
and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or
size is 0, then calloc() returns either NULL, or a unique pointer value that can later
be successfully passed to free().
我用 C 创建了一个非常短的示例程序,为自己编写了 C(哈哈):
int main() {
char *dynamic_chars;
unsigned amount;
printf("how much bytes you want to allocate?\n");
scanf("%d", &amount);
dynamic_chars = (char*)malloc(amount*sizeof(char));
printf("allocated:\n%s\n", dynamic_chars);
free(dynamic_chars);
return 0;
}
但是当执行这段代码时,它什么也没有输出。如果我自己初始化内存,例如使用循环用 0xFFFF
初始化每个字节,那么程序会准确地显示我所期望的。内存 space 实际上存在,因为我不会收到声称我正在尝试访问未初始化变量等的错误。
由于内存 space 通常不会被删除但被标记为可重写我想知道是否通过执行我的程序,我是否应该能够看到随机的以前使用的内存字节?但是我什么也看不到,所以我真的很困惑 malloc()
究竟是如何工作的。
编辑1
关于 malloc()
或者一般内存使用的另一件事,这对我的程序很有趣:
如果我使用 calloc()
分配内存,我可以跟踪我的程序的实际内存使用情况,例如监控它。例如,如果我告诉我的程序,为每个 calloc()
分配 1.000.000.000 字节的内存,我将在我的系统监视器中看到以下内容:
正如您可能想象的那样,当使用 malloc()
时,我什么也看不到。我明白,只是通过分配内存,我当时并没有真正使用它,但我仍然对为什么我的操作系统(unix derivate)不会识别它被使用感到困惑。由于 malloc()
就像 calloc()
returns 一个我没有得到的内存位置的物理地址,这个内存区域似乎并没有被 OS 实际保留。否则我可以在系统监视器中看到它吗?
如果我宁愿 post 这是一个新问题,请告诉我。但我认为,既然问题仍然是关于 malloc()
是如何工作的,那么它适合放在这里。
否,malloc()
returns 未初始化内存,其内容不确定。因此,尝试 使用 值调用 undefined behavior.
引用 C11
,附件 §J.2,未定义的行为
The value of the object allocated by the malloc
function is used
在这种情况下,%s
需要一个以 null 结尾的 char
数组。然而,dynamic_chars
的内容是不确定的,所以很可能根本没有空终止符,这将导致越界内存访问,进而调用UB。
引用 C11
,章节 §7.22.3.5,malloc
函数(强调我的):
The malloc
function allocates space for an object whose size is specified by size
and
whose value is indeterminate.
也就是说,please see this discussion on why not to cast the return value of malloc()
and family in C
.。
malloc 为您分配内存并设置指向它的指针。它不以任何方式初始化内存,因此分配的内存区域可以包含任何内容。由于它不包含字符串,因此您无法通过打印字符串来读取它的内容。相反,您可以逐字节打印它,如下所示:
for(int i=0;i<amount*sizeof(char);i++)
{
printf("%02x", (unsigned)dynamic_chars[i]);
}
获取内存块时,C语言未定义内存块包含什么。实际上,它很可能只包含以前物理内存中的内容。
如果内存之前被您的程序使用过并被释放,您可能只会得到之前的内容。如果它是操作系统新请求的内存,您将获得操作系统放入其中的内容。大多数操作系统 return 内存已专门设置为 'zero' 字节,因为如果内存仍然包含之前其他程序中的内容,这将是一个安全问题。
None 任何标准都可以保证,这正是大多数系统在实践中所做的。
我很好奇在malloc()
被用于分配内存space之后,指针究竟持有什么?联机帮助页告诉我 calloc()
将分配的内存 space 初始化为零。
The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
和
The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
我用 C 创建了一个非常短的示例程序,为自己编写了 C(哈哈):
int main() {
char *dynamic_chars;
unsigned amount;
printf("how much bytes you want to allocate?\n");
scanf("%d", &amount);
dynamic_chars = (char*)malloc(amount*sizeof(char));
printf("allocated:\n%s\n", dynamic_chars);
free(dynamic_chars);
return 0;
}
但是当执行这段代码时,它什么也没有输出。如果我自己初始化内存,例如使用循环用 0xFFFF
初始化每个字节,那么程序会准确地显示我所期望的。内存 space 实际上存在,因为我不会收到声称我正在尝试访问未初始化变量等的错误。
由于内存 space 通常不会被删除但被标记为可重写我想知道是否通过执行我的程序,我是否应该能够看到随机的以前使用的内存字节?但是我什么也看不到,所以我真的很困惑 malloc()
究竟是如何工作的。
编辑1
关于 malloc()
或者一般内存使用的另一件事,这对我的程序很有趣:
如果我使用 calloc()
分配内存,我可以跟踪我的程序的实际内存使用情况,例如监控它。例如,如果我告诉我的程序,为每个 calloc()
分配 1.000.000.000 字节的内存,我将在我的系统监视器中看到以下内容:
正如您可能想象的那样,当使用 malloc()
时,我什么也看不到。我明白,只是通过分配内存,我当时并没有真正使用它,但我仍然对为什么我的操作系统(unix derivate)不会识别它被使用感到困惑。由于 malloc()
就像 calloc()
returns 一个我没有得到的内存位置的物理地址,这个内存区域似乎并没有被 OS 实际保留。否则我可以在系统监视器中看到它吗?
如果我宁愿 post 这是一个新问题,请告诉我。但我认为,既然问题仍然是关于 malloc()
是如何工作的,那么它适合放在这里。
否,malloc()
returns 未初始化内存,其内容不确定。因此,尝试 使用 值调用 undefined behavior.
引用 C11
,附件 §J.2,未定义的行为
The value of the object allocated by the
malloc
function is used
在这种情况下,%s
需要一个以 null 结尾的 char
数组。然而,dynamic_chars
的内容是不确定的,所以很可能根本没有空终止符,这将导致越界内存访问,进而调用UB。
引用 C11
,章节 §7.22.3.5,malloc
函数(强调我的):
The
malloc
function allocates space for an object whose size is specified bysize
and whose value is indeterminate.
也就是说,please see this discussion on why not to cast the return value of malloc()
and family in C
.。
malloc 为您分配内存并设置指向它的指针。它不以任何方式初始化内存,因此分配的内存区域可以包含任何内容。由于它不包含字符串,因此您无法通过打印字符串来读取它的内容。相反,您可以逐字节打印它,如下所示:
for(int i=0;i<amount*sizeof(char);i++)
{
printf("%02x", (unsigned)dynamic_chars[i]);
}
获取内存块时,C语言未定义内存块包含什么。实际上,它很可能只包含以前物理内存中的内容。
如果内存之前被您的程序使用过并被释放,您可能只会得到之前的内容。如果它是操作系统新请求的内存,您将获得操作系统放入其中的内容。大多数操作系统 return 内存已专门设置为 'zero' 字节,因为如果内存仍然包含之前其他程序中的内容,这将是一个安全问题。
None 任何标准都可以保证,这正是大多数系统在实践中所做的。