471,089 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

sizeof object

Hi all,

class A
{
};

int main()
{
A a;
cout<<"size "<<sizeof(a)<<endl;
}

Now if i run this, i get sizeof(a) = 1, size is 1byte. Experts say that
even class A is not having any members, 1 byte is to differentiate b/w
objects.

Suppose if i add one int data member to class A i.e

class A
{
int a;
};

int main()
{
A a;
cout<<"size "<<sizeof(a)<<endl;
}

I supposed to get size = 4 + 1 = 5.. but its 4.. Why is it?? Here how
does it differentiate between objects..

Whats the theory behind this??

Cheers..

Aug 17 '05 #1
1 4074
Radde wrote:
class A
{
};

int main()
{
A a;
cout<<"size "<<sizeof(a)<<endl;
}

Now if i run this, i get sizeof(a) = 1, size is 1byte. Experts say that
even class A is not having any members, 1 byte is to differentiate b/w
objects.
Yes, and that's one of the significant differences between C++ and C,
BTW.

The need to differentiate comes from the ability to create an array of
empty objects. If their size were 0, all of them would have the same
address, right? So, there would be no way to tell between them...
Suppose if i add one int data member to class A i.e

class A
{
int a;
};

int main()
{
A a;
cout<<"size "<<sizeof(a)<<endl;
}

I supposed to get size = 4 + 1 = 5.. but its 4.. Why is it?? Here how
does it differentiate between objects..
If the size is 4, couldn't you already differentiate? There is no need to
add another byte for that, is there?
Whats the theory behind this??


No theory. Just practice.

V
Aug 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sunil Menon | last post: by
2 posts views Thread by Ronald A. Andersen | last post: by
31 posts views Thread by Anjali M | last post: by
3 posts views Thread by Xiangliang Meng | last post: by
9 posts views Thread by M Welinder | last post: by
90 posts views Thread by pnreddy1976 | last post: by
28 posts views Thread by Howard Bryce | last post: by
15 posts views Thread by junky_fellow | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.