Size of Class

why satic data member not consider in size of class?

1
2
3
4
5
6
7
8
9
10
11
  class Test
{
   static int i;
};

int main()
{
  cout<<sizeof(Test);
}

OutPut:- 1


static int i; is not a part of object of class Test but it is a part of class
Test, then why its not considerd?
Hi all,
check your code again please! , you call sizeof function with Test parametr and Test is name of class?? can we call functions like that? i didn't see it before!
after all, if that way of call functions is true maybe your problem is because of that: when you define an var... in class without any Classification (private , public , protected) it will define as private, so you cant use it in another function.
make it public and then check it again. and then post here the output again please.
Thanks

EDIT: Thank coder777 :D is that function(sizeof i mean) defined as template?
Last edited on
@amirtork
Yes, sizeof can be called like that. The [complete] type suffice for the size

@ak16
The point of static is that it's not part of the class.
sizeof gives you the size of objects. sizeof(Test) returns the size of any object of type Test.
sizeof() is not a function, it's a built-in part of the language. The compiler itself recognizes it and computes the value. So no, it is not defined with a template.

Regarding the original post, sizeof() returns the amount of memory taken by an instance of the variable (or type). Static members don't add to the memory requirement for an individual instance of the class.
Thanks all you guys for reply.

@amirtork
1. sizeof() :- can take data type also, u didn't see it before that's really surprising.
2. and I don't want to use class member out side , please read the real post again.
My concern is why satic data member not consider in size of class?
I hope now its clear to you.

@peter87
you said right that sizeof gives you the size of objects. sizeof(Test) returns the size of any object of type Test.
Then how we can calculate the size of class?
static member is not the part of each object means its the class level member.Thats means
<code>static int i;</code> is a class level.
Then my doubt is why its not under class size?


You want the size of all static members of a class? How would this information be useful? You could do it by hand by applying sizeof operator to each member individually. Note that this will not give a true picture of how much memory is used because there might be some padding and other overhead.
Last edited on
Hey peter , my concern is can we get the size of class?

if yes then how?

and in that size static member consider or not?
because static member are class members so hey should be the part of class size.
A class doesn't really have a size. It's just a description of objects.

In other words, no, it's not possible to get the size of a class because it's not clear what the size of a class is. The C++ standard doesn't define what it means. The size of a class could be ...
1. the number characters or the number of lines used to define the class in the source code.
2. the size of an instance of the class.
3. the size of all static members of the class.
4. the sum of 2 and 3.
5. something else.

If you want to know how to get the size of a class you better define what it means first.
Last edited on
Empty class have 1 byte size.
Empty class have 1 byte size.
Only because standard says so. Empty struct is 0 in C.
Edit: empty structs are GCC extencion in C and any other way to have an "empty" struct is technically is undefined behaior

static members are not really physically part of class. They are not present in the instance of class, how and where are they stored depends on compiler. Size of class is even more vague if you will think a bout inheritance and dynamic dispatch: class will have virtual table linked with it and merely adding another function can increase size of this table. Or maybe your compiler implements vtables that way that they are partially overlaps? How would you calculate class size now?
Last edited on
sizeof() tells you how much space must be allocated to store an instance the class. If you think of it this way, then it makes sense that sizeof() excludes the size of static members. IN particular, consider a class with 1MB if static data and 1 byte of non-static data. If you allocate an array of 1,000 of these, you want the array to occupy 1,000 bytes, not 1,000MB.

Defining the size of an empty class as zero means that you can still allocate space for it. Better yet, you can allocate an array of them.

If you want to know the space occupied by the static members, you can probably do something like this, although it isn't portable.
1
2
3
4
5
6
7
8
9
10
char start;
someType MyClass::staticMember1;
someType MyClass::staticMember2;
// etc/
char end;

size_t MyClass::sizeofStaticMembers() const
{
    return &end - &start;
}

[/code]

Topic archived. No new replies allowed.