#include <stdio.h>
#include <stdlib.h>
int count = 0;
int * new_array() {
int i, *array = (int *) malloc(sizeof(int) * 9);
for(i = 0; i <= 9; i++)
array[i] = count++;
for(i = 0; i <= 9; i++)
printf("%d ", array[i]);
printf("\n");
return array;
}
int main(void) {
int i;
int *a;
for(i = 0; i < 10; i++) {
a = new_array();
}
return 0;
}
这在 64 位中运行良好,并且输出符合预期。
但是,在 32 位中,输出结果为: 0 1 2 3 4 5 6 7 8 9
出现错误信息:
prog: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
我不明白为什么。正如我所担心的,它在 64 位中也应该失败,因为我只分配了 9 个整数大小,但访问了数组的第 10 个元素。如果是这样的话,我为什么要关心长度?我可以给出一个随机数作为长度。
最佳答案
由于您在 malloc()
调用中仅为 9
元素分配内存,但稍后使用返回的指针,您将编写
for(i = 0; i <= 9; i++)
array[i] = count++;
这实际上是一个off-by-one场景,本质上是越界访问内存,创建 undefined behaviour .
FWIW,UB的输出是UB,它与32
或64
位架构无关。
也就是说,在直接使用返回的指针之前,请始终通过测试其返回值是否为 NULL 来检查 malloc()
是否成功。
另外,请see why not to cast C
中 malloc()
及其族的返回值。
关于C 代码在 64 位 Ubuntu 14.04 中有效,但在 32 位 Ubuntu 14.04 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32441032/