<div>컴파일 환경</div> <div>x86_64-linux-gnu</div> <div>gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)</div><br /><br />코드<br /> <table border="1" width="100%" cellpadding="3" cellspacing="0" bordercolor="#000000" style="border-style: solid; border-collapse: collapse"> <tbody> <tr> <td><font face="Tahoma"><span style="font-size: medium; line-height: normal">#</span><span style="font-size: medium; line-height: normal; color: blue">include</span><span style="font-size: medium; line-height: normal"> <stdio.h><stdio.h> </stdio.h></span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal">#</span><span style="font-size: medium; line-height: normal; color: blue">include</span><span style="font-size: medium; line-height: normal"> <</span><span style="font-size: medium; line-height: normal; color: blue">string</span><span style="font-size: medium; line-height: normal">.h> </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> main() </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal">{ </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> { </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> *str1 = </span><span style="font-size: medium; line-height: normal; color: maroon">"hello!"</span><span style="font-size: medium; line-height: normal">; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> *str2; </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"str1(%d): %s\\n"</span><span style="font-size: medium; line-height: normal">, strlen(str1), str1); </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> str2 = str1; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"str2(%d): %s\\n"</span><span style="font-size: medium; line-height: normal">, strlen(str2), str2); </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"please input a character -> "</span><span style="font-size: medium; line-height: normal">); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: green">// *str2 = getchar(); // Segmentation fault here</span><span style="font-size: medium; line-height: normal"> </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"modified str2(%d): %s\\n"</span><span style="font-size: medium; line-height: normal">, strlen(str2), str2); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> }printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"\\n"</span><span style="font-size: medium; line-height: normal">); </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> { </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> a = </span><span style="font-size: medium; line-height: normal; color: maroon">65</span><span style="font-size: medium; line-height: normal">; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> *b = &</span><span style="font-size: medium; line-height: normal; color: maroon">"ABC"</span><span style="font-size: medium; line-height: normal">; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> *c = &</span><span style="font-size: medium; line-height: normal; color: maroon">"ABC"</span><span style="font-size: medium; line-height: normal">; </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"a=0x%x, *b=0x%x, *c=0x%x\\n"</span><span style="font-size: medium; line-height: normal">, a, *b, *c); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"&a=0x%x, b=0x%x, c=0x%x\\n"</span><span style="font-size: medium; line-height: normal">, &a, b, c); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"&\\"ABC\\" is 0x%x\\n"</span><span style="font-size: medium; line-height: normal">, &</span><span style="font-size: medium; line-height: normal; color: maroon">"ABC"</span><span style="font-size: medium; line-height: normal">); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"int value of \\"ABC\\" is 0x%x\\n"</span><span style="font-size: medium; line-height: normal">, *((</span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> *)&</span><span style="font-size: medium; line-height: normal; color: maroon">"ABC"</span><span style="font-size: medium; line-height: normal">)); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> }printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"\\n"</span><span style="font-size: medium; line-height: normal">); </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> { </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">typedef</span><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">struct</span><span style="font-size: medium; line-height: normal"> </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> { </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> a; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> b; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> c; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> } user_unpacked_type; </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> #pragma pack(push, </span><span style="font-size: medium; line-height: normal; color: maroon">1</span><span style="font-size: medium; line-height: normal">); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">typedef</span><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">struct</span><span style="font-size: medium; line-height: normal"> </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> { </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> a; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> b; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> c; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> } user_type; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> #pragma pop(); </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"size of user_unpacked_type is %d\\n"</span><span style="font-size: medium; line-height: normal">, </span><span style="font-size: medium; line-height: normal; color: blue">sizeof</span><span style="font-size: medium; line-height: normal">(user_unpacked_type));</span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"size of user_type is %d\\n"</span><span style="font-size: medium; line-height: normal">, </span><span style="font-size: medium; line-height: normal; color: blue">sizeof</span><span style="font-size: medium; line-height: normal">(user_type)); </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> { </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> i; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> user_type ut = {</span><span style="font-size: medium; line-height: normal; color: maroon">'A'</span><span style="font-size: medium; line-height: normal">, </span><span style="font-size: medium; line-height: normal; color: maroon">'B'</span><span style="font-size: medium; line-height: normal">, *((</span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> *)&</span><span style="font-size: medium; line-height: normal; color: maroon">"CDE"</span><span style="font-size: medium; line-height: normal">)}; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">for</span><span style="font-size: medium; line-height: normal">(i=</span><span style="font-size: medium; line-height: normal; color: maroon">0</span><span style="font-size: medium; line-height: normal">; i<</span><span style="font-size: medium; line-height: normal; color: blue">sizeof</span><span style="font-size: medium; line-height: normal">(user_type); i++) </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"ut[%d] is 0x%x\\n"</span><span style="font-size: medium; line-height: normal">, i, ((</span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> *)&ut)[i]); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"or otherwise....\n"</span><span style="font-size: medium; line-height: normal">); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">for</span><span style="font-size: medium; line-height: normal">(i=</span><span style="font-size: medium; line-height: normal; color: maroon">0</span><span style="font-size: medium; line-height: normal">; i<</span><span style="font-size: medium; line-height: normal; color: blue">sizeof</span><span style="font-size: medium; line-height: normal">(user_type)-</span><span style="font-size: medium; line-height: normal; color: blue">sizeof</span><span style="font-size: medium; line-height: normal">(</span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal">); i++) </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"ut[%d] is 0x%x\\n"</span><span style="font-size: medium; line-height: normal">, i, ((</span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> *)&ut)[i]); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> } </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> }printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"\\n"</span><span style="font-size: medium; line-height: normal">); </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> { </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">unsigned</span><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> a = </span><span style="font-size: medium; line-height: normal; color: maroon">1234567890</span><span style="font-size: medium; line-height: normal">; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> b = *((</span><span style="font-size: medium; line-height: normal; color: blue">int</span><span style="font-size: medium; line-height: normal"> *)&a); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> c = *((</span><span style="font-size: medium; line-height: normal; color: blue">char</span><span style="font-size: medium; line-height: normal"> *)&a); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">long</span><span style="font-size: medium; line-height: normal"> d = *((</span><span style="font-size: medium; line-height: normal; color: blue">long</span><span style="font-size: medium; line-height: normal"> *)&a); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"a = 0x%x\\n"</span><span style="font-size: medium; line-height: normal">, a); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"b = 0x%x\\n"</span><span style="font-size: medium; line-height: normal">, b); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"c = 0x%x\\n"</span><span style="font-size: medium; line-height: normal">, c); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"d = 0x%x\\n"</span><span style="font-size: medium; line-height: normal">, d); </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> }printf(</span><span style="font-size: medium; line-height: normal; color: maroon">"\\n"</span><span style="font-size: medium; line-height: normal">); </span><br style="font-size: medium; line-height: normal" /><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: blue">return</span><span style="font-size: medium; line-height: normal"> </span><span style="font-size: medium; line-height: normal; color: maroon">0</span><span style="font-size: medium; line-height: normal">; </span><br style="font-size: medium; line-height: normal" /><span style="font-size: medium; line-height: normal">} </span></font></td></tr></tbody></table><font face="Tahoma"><br /></font> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma">결과:</font></div> <div> <table border="1" width="100%" cellpadding="3" cellspacing="0" bordercolor="#000000" style="border-style: solid; border-collapse: collapse"> <tbody> <tr> <td><b><font face="Tahoma">// first running</font></b> <div> <div><font face="Tahoma">str1(6): hello!</font></div> <div><font face="Tahoma">str2(6): hello!</font></div> <div><font face="Tahoma">please input a character -> modified str2(6): hello!</font></div> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma">a=0x41, *b=0x41, *c=0x434241</font></div> <div><font face="Tahoma">&a=0x1ae753b0, b=0x4009c7, c=0x4009c7</font></div> <div><font face="Tahoma">&"ABC" is 0x4009c7</font></div> <div><font face="Tahoma">int value of "ABC" is 0x434241</font></div> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma">size of user_unpacked_type is 8</font></div> <div><font face="Tahoma">size of user_type is 6</font></div> <div><font face="Tahoma">ut[0] is 0x41</font></div> <div><font face="Tahoma">ut[1] is 0x42</font></div> <div><font face="Tahoma">ut[2] is 0x43</font></div> <div><font face="Tahoma">ut[3] is 0x44</font></div> <div><font face="Tahoma">ut[4] is 0x45</font></div> <div><font face="Tahoma">ut[5] is 0x0</font></div> <div><font face="Tahoma">or otherwise....</font></div> <div><font face="Tahoma">ut[0] is 0x44434241</font></div> <div><font face="Tahoma">ut[1] is 0x45</font></div> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma">a = 0x499602d2</font></div> <div><font face="Tahoma">b = 0x499602d2</font></div> <div><font face="Tahoma">c = 0xffffffd2</font></div> <div><font face="Tahoma">d = 0x499602d2</font></div></div> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma"><br /></font></div> <div><b><font face="Tahoma">// second running</font></b> <div> <div><font face="Tahoma">str1(6): hello!</font></div> <div><font face="Tahoma">str2(6): hello!</font></div> <div><font face="Tahoma">please input a character -> modified str2(6): hello!</font></div> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma">a=0x41, *b=0x41, *c=0x434241</font></div> <div><font face="Tahoma">&a=0xd94056b0, b=0x4009c7, c=0x4009c7</font></div> <div><font face="Tahoma">&"ABC" is 0x4009c7</font></div> <div><font face="Tahoma">int value of "ABC" is 0x434241</font></div> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma">size of user_unpacked_type is 8</font></div> <div><font face="Tahoma">size of user_type is 6</font></div> <div><font face="Tahoma">ut[0] is 0x41</font></div> <div><font face="Tahoma">ut[1] is 0x42</font></div> <div><font face="Tahoma">ut[2] is 0x43</font></div> <div><font face="Tahoma">ut[3] is 0x44</font></div> <div><font face="Tahoma">ut[4] is 0x45</font></div> <div><font face="Tahoma">ut[5] is 0x0</font></div> <div><font face="Tahoma">or otherwise....</font></div> <div><font face="Tahoma">ut[0] is 0x44434241</font></div> <div><font face="Tahoma">ut[1] is 0x45</font></div> <div><font face="Tahoma"><br /></font></div> <div><font face="Tahoma">a = 0x499602d2</font></div> <div><font face="Tahoma">b = 0x499602d2</font></div> <div><font face="Tahoma">c = 0xffffffd2</font></div> <div><font face="Tahoma">d = 0x499602d2</font></div></div></div></td></tr></tbody></table><br /></div> <div><br /></div> <div><br /></div> <div><br /></div> <div><br /></div> <div>코드와 실행 결과가 직관적으로 이해가신다면 당신은 C산맥 중 포인터 고개를 반정도 넘으신겁니다...</div> <div><br /></div> <div>함수포인터를 다루는 부분이나 구조체간 타입 캐스팅 같은 것들이 아직 남았...ㅠ<br /><br />근데 그거 끝내면 또 매크로와 레지스터 접근, CPU캐시와 메모리간의 일관성 관리 이슈가 또 있고</div> <div><br /></div> <div>하드웨어 깊숙한 곳까지 하드하게 하면 어셈블리 혼성 코딩이 또 있네요(....)</div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.