Memory allocation to class

Here is my code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
using namespace std;

class Person
{
private :
    string name;
    int age ;
    float salary ;
    char yes ;

public :
    void getInput(string n , int a)
    {
        name = n ;
        age = a ;
    }

    void display()
    {
        cout << "Name " << name << " Age " << age << endl;
    }
};

int main()
{
    Person raman ;
    raman.getInput("Raman",22);
    raman.display();
    cout <<"Memory allocated to class: " << sizeof(raman) << endl;
    return 0 ;
}


Query :
How is the memory allocated to the given class ?
Is it size of all the data members in the given class ie sizeof(name) + sizeof(age) + sizeof(salary) + sizeof(yes)?
If yes , then
sizeof(int) = 4
sizeof(char) = 1
sizeof(float) = 4
sizeof(string) = 4

So Shouldn't it be 13 but it gives 16 ? why ?

The compiler is free to add padding bytes to a structure for better alignment.
Does it mean that it completely depends on the compiler to decide any number of padding bytes and allocate the same to the class ?
Last edited on
closed account (zb0S216C)
A class template (not to be confused with template class template) defines the amount, type, and positioning of the members. The class template doesn't allocate memory, until it's instantiated. As Athar said, the compiler is free to insert padding bytes wherever it wants. A decent compiler will add bytes where it makes sense. Adding bytes is called Alignment, and its purpose is to align all the members on word-sized boundaries so that the CPU can cache the members more efficiently. A compiler will only insert bytes only if the class template isn't perfectly aligned (the size is not a power of 2).

Passing the class template to sizeof will yield the size of each member, including the additional bytes.
When you instance a class template, the object can go in 1 of 2 places: heap, or stack. By default, it'll be placed on the stack with automatic storage (automatic destructor call).

Raman009 wrote:
"Does it mean that it completely depends on the compiler to decide any number of padding bytes and allocate the same to the class ?"

Yes, it's compiler specific. However, compilers will align data members according to the architecture of the host CPU.

Wazzak
Last edited on
Hum... a class template is not what you are talking about. It is a class with one or more template parameters. A class without templates parameters is simply a class and the instantiation of a class is an object. And the instantiation of a class template is a class.

And an aligned structure doesn't have to have a size of a power of two but a multiple of the processor size(4, 8, ... bytes).
closed account (zb0S216C)
aquaz wrote:
"a class template is not what you are talking about. It is a class with one or more template parameters. "

No, you're thinking of a template class template. A class is a template in itself.

aquaz wrote:
"And an aligned structure doesn't have to have a size of a power of two but a multiple of the processor size"

...which are powers of 2. Computers follow the base 2 (binary) numerical system.

Wazzak
wow..thanks
I never saw the expression template class template. I don't know in anothers languages but in C++ if you talk about class templates it is definitely a class with templates parameters. Google it and see.

A data structure of size 24 on a processor of size 4 is aligned since 24 = 6 * 4 and 24 is not a power of two.
Topic archived. No new replies allowed.