extern1.c
#include <stdio.h>
extern int *array;
int test();
int main(int argc, char *argv[])
{
printf ("in main: array address = %x\n", array);
test();
return 0;
}
extern2.c
int array[10] = {1, 2, 3};
int test()
{
printf ("in test: array address = %x\n", array);
return 0;
}
출력 :
in main: array address = 1
in test: array address = 804a040
그리고 조립 코드 :
08048404 <main>:
8048404: 55 push %ebp
8048405: 89 e5 mov %esp,%ebp
8048407: 83 e4 f0 and $0xfffffff0,%esp
804840a: 83 ec 10 sub $0x10,%esp
804840d: 8b 15 40 a0 04 08 mov 0x804a040,%edx <--------- this (1)
8048413: b8 20 85 04 08 mov $0x8048520,%eax
8048418: 89 54 24 04 mov %edx,0x4(%esp)
804841c: 89 04 24 mov %eax,(%esp)
804841f: e8 dc fe ff ff call 8048300 <[email protected]>
8048424: e8 07 00 00 00 call 8048430 <test>
8048429: b8 00 00 00 00 mov $0x0,%eax
804842e: c9 leave
804842f: c3 ret
08048430 <test>:
8048430: 55 push %ebp
8048431: 89 e5 mov %esp,%ebp
8048433: 83 ec 18 sub $0x18,%esp
8048436: c7 44 24 04 40 a0 04 movl $0x804a040,0x4(%esp) <------- this (2)
804843d: 08
804843e: c7 04 24 3d 85 04 08 movl $0x804853d,(%esp)
8048445: e8 b6 fe ff ff call 8048300 <[email protected]>
804844a: b8 00 00 00 00 mov $0x0,%eax
804844f: c9 leave
8048450: c3 ret
,
어셈블 코드에 < -------에주의하십시오. main 함수에서 배열은 array [0]이며, test 함수에서는 배열이 주소임을 알 수 있습니다.
하지만 실제로 메모리에서 어떻게됩니까? 배열 (40 바이트)과 포인터 (32 비트에서 4 바이트) 모두에 대해 메모리가 할당됩니까? –
정의되지 않은 동작 (실제로 형식이 잘못된 프로그램). 그러나 아마, 배열의 첫 번째 네 (또는 여덟) 바이트는 주소로 해석됩니다. –
괜찮지 만 링커는 포인터를 배열에 연결하며 컴파일 타임 오류는보고하지 않습니다. 실제로 연결된 것은 무엇입니까? – Azazle