【发布时间】:2014-01-26 01:20:26
【问题描述】:
我有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char ** argv)
{
//just checking to see where the stack
printf("The stack is around %p\n", &argc); is, making sure arr isn't in it
char ** arr = malloc(8*sizeof(char*));
printf("arr is size %li, at %p\n", sizeof(arr), arr);
arr = realloc(arr, 100); //I picked a weird number to show it isn't doing anything. I've picked different numbers (like 200, 2*sizeof(char*)*sizeof(arr), and 16)
printf("arr is size %li, at %p\n", sizeof(arr), arr);
}
这是整个文件(这是一个单元测试;我在其他地方注意到了)
上面的输出如下:
The stack is around 0x7fff5b94d12c
arr is size 8, at 0x120f010
arr is size 8, at 0x120f010
也许我误解了 realloc 应该做什么。我期待以下输出。
The stack is around 0x7fff5b94d12c
arr is size 8, at 0x120f010
arr is size <size>, at <somewhere>
<size> 是...奇怪的值,例如 12...至少不是 8,<somewhere> 很可能是 0x120f010,但可能在任何合理的地方。
是我的期望错误还是我使用 realloc 不正确?
【问题讨论】:
-
真正的问题是你误解了
sizeof... -
你是对的,我是。谢谢...现在调试原程序。