View Full Version : why an empty Structure.....
kim.wilzon
February 2nd, 2009, 05:40 AM
hi,
Please tell me why an empty Structure will occupy 2Bytes in C++ ?
Thanks
maximinus_uk
February 2nd, 2009, 05:44 AM
Your probably looking at the size of the pointer, rather than the size of a structure. Pointer being 32 bits, i.e. 2 bytes on a 32-bit machine.
Zugzwang
February 2nd, 2009, 05:46 AM
What is an empty structure? Is it something like this?
#include <iostream>
class Test {
};
int main() {
std::cout << "Sizeof(Test): " << sizeof(Test) << "\n";
}
Zugzwang
February 2nd, 2009, 05:47 AM
Your probably looking at the size of the pointer, rather than the size of a structure. Pointer being 32 bits, i.e. 2 bytes on a 32-bit machine.
No, that's 4 bytes. In fact, the 2 bytes thing looks rather incorrect. That might IMHO only be the case on some 16bit compilers with alignment enabled.
maximinus_uk
February 2nd, 2009, 05:59 AM
I hereby hand in my geek card. 32/8=4 :(
mdurham
February 2nd, 2009, 07:19 AM
It is curios indeed.
class Test {
char x[0];
};
results in a size of 0 (zero)
class Test {
};
results in a size of 1
What's going on here? It can't be due to alignment as someone suggested because sizeof(something) just gives the size of err... something.
doojsdad
February 2nd, 2009, 12:30 PM
Looks like it has something to do with the fact that "no object shall have the same address in memory as any other variable".
http://bytes.com/topic/c/insights/660463-sizeof-empty-class-structure-1-a
What compiler are you using?
Npl
February 2nd, 2009, 03:27 PM
sizeof on an Type/Class must be non-zero, its a requirement of C/C++ Standard.
sizeof on an Array can be zero.
mdurham
February 2nd, 2009, 07:01 PM
sizeof on an Type/Class must be non-zero, its a requirement of C/C++ Standard.
sizeof on an Array can be zero.
how do you explain this?
class Test {
char x[0];
};
sizeof(Test) results in a size of 0 (zero)
Using 64bit G++ in Jaunty
johnl
February 2nd, 2009, 07:14 PM
I tried compiling the same snippet -pedantic:
g++ test.cc -Wall -pedantic -o test
test.cc:6: error: ISO C++ forbids zero-size array `x`
It sounds to me like the ability to have a 0-sized structure is supported by g++ but not the C++ standard.
Compiling the following:
class Test {
};
with -pedantic resulted in sizeof(Test) == 1.
Npl
February 3rd, 2009, 06:22 AM
how do you explain this?
class Test {
char x[0];
};
sizeof(Test) results in a size of 0 (zero)
Using 64bit G++ in JauntyCompilerbug?
Returns 1 on MSVC.
Zugzwang
February 3rd, 2009, 07:31 AM
Compilerbug?
Returns 1 on MSVC.
No. That's called undefined behaviour.
Npl
February 3rd, 2009, 08:57 AM
No. That's called undefined behaviour.Do you agree that Test is a class, irregardless of its members?
class Test {
char x[0];
};
The C++ Standard is very clear about classes having a size > 0.
When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array. The size of a most derived class shall be greater than zero (1.8)
Zugzwang
February 3rd, 2009, 10:59 AM
The C++ Standard is very clear about classes having a size > 0.
Right, but it also does not allow arrays of size 0. Therefore, since the input isn't standard, the output does not need to be either.
Npl
February 3rd, 2009, 11:19 AM
Right, but it also does not allow arrays of size 0. Therefore, since the input isn't standard, the output does not need to be either.Again, what do you think Test is, Is it a class or not?
Either zero-sized arrays are an extension which means they dont break other rules (or atleast note what rules they affect). Or we talking about intentionally breaking the whole C++-Standard.
The third option is the most likely - planned to be an extension but buggy.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.