Sizeof and C++ Object Memory allocation

sureshsundar007's Avatar, Join Date: Oct 2006
Newbie Member
Hi,

I Wonder whether the 'sizeof ' operator works at compile time or run time.

I understand the C++ objet model stores the non-static data members as part of the object.When a Object is created its size is determined by using the sizeof operator.

For ex:

Class1 *obj = new Class1;

is approximately equal to (excluding constructor calling)

Class1 *obj = (Class1)malloc(sizeof(Class1));

The memory is allocated comfortably.Fine.

I also read that when there is a addition of a member in the class (Class1) the client code which instantiates the object (previous code piece for ex) must be recompiled.If the sizeof operator finds the size of Class1 at run-time it could get the correct modified size of the Class.Why it is not implemented like that?

Thanks,
Suresh.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I could not get what you are looking for. Runtime does not mean if you are in the middle of a debug and edit something to add more member variable it will calculate accordingly without compiling.
0
sureshsundar007's Avatar, Join Date: Oct 2006
Newbie Member
I dont mean a debug.My question is related to the failure of C++ wrt Component based software.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
So you mean I edit the component and without building that should be incorporated.
0
sureshsundar007's Avatar, Join Date: Oct 2006
Newbie Member
Yes.If we dont recompile the Clients, they will break (because of its static size allocation done at compile time).This could have avoided by dynamically finding the size of the type at run time.

Thanks,
Suresh.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
If it would have been runtime then also it would have failed because you need to compile the correct implemented code for anything runtime to take place even.

Can you show the example of where such an implementation is working for runtime.
0
sureshsundar007's Avatar, Join Date: Oct 2006
Newbie Member
Yes,We must compile the Components whenever we modify its members.No problems in that.The problem is with the Clients which uses these Components, also needs to be recompiled for correct working.

Thanks,
Suresh.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I dont think thats true unless and until you dont have a class in the .h file which is same for the server as well as client. If thats the case then memory will also be a problem as that will be overlapping for some members in server