Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Sizeof and C++ Object Memory allocation (http://www.go4expert.com/forums/sizeof-cpp-object-memory-allocation-t1709/)

sureshsundar007 23Oct2006 19:22

Sizeof and C++ Object Memory allocation
 
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.

shabbir 25Oct2006 10:46

Re: Sizeof and C++ Object Memory allocation
 
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.

sureshsundar007 25Oct2006 20:10

Re: Sizeof and C++ Object Memory allocation
 
I dont mean a debug.My question is related to the failure of C++ wrt Component based software.

shabbir 25Oct2006 20:16

Re: Sizeof and C++ Object Memory allocation
 
So you mean I edit the component and without building that should be incorporated.

sureshsundar007 25Oct2006 21:18

Re: Sizeof and C++ Object Memory allocation
 
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.

shabbir 26Oct2006 10:17

Re: Sizeof and C++ Object Memory allocation
 
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.

sureshsundar007 26Oct2006 12:24

Re: Sizeof and C++ Object Memory allocation
 
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.

shabbir 26Oct2006 13:17

Re: Sizeof and C++ Object Memory allocation
 
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


All times are GMT +5.5. The time now is 09:12.