answersLogoWhite

0

An object is simply an instance of a class, thus the sizeof() operator will always return the same size regardless of whether the argument is a class or an instance of the class.

But it's not always quite so clear cut. The sizeof() operator does not include the size of static member variables nor does it include any memory that is dynamically allocated and owned by the class. Thus in terms of actual memory consumption, the sizeof() operator can be misleading. The only way to determine the actual consumption is to calculate it yourself.

Most of the time the sizeof() operator is sufficient. The nature of object oriented programming is such that individual objects take care of their own memory allocations and clean up behind themselves. The only time we're really concerned with those allocations is when we constantly run out memory, which is usually an indication of poor design. In that event, it can be useful to determine just how much additional memory is actually being consumed by an object.

The size of a class or object is determined by the sum of its non-static member variables, plus any required padding (used for memory alignment), plus its virtual table if it has one (applies to derived classes only). This is the value reported by sizeof(). You must then add on the size of any allocations owned by the class or object. Note that this may result in several objects of the same class having completely different sizes, depending on the amount of memory allocated to each of them.

If your class also has static member variables, the size of those members may need to be taken into account as well, especially if dynamic memory is allocated to them. Static member variables exist whether an object is instantiated from the class or not, but there is only one instance of each static member, regardless of the number of objects instantiated.

In order to make best use of available memory, always declare your class members in descending order of size as this will greatly reduce the amount of padding required. By way of example, consider the example code below. Two classes are declared with the exact same members with a total size of 10 bytes. Class x is a poorly-designed class because char x::a (which is only 1 byte in size) is the first variable declared and therefore must occupy the starting address of the class. int x::b is 4 bytes long so it won't fit in the remaining three bytes, so it is forced onto the next 32-bit boundary, thus wasting three bytes. This is then followed by char x::c which forces int x::d onto yet another 32-bit boundary, wasting another three bytes. Thus 6 bytes are wasted in total.

Class y is much more efficient as the two largest members (int y::b and int y::d) are declared first and thus occupy the first 8 bytes with no padding. char y::a then occupies the first byte of the next 32-bit boundary and is immediately followed by char y::c in the next byte. The remaining two bytes cannot be used for any other purpose so the class is padded with those two bytes, thus rounding the class size to the nearest multiple of 4 bytes.

Note that although object X is instantiated before object Y, object Y precedes object X in memory. This is simply a result of memory being allocated to the classes from the top down. Once the class is allocated, the members are assigned addresses from the bottom of the allocation up.

#include <iostream>

class x

{

public:

char a;

int b;

char c;

int d;

};

class y

{

public:

int b;

int d;

char a;

char c;

};

int main()

{

x X;

y Y;

unsigned int Address_X = ( unsigned int ) &X;

unsigned int Address_Xa = ( unsigned int ) &X.a;

unsigned int Address_Xb = ( unsigned int ) &X.b;

unsigned int Address_Xc = ( unsigned int ) &X.c;

unsigned int Address_Xd = ( unsigned int ) &X.d;

unsigned int Size_X = ( unsigned int ) sizeof(X);

unsigned int Size_Xa = ( unsigned int ) sizeof(X.a);

unsigned int Size_Xb = ( unsigned int ) sizeof(X.b);

unsigned int Size_Xc = ( unsigned int ) sizeof(X.c);

unsigned int Size_Xd = ( unsigned int ) sizeof(X.d);

unsigned int Address_Y = ( unsigned int ) &Y;

unsigned int Address_Ya = ( unsigned int ) &Y.a;

unsigned int Address_Yb = ( unsigned int ) &Y.b;

unsigned int Address_Yc = ( unsigned int ) &Y.c;

unsigned int Address_Yd = ( unsigned int ) &Y.d;

unsigned int Size_Y = ( unsigned int ) sizeof(Y);

unsigned int Size_Ya = ( unsigned int ) sizeof(Y.a);

unsigned int Size_Yb = ( unsigned int ) sizeof(Y.b);

unsigned int Size_Yc = ( unsigned int ) sizeof(Y.c);

unsigned int Size_Yd = ( unsigned int ) sizeof(Y.d);

printf("\nVar\tAddress\t\tSize\tPadding\n" );

printf("X\t0x%.8x\t%u\t%u\n", Address_X, Size_X, Size_X-(Size_Xa + Size_Xb + Size_Xc + Size_Xd));

printf("X.a\t0x%.8x\t%u\t%u\n", Address_Xa, Size_Xa, (Address_Xb - Address_Xa) - Size_Xa);

printf("X.b\t0x%.8x\t%u\t%u\n", Address_Xb, Size_Xb, (Address_Xc - Address_Xb) - Size_Xb);

printf("X.c\t0x%.8x\t%u\t%u\n", Address_Xc, Size_Xc, (Address_Xd - Address_Xc) - Size_Xc);

printf("X.d\t0x%.8x\t%u\t%u\n", Address_Xd, Size_Xd, ((Address_X + Size_X) - Address_Xd) - Size_Xd);

printf("\nVar\tAddress\t\tSize\tPadding\n" );

printf("Y\t0x%.8x\t%u\t%u\n", Address_Y, Size_Y, Size_Y - (Size_Ya + Size_Yb + Size_Yc + Size_Yd));

printf("Y.b\t0x%.8x\t%u\t%u\n", Address_Yb, Size_Yb, (Address_Yd - Address_Yb) - Size_Yb);

printf("Y.d\t0x%.8x\t%u\t%u\n", Address_Yd, Size_Yd, (Address_Ya - Address_Yd) - Size_Yd);

printf("Y.a\t0x%.8x\t%u\t%u\n", Address_Ya, Size_Ya, (Address_Yc - Address_Ya) - Size_Ya);

printf("Y.c\t0x%.8x\t%u\t%u\n", Address_Yc, Size_Yc, ((Address_Y + Size_Y) - Address_Yc) - Size_Yc);

return(0);

}

Example Output:

VarAddressSizePaddingX0x0018fd7c166X.a0x0018fd7c13X.b0x0018fd8040X.c0x0018fd8413X.d0x0018fd8840

VarAddressSizePaddingY0x0018fd68122Y.b0x0018fd6840Y.d0x0018fd6c40Y.a0x0018fd7010Y.c0x0018fd7112

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Will density always stay the same no matter what size the object is?

Density of the substance will always stay the same. Density of the object will also stay the same if solid, no matter the size, but not if it is carved out. That is why a steel boat can float


Is the mass of an object always the same regardless of the size?

No, the mass of an object can change depending on factors such as the addition or removal of material. However, the mass is an intrinsic property of an object and does not change with its size or volume.


Where does the image of a concave lens always appear?

The image formed by a concave lens is always virtual, upright, and located on the same side as the object. It is also diminished in size compared to the object.


Will the density of a material always be the same regardless of its size if so why?

No, the density of a material is not always the same regardless of its size. Density is a property that is determined by dividing mass by volume, so as the size of an object changes, its volume also changes which can affect its density.


What mirror always forms virtual image of the same size on the object?

A concave mirror with its object placed between the focal point and the mirror forms a virtual image of the same size. This image is upright and located behind the mirror.


Do two objects that are the same size always have the same inertia?

Not necessarily. Inertia depends on both an object's mass and how that mass is distributed. Two objects of the same size but with different densities or internal structures can have different inertias.


What has the same shape but not the same size?

A SIMILAR object.


How can I measure the size of an object from an image?

One way to measure the size of an object from an image is to use a known reference object of a standard size in the same image. By comparing the size of the reference object to the size of the object you want to measure, you can calculate the size of the object in the image.


What is the size of an object of empty class?

That would depend on the programming language; even for the same language, it may vary for specific implementations. Usually such an object should just use a few bytes.


Which position of the object will produce an image of the same size as the object?

In case of reflection at a concave mirror as the object is placed at its center of curvature we get the same size And in case of lens, as object is placed at 2F, we get same sized image


Does the size of the object have an effect on the amount of matter an object has?

No, the size of an object does not affect the amount of matter it has. The amount of matter in an object is determined by its mass, which remains the same regardless of its size.


What is an object the same size as a decimeter?

a hand