빈 구조체에대한 처리에 대해서....

toy의 이미지

코드 -----------------------------------------------------------------------------
struct zero_struct
{
};

struct temp2
{
int a ;
struct zero_struct z1 ;
int b ;
struct zero_struct z2 ;
int c ;
};

int main()
{
struct temp2 temp2object ;
temp2object.a=10 ;
temp2object.b=20 ;
temp2object.c=30 ;

printf("%d\n", sizeof( temp2object) );
printf("a :%d\n", temp2object.a );
printf("z1:%d\n", temp2object.z1 );
printf("b :%d\n", temp2object.b );
printf("z2:%d\n", temp2object.z2);
printf("c :%d\n", temp2object.c );

printf("\n%x\n", &temp2object );
printf("offset(a) : %x\n", &(temp2object.a) );
printf("offset(z1): %x\n", &(temp2object.z1) );
printf("offset(b) : %x\n", &(temp2object.b) );
printf("offset(z2): %x\n", &(temp2object.z2) );
printf("offset(c) : %x\n", &(temp2object.c) );


return 0 ;
}
-----------------------------------------------------------------------------

결과 -----------------------------------------------------------------------
12
a :10
z1:10
b :20
z2:20
c :30
bffffb20
offset(a) : bffffb20
offset(z1): bffffb24
offset(b) : bffffb24
offset(z2): bffffb28
offset(c) : bffffb28

----------------------------------------------------------------------------

여기서 이상한점이 z1의 offset은 b와 같은데.... 값은 a가 찍힌다는겁니다.
어떻게 이런일이 가능할까요???? :oops:

vajna의 이미지

거참 궁금하네..
temp2object.z1값 찍기 전에 포인터값을
int *tmpptr = (int *)&(temp2object.z1);
로하고 *tmpptr값을 찍어도 마찬가지 인가요?
한번 해봐야지.. --a
..
공용체 쓰시죠..

머나먼 땅으로 우리 동무가 이제 떠나간다네.
고향의 바람이 불어와 동무 뒤를 따르고
사랑스러운 도시가 푸른 이내 속에서 사라지네.
정든 집, 푸른 동산, 부드러운 눈길도...

ssif의 이미지

오프셋 값중 z2가 c 와 같습니다.
스텍에 들어간 값은 a,b,c가 순서대로 들어가 있고
두번째 구조체의 시작 주소에서 offset된 지점의
값이 출력 되어 있는것이 아닌가 합니다.

짧은 지식이어서 제가 틀릴 수도 있습니다.

봄들판에서다

운형의 이미지

toy님 처럼 기발한(?) 생각으로 테스트 해본적은 없지만 스택은 주소가 할당되는 걸로 알고 있습니다(윈도우 쪽 플그램 공부할 때 주서 들은 것...)

int a;
int b;
int c;

라고 스택에서 메모리가 할당 되면.. 0x00 ~ 0x03 이 a, 0x04 ~ 0x07 이 b, 0x08 ~ 0x0a가 c 라면, toy님 생각이 맞겠죠. (0x00은 제가 예를 든 값입니다.)

실제로 스텍에서 할당할 수 있는 첫 주소가 0xff라고 가정하면,
0xff ~ 0xfc 가 a, 0xfb ~ 0xf8 이 b, 0xf7 ~ 0xf4가 c에 할당 됩니다.

위에 언급한 0xff는 제가 가정한 값이구요.. 운영체제 마다 정해진 값들이 있습니다. 윈도우에서는 22f06c였던걸로 기억하는데...
(과도한 술 담배는 뇌세포를 죽이죠..)

메모리 관리에 관한 부분을 운영체제에서 찾아보시면 나올겁니다.

Do you think that's the air you are breathing now?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.