sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?)

A

Alex Vinokur

Alf P. Steinbach said:
* (e-mail address removed):
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn't find answer.

Needs a unique address.

struct Empty {};

C: sizeof(Empty) == 0
C++: sizeof(Empty) > 0

Why doesn't C need a unique address?
 
K

Keith Thompson

Alex Vinokur said:
Alf P. Steinbach said:
* (e-mail address removed):
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn't find answer.

Needs a unique address.

struct Empty {};

C: sizeof(Empty) == 0
C++: sizeof(Empty) > 0

Why doesn't C need a unique address?

In C,
struct Empty {};

is a syntax error. (Some compilers might support that as an
extension; if so, it's up to the compiler to decide what
sizeof(struct Empty) should be.)
 
S

swets

Keith said:
Alex Vinokur said:
Alf P. Steinbach said:
* (e-mail address removed):
This may be stupid question, but why is sizeof(Base) == 1 in:

int main(int argc, char* argv[])
{
class Base
{
};
cout << sizeof(Base) << endl;
return 0;
}

I guess I want to know what the 1 byte is for? There is no vptr here,
so why 1 byte?I checked FAQ and couldn't find answer.

Needs a unique address.

struct Empty {};

C: sizeof(Empty) == 0
C++: sizeof(Empty) > 0

Why doesn't C need a unique address?

In C,
struct Empty {};

is a syntax error. (Some compilers might support that as an
extension; if so, it's up to the compiler to decide what
sizeof(struct Empty) should be.)

What is the size of a in
int a[0];
 
S

Sweta

What if I use it in a structure like -
struct A {
int a[0];
};

What is the size of such structure?

Thanks,
Sweta

Igmar said:
swets said:
What is the size of a in
int a[0];

A zero size array is illegal according to ANSI C.


Igmar
 
H

Hallvard B Furuseth

Sweta said:
What if I use it in a structure like -
struct A {
int a[0];
};

What is the size of such structure?

Still not valid. Standard C does not allow 0-sized objects - and that
includes objects that are part of other objects.
 
I

Igmar Palsenberg

Sweta said:
What if I use it in a structure like -
struct A {
int a[0];
};

What is the size of such structure?

What isn't clear about 'a zero size array is illegal' ? Putting it in a
doesn't make it less of an array.


Igmar
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top