enum있는 struct의 sizeof

macmac83638의 이미지

typedef enum A{
ONE,
TWO,
THREE};

typedef struct myS {
int nBlaBla;
A myEnum;
};

myS mystructvar;

--
sizeof(mystructvar) 가 왜 9가 나올까요.
sizeof(int)는 4
sizeof(A)도 4가 나옵니다만..

memset(mystructvar, 0x00, sizeof(mystructvar));
memset(mystructvar, 0x00, 8);

위 둘중 무엇이 맞는 걸까요.
아 난해합니다.

girneter의 이미지

개념없는 초딩들은 좋은 말로 할때 DC나 웃대가서 놀아라. 응?

doldori의 이미지

vincentius wrote:
sizeof(mystructvar) 가 왜 9가 나올까요.

상당히 희귀한 환경이군요. 어떤 환경인가요?
vincentius wrote:
memset(mystructvar, 0x00, sizeof(mystructvar));
memset(mystructvar, 0x00, 8);

위 둘중 무엇이 맞는 걸까요.


sizeof(mystructvar)가 9라면 윗쪽이 맞지요. ^^;
스타일의 관점에서도 8(or 9?) 같은 magic number를 쓰는 것은 좋지 않습니다.
bleu의 이미지

이 문제 하고 패딩하고 직접적인 연관이 있는건가요?

girneter의 이미지

bleu wrote:

이 문제 하고 패딩하고 직접적인 연관이 있는건가요?

어쩌면 아닐지도 모르겠습니다.
보통은 패딩이 문제였는데,
확실치 않은 답글을 달았네요.

개념없는 초딩들은 좋은 말로 할때 DC나 웃대가서 놀아라. 응?

bleu의 이미지

신기하네요...흐..^^;;
첨보는 상황이라 그런데..
질문 하신문 어떤 환경에서 테스트 하신건지 알려 주실수
있을런지..?
부탁드립니다..^^;;

NoSyu의 이미지

진짜 신기하네요.

제가 C를 잘못알고 있나 싶어서 VC 6.0, Dev-C++로 돌려봤는데,

둘 다 8 == sizeof(mystructvar)이 되더군요.

신기신기..~~

c'est un des orgueils de notre pauvre humanit?, que chaque homme se croie plus malheureux qu'un autre malheureux qui pleure et qui g?mit ? c?t? de lui
- Le Comte de Monte-Cristo
-----------------------------------------------------------------------

pizza1977의 이미지

소설한번 써보면...
enum의 경우 정수(int)에 대한 열거형 타입입니다.
위와 같이 enum 타입의 인스턴스를 생성하였을때, 이 인스턴스가 갖을 수 있는 값은 열거형 타입내의 하나의 정수만 갖을 수 있을 것입니다.

그래서....사이즈가 sizeof(int)와 같지 않을까요....

앞서 말했지만 추측입니다.

sizeof(mystructvar) 가 왜 9가 나올까요. <-- 이건 오타인것 같습니다.
9는 나올 수 없을것 같습니다만....

-------------
포탈이는 불사신

addnull의 이미지

pizza1977 wrote:
sizeof(mystructvar) 가 왜 9가 나올까요. <-- 이건 오타인것 같습니다.
9는 나올 수 없을것 같습니다만....

enum의 크기를 재본적이 없었는데 신기하군요.
저도 sizeof(mystructvar) == 8, sizeof(A) == 4가 나왔습니다.

2005년 10월 19일.

pizza1977의 이미지

ㅋㅋ 어떻게는 사이즈가 9가 나오게끔 해보려고 했는데.. 안나오네요
패딩문제는 아닌것 같고....
어떤 환경인가요...정말 신기합니다.

-------------
포탈이는 불사신

cppig1995의 이미지

vincentius wrote:
typedef enum A{
ONE,
TWO,
THREE};

typedef struct myS {
int nBlaBla;
A myEnum;
};

myS mystructvar;

--
sizeof(mystructvar) 가 왜 9가 나올까요.
sizeof(int)는 4
sizeof(A)도 4가 나옵니다만..

memset(mystructvar, 0x00, sizeof(mystructvar));
memset(mystructvar, 0x00, 8);

위 둘중 무엇이 맞는 걸까요.
아 난해합니다.

long 이 32-bit 라고 가정하고,

- ONE, TWO, THREE 중 하나만이라도 32-bit 초과 40-bit 이하의 값을 가지는 매크로 변수라면
- enum 의 크기는 일부 컴파일러에서 최적화 확장에 따라 5바이트로 enum 의 크기를 조절할 수도 있다는

소설을 써 봅니다.

ps. 헝가리안 노테이션! nBlaBla~~~
ps. Bla 가 아니라 Blah 아니었나요? (둘 다 맞나?)

Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.