나는 다음과 같은 코드를 가지고 erroring하지, 아무것도 diong하지 :realloc을 단순히
#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이해야 할 일을 오해하고 있습니다. 나는 을 기대하고 다음 출력을 기대합니다. <size>
이
The stack is around 0x7fff5b94d12c
arr is size 8, at 0x120f010
arr is size <size>, at <somewhere>
... (12)와 같은 뭔가 이상한 ... 적어도 8 <somewhere>
은 대부분 0x120f010
그러나 가능 어디서나 합리적이다.
내 기대가 잘못되었거나 realloc을 잘못 사용하고 있습니까? 포인터의 크기를 변경하려고하고가 참조하는 메모리의 양을 말하려고하지 않는 포인터의 크기를 참가하는
sizeof char**
와 동일합니다
진짜 문제는 당신이'sizeof'를 오해하고 있다는 것입니다 ... –
당신 말이 맞아요. 고맙습니다 ... 이제 원본 프로그램을 디버그하십시오. –