<div><a target="_blank" href="http://www.todayhumor.co.kr/board/view.php?table=programmer&no=16432&s_no=16432&page=1">http://www.todayhumor.co.kr/board/view.php?table=programmer&no=16432&s_no=16432&page=1</a></div> <div><br></div>코드가 잘못됐다 하시는분 있는데 <b>절대 아닙니다.</b> <div><br></div> <div><span style="font-size:9pt;line-height:1.5;">[root@archlinux ~]# cat test.c</span></div> <div> <div><b>#include <stdio.h></b></div> <div><b><br></b></div> <div><b>int main(int argc, char **argv) {</b></div> <div><b> char *p = &"ABCD";</b></div> <div><b><br></b></div> <div><b> printf("%x \n", p);</b></div> <div><b> printf("%x \n", p+1);</b></div> <div><b> printf("%x \n", p+2);</b></div> <div><b> printf("%x \n", p+3);</b></div> <div><b> printf("%x \n", p+4);</b></div> <div><b> printf("------------\n");</b></div> <div><b><br></b></div> <div><b> printf("%x %x \n", &"ABCD", p);</b></div> <div><b><br></b></div> <div><b> return 0;</b></div> <div><b>}</b></div></div> <div><br></div> <div>흔해빠진 포인터 예제군요.</div> <div><br></div> <div>&"ABCD"가 틀렸다고 하신분. 틀렸다고 하신게 틀렸습니다.</div> <div><br></div> <div>"ABCD"는 컴파일 타임에 rodata 로 넘어가고 임시 심볼로 대체 됩니다. 그러므로 정당한 문법입니다.</div> <div><br></div> <div> <div>[root@archlinux ~]# gcc test.c -S</div> <div>test.c: In function ‘main’:</div> <div>test.c:4:12: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]</div> <div> char *p = &"ABCD";</div></div> <div> <div>[root@archlinux ~]# cat test.s</div> <div> .file "test.c"</div> <div> .section .rodata</div> <div><b>.LC0:</b></div> <div><b> .string "ABCD"</b></div> <div>.LC1:</div> <div> .string "%x \n"</div> <div>.LC2:</div> <div> .string "------------"</div> <div>.LC3:</div> <div> .string "%x %x \n"</div> <div> .text</div> <div> .globl main</div> <div> .type main, @function</div> <div>main:</div> <div>.LFB0:</div> <div> .cfi_startproc</div> <div> pushq %rbp</div> <div> .cfi_def_cfa_offset 16</div> <div> .cfi_offset 6, -16</div> <div> movq %rsp, %rbp</div> <div> .cfi_def_cfa_register 6</div> <div> subq $32, %rsp</div> <div> movl %edi, -20(%rbp)</div> <div> movq %rsi, -32(%rbp)</div> <div><b> movq $.LC0, -8(%rbp)</b></div> <div> movq -8(%rbp), %rax</div> <div> movq %rax, %rsi</div> <div> movl $.LC1, %edi</div> <div> movl $0, %eax</div> <div> call printf</div> <div><b> movq -8(%rbp), %rax</b></div> <div><b> addq $1, %rax</b></div> <div> movq %rax, %rsi</div> <div> movl $.LC1, %edi</div> <div> movl $0, %eax</div> <div> call printf</div> <div><b> movq -8(%rbp), %rax</b></div> <div><b> addq $2, %rax</b></div> <div> movq %rax, %rsi</div> <div> movl $.LC1, %edi</div> <div> movl $0, %eax</div> <div> call printf</div> <div><b> movq -8(%rbp), %rax</b></div> <div><b> addq $3, %rax</b></div> <div> movq %rax, %rsi</div> <div> movl $.LC1, %edi</div> <div> movl $0, %eax</div> <div> call printf</div> <div><b> movq -8(%rbp), %rax</b></div> <div><b> addq $4, %rax</b></div> <div> movq %rax, %rsi</div> <div> movl $.LC1, %edi</div> <div> movl $0, %eax</div> <div> call printf</div> <div> movl $.LC2, %edi</div> <div> call puts</div> <div><b> movq -8(%rbp), %rax</b></div> <div><b> movq %rax, %rdx</b></div> <div> movl $.LC0, %esi</div> <div> movl $.LC3, %edi</div> <div> movl $0, %eax</div> <div> call printf</div> <div> movl $0, %eax</div> <div> leave</div> <div> .cfi_def_cfa 7, 8</div> <div> ret</div> <div> .cfi_endproc</div> <div>.LFE0:</div> <div> .size main, .-main</div> <div> .ident "GCC: (GNU) 5.3.0"</div> <div> .section .note.GNU-stack,"",@progbits</div></div> <div><br></div> <div>대충 패턴 보이죠? C 코드에서 포인터 참조 한 것과, +1 +2 +3 한 부분입니다.</div> <div> <div><br></div> <div>[root@archlinux ~]# gcc test.c -c</div> <div>test.c: In function ‘main’:</div> <div>test.c:4:12: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]</div> <div> char *p = &"ABCD";</div> <div> ^</div></div> <div><span style="font-size:9pt;line-height:1.5;">[root@archlinux ~]# objdump test.o -D</span></div> <div> <div><br></div> <div>test.o: file format elf64-x86-64</div> <div><br></div> <div>생략</div> <div><br></div> <div>Disassembly of section .rodata:</div> <div><br></div> <div><b>0000000000000000 <.rodata>:</b></div> <div><b> 0: 41 rex.B</b></div> <div><b> 1: 42 rex.X</b></div> <div><b> 2: 43 rex.XB</b></div> <div><b> 3: 44 </b>00 25 78 20 0a 00 add %r12b,0xa2078(%rip) # a2082 <main+0xa2082></div> <div> a: 2d 2d 2d 2d 2d sub $0x2d2d2d2d,%eax</div> <div> f: 2d 2d 2d 2d 2d sub $0x2d2d2d2d,%eax</div> <div> 14: 2d 2d 00 25 78 sub $0x7825002d,%eax</div> <div> 19: 20 25 78 20 0a 00 and %ah,0xa2078(%rip) # a2097 <main+0xa2097></div></div> <div><br></div> <div>링크 하기 전이라, 00으로 시작하는군요. </div> <div><br></div> <div>그렇다면 링크까지 한다면?</div> <div><br></div> <div> <div>[root@archlinux ~]# gcc test.c</div> <div>test.c: In function ‘main’:</div> <div>test.c:4:12: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]</div> <div> char *p = &"ABCD";</div> <div> ^</div> <div>[root@archlinux ~]# objdump a.out -D</div> <div><br></div> <div>a.out: file format elf64-x86-64</div></div> <div><br></div> <div>생략</div> <div><br></div> <div> <div>Disassembly of section .rodata:</div> <div><br></div> <div>0000000000400680 <_IO_stdin_used>:</div> <div> 400680: 01 00 add %eax,(%rax)</div> <div> 400682: 02 00 add (%rax),%al</div> <div><b> 400684: 41 rex.B</b></div> <div><b> 400685: 42 rex.X</b></div> <div><b> 400686: 43 rex.XB</b></div> <div><b> 400687: 44</b> 00 25 78 20 0a 00 add %r12b,0xa2078(%rip) # 4a2706 <__FRAME_END__+0xa1f3e></div> <div> 40068e: 2d 2d 2d 2d 2d sub $0x2d2d2d2d,%eax</div> <div> 400693: 2d 2d 2d 2d 2d sub $0x2d2d2d2d,%eax</div> <div> 400698: 2d 2d 00 25 78 sub $0x7825002d,%eax</div> <div> 40069d: 20 25 78 20 0a 00 and %ah,0xa2078(%rip) # 4a271b <__FRAME_END__+0xa1f53></div></div> <div><br></div> <div>400684 에서 시작 하는군요.</div> <div><br></div> <div>실행 결과 볼까요?</div> <div><br></div> <div> <div>[root@archlinux ~]# ./a.out</div> <div>400684</div> <div>400685</div> <div>400686</div> <div>400687</div> <div>400688</div> <div>------------</div> <div>400684 400684</div></div> <div><br></div> <div><br></div> <div>네. 예제 자체는 문제도 없었고 의도는 다음과 같습니다.</div> <div><br></div> <div>1. 문자열 상수는 어디에 저장될까? 찍어보자.</div> <div>2. </div> <div> <div> char *p = &"ABCD";</div></div> <div> <div> printf("%x %x \n", &"ABCD", p);</div></div> <div>두가지 "ABCD"는 같은가? 찍어보자.</div> <div><br></div> <div><br></div> <div>이런 정상적인 예제를 왜 printf 로 A 찍고 B 찍고 C 찍는다고 마음대로 수정해서 설명하고 있는지 모르겠네요.</div> <div><br></div> <div>이런 예제는 빌드해서 돌리는데 몇십분씩 안걸립니다. </div> <div><br></div> <div>틀렸다고 주장하기 전에 한번이라도 빌드 돌리고 이야기 하시면 좋겠군요.</div>