我有下面的代碼:ReAlloc如果根本就沒有diong什麼,不示數
#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' ... –
你說得對,我是。謝謝......現在來調試原來的程序。 –