#include<iostream>
usingnamespace std;
class sample
{
int i;
int j;
};
int main()
{
cout<<sizeof(sample);
return 0;
}
[output]8[/output]
Conceptually, the class is loaded into memory only when we create an object of the class. Only after creating an object, memory is assigned to data members. So in above case, if I try to find size of the class, shouldn't it be something like 1 byte? I do know how declaration differs from the definition. But how is it treated in terms of a class?
Thanks in advance :)
A class acts as a template for future instantiations of that class. The compiler knows how to align the members and how much memory each member should allocate. With this information, the compiler can compute the size of the class by taking into consideration:
- The virtual table
- Padding
- The size of each member
"Virtual Table" is a pointer to a table of virtual class functions. Typically, this is the same size as a pointer.
"Padding" is extra bytes between each member. These extra bytes align the members so that CPU's read and write operations are more efficient.
Thanks for the replies :)
Yes, as I said, I am aware of the difference between declaration and definition. It's just that, the compiler anyway knows the size of member variables so why do we have the concept of "the class is allocated memory only after we create an object of the class?" If memory is anyway allocated and size of variables is also determined, I don't see the concept behind above statement :) Is there any difference in allocation when we create an object?
It's just that, the compiler anyway knows the size of member variables so why do we have the concept of "the class is allocated memory only after we create an object of the class?"
Knowing how much memory you need for something is different from actually having that memory.
Knowing that a pizza costs $12 is different from actually paying $12 to buy a pizza. sizeof(aclass) just tells you how much space is needed to create an instance of aclass. This does not mean that the space is already allocated... it just means if you are going to allocate space for it, you will need that many bytes.
If memory is anyway allocated
...
Is there any difference in allocation when we create an object?
It isn't.
And yes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class sample
{
int i;
int j;
};
// at this point, we have zero memory allocated (*)
cout << sizeof(sample); // this prints 8, even though we are not using any memory
sample a; // this actually allocates space for one 'sample' called 'a'
// we are now using 8 bytes of memory
sample b; // this allocates another 8 bytes for another sample called 'b'
// now we are using 16 bytes of memory total
// ... etc
* technically any running program will never actually be using zero memory, but hopefully you get what I mean
sizeof(T) is evaluated at compile time; it is just a number that is known to the compiler (at compile time before the program is run).
The sale price of the book 'Ruminations on C++' is (say) $24.50 - that is just a number that is known to amazon.com when the web page is created; before even one copy is actually sold.
Objects of type T are created at run-time; each object of type T 'would cost' sizeof(T) bytes.
Each copy of 'Ruminations on C++' would cost $24.50 - if I buy ten copies of the book (at run-time) I would have to pay $245.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main()
{
int i = 9 ;
int j = sizeof(++i) ; // type of ++i, int, and sizeof(int) is evaluated at compile-time
std::cout << i << '\n' ; // 9
j = ++i ; // ++i is evaluated at run-time; and i is incremented
std::cout << i << '\n' ; // 10
}
@disch - Sorry for the late reply. I was out of town. :) Well, that explanation solves most of the doubts I had :D Thank you so much for replying to the point.
@JLBorges - I guess you have given a wrong example to prove your point :)
But anyway, thanks for the input and explanation :)