By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

sizeof operator doesn't work properly in call with static data members

P: n/a
I comile the code in unix where sizof(int)=4
#include<iostream>
using namespace std;

class A
{
int i;
static int j;
};
int A::j = 10;
int main()
{
cout<<sizeof(A)<<endl;

}
both i,j are members of class A.so sizeof(A) should be printed as 8.
But i get 4.
could anyone explain this behavior.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 29 Nov 2003 21:15:49 -0800, ba*****@yahoo.co.in (balasubramaniam)
wrote:
I comile the code in unix where sizof(int)=4
#include<iostream>
using namespace std;

class A
{
int i;
static int j;
};
int A::j = 10;
int main()
{
cout<<sizeof(A)<<endl;

}
both i,j are members of class A.so sizeof(A) should be printed as 8.
But i get 4.
could anyone explain this behavior.


sizeof(A) give you the size of an instance of class A. j is not stored
in any instance of A, it is stored separately and shared by every
instance.

--
Be seeing you.
Jul 22 '05 #2

P: n/a
balasubramaniam wrote:
I comile the code in unix where sizof(int)=4
#include<iostream>
using namespace std;

class A
{
int i;
static int j;
};
int A::j = 10;
int main()
{
cout<<sizeof(A)<<endl;

}
both i,j are members of class A.so sizeof(A) should be printed as 8.
No.

Firstly, in general case size of class type cannot be calculated as
total size of its data members.

Secondly, static data members do not contrubute to size of class type.
But i get 4.
could anyone explain this behavior.


--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.