我已经研究过堆栈和堆,以及一般的内存管理主题,但有很多东西我无法理解
就像,如果我使用堆分配一个整数数组malloc,realloc我如何确定我想要使用的数组的确切大小?
这个例子:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
int *array = (int *)malloc(2);
array[0] = 2;
array[1] = 1;
//What? i have allocated just 2 to be the size
array[2] = 3;
array[3] = 4;
array[4] = 4;
array[5] = 6;
//there is no segmentation fault
for(int i = 0; i < sizeof(array); i++){
printf("%d\n",array[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的奇怪结果是:
2
1
3
4
4
6
1041 // ???
0
Run Code Online (Sandbox Code Playgroud)
那么,有人可以向我解释一下我怎样才能malloc以100%正确的方式使用吗?
要使用 正确分配数组malloc,请使用 sizeof 确定数组中每个元素的大小,然后乘以所需的每个元素的数量。
您的代码仅在堆中分配 2 个字节的内存,因此当您写入这些整数(在我的机器上每个整数占用 4 个字节)时,您将覆盖堆中位于这两个字节之外的不相关状态的值,从而损坏机器状态并创建未定义的(这是不好的)行为。
此外,您的for循环是在数组指针的大小上循环的,该大小通常为 8 个字节。因此,您的for循环将尝试遍历 6 个 int 数组中的 8 个 int 元素,其中您只分配了 2 个字节,而不是所需的 24 个字节。这里有很多不好的未定义行为!
您可能会也可能不会因此而出现分段错误。分段错误意味着您取消引用指向无效内存页(段)的指针,并且这会被硬件捕获。
当您损坏内存时,如果您正在写入的内存是有效内存,则您可能不会立即将错误结果视为分段错误。更糟糕的是,如果损坏了堆栈或指针,可能需要很长时间才能获得实际故障以帮助检测所产生的损坏。这使得很难将错误与导致异常的事件联系起来,因为您的代码在出现分段错误之前可能会运行很长时间。
#include <stdio.h>
#include <stdlib.h>
#define NUM_INTS_WANTED 6
int main(int argc, char* argv[]){
// No more trampling memory since array allocated to correct size
int *array = malloc(sizeof(int)*NUM_INTS_WANTED);
// Always check if malloc succeeded by checking that pointer is not NULL
if (array != NULL) {
array[0] = 2;
array[1] = 1;
array[2] = 3;
array[3] = 4;
array[4] = 4;
array[5] = 6;
// No more seeing 8 ints since you stop after 6 ints now
for(int i = 0; i < NUM_INTS_WANTED; i++){
printf("%d\n",array[i]);
}
} else {
// malloc failed! Report it.
printf("malloc failed!\n");
}
}
Run Code Online (Sandbox Code Playgroud)