1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Sizeof and C++ Object Memory allocation

Discussion in 'C++' started by sureshsundar007, Oct 23, 2006.

  1. sureshsundar007

    sureshsundar007 New Member

    Joined:
    Oct 23, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  3. sureshsundar007

    sureshsundar007 New Member

    Joined:
    Oct 23, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I dont mean a debug.My question is related to the failure of C++ wrt Component based software.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    So you mean I edit the component and without building that should be incorporated.
     
  5. sureshsundar007

    sureshsundar007 New Member

    Joined:
    Oct 23, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  7. sureshsundar007

    sureshsundar007 New Member

    Joined:
    Oct 23, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    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
     

Share This Page