473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
2 1412
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Russ Ford | last post by:
Hi all, I'm trying to get inheritance and constructors clear in my head (and in my code). I have the following inheritance situation (all derivations public): A is the base class B is...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
3
by: Christof Warlich | last post by:
Hi, I'm trying to build an _efficient_, _purely_ _abstract_ API for inter process(or) communication, _completely_ hiding any implementation details. The core components are "Buffer", "Address"...
1
by: nicolas.gendron | last post by:
Is there a way to do this? I don't want to use sizeof(non_static_member_variable_type) because if the type of my member variable changes, the sizeof operator won't be informe. Any solution? ...
32
by: moleskyca1 | last post by:
This may be stupid question, but why is sizeof(Base) == 1 in: int main(int argc, char* argv) { class Base { }; cout << sizeof(Base) << endl; return 0; }
2
by: GRenard | last post by:
Hello, We just switch in our company to VisualStudio 2005 and the new ATL library. We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe Here the call CFileDialogDeluxe...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.