what's the hidden cost of creating object?

This is a discussion on what's the hidden cost of creating object? within the C++ Programming forums, part of the General Programming Boards category; I'm not yet an expert but I'm just wondering what is an extra cost of creating and using object during ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    71

    what's the hidden cost of creating object?

    I'm not yet an expert but I'm just wondering what is an extra cost of creating and using object during RUNTIME?

    for example what is the difference between

    int i;

    and

    class Foo {
    int i;
    };

    Let just assume that Foo is NOT created using new here.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would suppose there is zero hidden cost here. Of course, Foo could have a constructor that contains an infinite loop, so the cost would be infinite
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by anon View Post
    I would suppose there is zero hidden cost here. Of course, Foo could have a constructor that contains an infinite loop, so the cost would be infinite
    Thanks,that's what I'm wondering too but just need a confirmation.
    Hehehe can't praise this site enough.5 min of question posted here,I got my answer.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The difference about would be that the first defines an integer (cost: 4 bytes of space, minimal cost during application load) whereas the second defines a type (cost: 0 bytes of space, no cost during application load).

    I presume you meant to instantiate Foo, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    71

    Angry

    Quote Originally Posted by CornedBee View Post
    The difference about would be that the first defines an integer (cost: 4 bytes of space, minimal cost during application load) whereas the second defines a type (cost: 0 bytes of space, no cost during application load).

    I presume you meant to instantiate Foo, too.
    Foo foo;

    huh,Foo have integer also but when Foo object instantiated,Foo object is 0 byte???

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No - but the Foo type itself doesn't need any storage. Only objects of it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Ah yes,that made sense

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by CornedBee View Post
    No - but the Foo type itself doesn't need any storage. Only objects of it.
    True, but the constructor code will take space, even if its not created using new, there will be code that allocates the 4 bytes.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    Quote Originally Posted by abachler View Post
    True, but the constructor code will take space, even if its not created using new, there will be code that allocates the 4 bytes.
    Not so.

    Since the constructor is empty, the compiler does not need to generate a function for it.

    And there is no need to allocate 4 bytes. The same four byte of stack space, which is allocated before the main starts, is used in both cases. This is not Java.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just make a program to test out the things that make you curious? If you are asking about the general cost of instanciating a class then you could say the cost would be similar to the cost of initializing a set of variables. Of course, ctors can be inlined or not. So the only additional cost would present itself in the form of the cost of branching to another function. I would say that you could safely call the cost "minimal" without any arguments from anyone.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    I already use sizeof,so I know the answer.
    I just want certainty as not to be surprised later on.....
    beside posting in this forum,I got my got answer in 5 minutes

    Yeah I agree with your point that if you are curious,you should try out and see for yourselves...

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would also like to point out that the exact behaviour here depends on the compiler. One can make assumptions and be reasonably sure to be right, but compilers are written by people, and people do things differently if they work in different groups. So what one compiler does with the code may not be what another compiler does.

    That being said, I would say that there is little reason to believe that instantiation of an object that contains an integer, and "instantiation" of a basic integer would be different in any way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But couldn't a dumb compiler (or maybe one with no optimizations enabled) create & call an empty constructor?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    But couldn't a dumb compiler (or maybe one with no optimizations enabled) create & call an empty constructor?
    That's the purpose of my comment, yes - any decent compiler will realize that the constructor is comletely empty and thus not call it. But a really stupid compiler may not.

    And of course, with optimization turned off, it is likely that such optimizations are not done (if you say no to optimization, then it would be WRONG by the compiler to optimize your code - however, it doesn't mean that the compiler will necessarily PESSIMISE your code - so if it may not generate an empty function as a constructor and call it if the constructor isn't even declared).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an array of object w/no default constructor
    By Angus in forum C++ Programming
    Replies: 28
    Last Post: 05-06-2009, 05:10 PM
  2. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Replies: 3
    Last Post: 12-03-2001, 12:45 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21