www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sizeof struct no less than 1?

reply "Mike" <none none.com> writes:
Hello,

Consider an 'empty' struct as follows:

struct Test {}

When printing it's size, it's always 1.
void main()
{
     writeln(Test.sizeof);
}

Output:
1

Why is it not 0?  What about struct Test is consuming 1 byte of 
memory?

Thanks,
Mike
Mar 21 2014
next sibling parent "Mike" <none none.com> writes:
On Saturday, 22 March 2014 at 02:27:02 UTC, Mike wrote:
 Hello,

 Consider an 'empty' struct as follows:

 struct Test {}

 When printing it's size, it's always 1.
 void main()
 {
     writeln(Test.sizeof);
 }

 Output:
 1

 Why is it not 0?  What about struct Test is consuming 1 byte of 
 memory?

 Thanks,
 Mike
Looks like this answers my question: http://forum.dlang.org/post/l03oqc$mpq$1 digitalmars.com
Mar 21 2014
prev sibling parent "Philpax" <phillip philliplarkson.com> writes:
C++ exhibits the same behaviour for what is most likely the same 
reason: so that empty structs can be allocated without two 
distinct objects having the same memory address ( 
http://stackoverflow.com/questions/2362097/why-is-the-size-of-an-empty
class-in-c-not-zero 
)
Mar 21 2014