memory structure of object and structure

Hello,

i would like to know the memory structure of obj and structure could some please guide me.

for ex i have the below class and syructure::

class complex
{
private:

double real;
double imaginary;

public:

complex()
{
real=0.0;
imaginary=0.0;
}
void showComplex()
};

structure complexstruct
{
double real;
double imaginary;
};

when i check the size of complex object and complex C++ struct bothe show 16.I want to know where the functions of the class are stored.
since class is having functions why is size of structure and class object is same?

also when i created empty class and C++ structure as below:
class empty
{
public:
void inempty(){cout<<"empty class"<<endl;}
};

struct emptysrtuct
{
};


size of empty object(even when the function inempty removed from the class) and emptystruct is 1byte.why is the size 1byte for a empty class and struct what info is stored in it?

Thanks in advance!!!
Functions don't contribute to the size of the class.

It's just the member variables that do. It's also not uncommon for them to be padded, so a class containing and int and a char may take up eight bytes, rather than five. Note that static variables won't contribute to struct size either.

I have no idea about the empty struct.

Edit: There you go, that's why empty objects don't take up 0 space. It's forbidden. http://www.cantrip.org/emptyopt.html
Last edited on
Thanks iHutch!!!!


could you provide more info about padding.

Also i am having one more question:
in the above example if i have not defined constructor default implicit constructor will be called to allocate memory to object i.e

complex complex1;//this will call a implicit constr so that complex1 object gets the memory allocated(complex1.real and complex1.imaginary will have unknown values)

now if i try the same wit struct

complexstruct complexstruct1;//this will also allocate a memory to structure but with out calling any constructor.


How does object gets memory allocated with implicit constroctor and how does structure gets memory allocated without the help of constructor?

Thanks..
Padding/Memory alignment optimizes things a bit. It's quicker to read from word sized chunks than it is from something that isn't a multiple of a word.

Wikipedia has a pretty decent article on it: http://en.wikipedia.org/wiki/Data_structure_alignment

The struct above does have a constructor - just you haven't written it so it uses a default constructor and the variables will be uninitialised.

It is not the constructor's responsibility to allocate memory for the class. They're more for initialising values or allocating dynamic memory should any of your members require it.

I think by "implicit" you mean default. All constructors, by their very nature, are implicit in that they're called automatically upon the objects creation. If you override the default constructor (i.e. the one with no parameters) then your implementation is called when the object is created.

Similarly, if you implement a constructor that takes parameters, that will be called should you create the object with the appropriate arguments.

Don't be fooled into thinking there's a great deal of difference between classes and structs. In C++ they're exactly the same other than the access level - structs, by default, are public whereas classes are private.
Thanks again hutch it was really helpful.
Topic archived. No new replies allowed.