Size of an empty class
-
Hi! What's the size of an empty class? When I execute the following code:
#include "iostream"
using namespace std;
class test
{
};
void main()
{
test test1;
std::cout<it prints 1. How it prints 1 with out any variable being declared?
-
Hi! What's the size of an empty class? When I execute the following code:
#include "iostream"
using namespace std;
class test
{
};
void main()
{
test test1;
std::cout<it prints 1. How it prints 1 with out any variable being declared?
-
Hi! What's the size of an empty class? When I execute the following code:
#include "iostream"
using namespace std;
class test
{
};
void main()
{
test test1;
std::cout<it prints 1. How it prints 1 with out any variable being declared?
-
Hi! What's the size of an empty class? When I execute the following code:
#include "iostream"
using namespace std;
class test
{
};
void main()
{
test test1;
std::cout<it prints 1. How it prints 1 with out any variable being declared?
The answer to your question is in the link provided by markkuk. You can actually store a one byte character in an empty class.
class test
{
};void main()
{
test test1;
*((char*)&test1) = 'A';
}«_Superman_» _I love work. It gives me something to do between weekends.
-
The answer to your question is in the link provided by markkuk. You can actually store a one byte character in an empty class.
class test
{
};void main()
{
test test1;
*((char*)&test1) = 'A';
}«_Superman_» _I love work. It gives me something to do between weekends.
Dangerous! The standard says tat distinct objects must have distinct identity (and if the identity is the address, a common way to get that is posing sizeof(A) = 1 by definition). But this is ... compiler dependent. (they can even use 4 bytes to preserve the alignment!) An Empty B inheriting the empty A may still have sizeof(B) == 1, not 2.
2 bugs found. > recompile ... 65534 bugs found. :doh:
-
Dangerous! The standard says tat distinct objects must have distinct identity (and if the identity is the address, a common way to get that is posing sizeof(A) = 1 by definition). But this is ... compiler dependent. (they can even use 4 bytes to preserve the alignment!) An Empty B inheriting the empty A may still have sizeof(B) == 1, not 2.
2 bugs found. > recompile ... 65534 bugs found. :doh:
Why is it dangerous (maybe I didn't get you)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Why is it dangerous (maybe I didn't get you)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Because what Santosh is trying to do is assign a char to a byte that it is not granted to really exist. (it may or not depending on the compiler implementation and level of optimization)
2 bugs found. > recompile ... 65534 bugs found. :doh:
-
Because what Santosh is trying to do is assign a char to a byte that it is not granted to really exist. (it may or not depending on the compiler implementation and level of optimization)
2 bugs found. > recompile ... 65534 bugs found. :doh:
I'm almost sure you're right (I cannot see other ways to implement it, but this is a problem of mine). Thank you. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi! What's the size of an empty class? When I execute the following code:
#include "iostream"
using namespace std;
class test
{
};
void main()
{
test test1;
std::cout<it prints 1. How it prints 1 with out any variable being declared?
Dear, empty class does not have any size. but size of operator always return +ve value. so you get empty class size 1.
-
An object must have non-zero size: http://www2.research.att.com/~bs/bs_faq2.html#sizeof-empty[^]