About the size of structure

  • Thread starter Albert Yan Qing Ge
  • Start date
A

Albert Yan Qing Ge

typedef struct AA {
} A;

when use gcc compiles, sizeof(A) is 0
when use g++ compiles, sizeof(A) is 1

So I can known the length of this pointer is 1

but when I add one member to the structure,
typedef struct AA {
int i;
} A;

Now if use gcc, sizeof(A) is 4
if use g++, sizeof(A) is still 4.

where is this pointer?

Thanks
 
R

Rob Williscroft

Albert Yan Qing Ge wrote in
typedef struct AA {
} A;

when use gcc compiles, sizeof(A) is 0
when use g++ compiles, sizeof(A) is 1

Yup C++ insists every type has a minimum size of 1, I don't know
about C so whether gcc output is correct I can't say.
So I can known the length of this pointer is 1

What pointer ?. There is non *special* pointer stored in every C++
user defined type.
but when I add one member to the structure,
typedef struct AA {
int i;
} A;

Now if use gcc, sizeof(A) is 4
if use g++, sizeof(A) is still 4.

The extra byte in the first example was just padding (or a filler byte
if you like), it'e not some special value (i.e. 'this').

where is this pointer?

Again what pointer !

HTH.

Rob.
 
J

Jack Klein

typedef struct AA {
} A;

when use gcc compiles, sizeof(A) is 0

GCC is a C compiler. The structure definition is illegal in C, where
structures with no members are not allowed. So you are not using GCC
as a conforming C compiler, consult your documentation or a gcc
newsgroup for how to do this.
when use g++ compiles, sizeof(A) is 1

Structs or classes with no members are allowed in C++, but 0 length
objects are forbidden. So a class or struct with no members must have
sizeof of at least 1. Otherwise it would be impossible to have an
array of such objects, as each array element must have a unique
address.
So I can known the length of this pointer is 1

Do you mean the "this" pointer that is passed to non-static member
functions? The sizeof value of a class never includes room for that,
it is not a member of the class or struct.
but when I add one member to the structure,
typedef struct AA {
int i;
} A;

Now if use gcc, sizeof(A) is 4
if use g++, sizeof(A) is still 4.

where is this pointer?

The "this" pointer is not EVER in the class or struct. It has no
effect on the size of a class or struct.

You need to get a good book on C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

JustSomeGuy

Try #pragma pack(1)
before the structure definition... then check your sizeof(...)
 
R

Ron Natalie

Rob Williscroft said:
Albert Yan Qing Ge wrote in

Yup C++ insists every type has a minimum size of 1, I don't know
about C so whether gcc output is correct I can't say.
The above struct is illegal in C. You aren't allowed to define
empty structs. GCC is wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top