constructor called on statically allocated arrays?

This is a discussion on constructor called on statically allocated arrays? within the C++ Programming forums, part of the General Programming Boards category; Just a quick question. In this example: Code: int main() { MyClass a[10]; } is the constructor of MyClass called ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,146

    constructor called on statically allocated arrays?

    Just a quick question. In this example:
    Code:
    int main() {
         MyClass a[10];
    }
    is the constructor of MyClass called 10 times (assuming it has a default constructor)? or only the memory is allocated?

    How about something like this:
    Code:
    int main() {
         std::vector<MyClass> v(10);
    }
    If the constructors are called, wouldn't that make the allocation of an array and construction of the vector O(n)? (assuming the constructor itself is O(1) ).

    Thank you

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Both call the default constructor 10 times.

    Yes, creation and initialization of the array would be O(n).

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,146
    I see. Thank you.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Quote Originally Posted by cyberfish View Post
    Just a quick question. In this example:
    is the constructor of MyClass called 10 times (assuming it has a default constructor)? or only the memory is allocated?
    As part of simple experimentation with the language you could have easily found that out by outputting something in the default constructor:
    Code:
    class MyClass
    {
    public:
        MyClass()
        {
            std::cout << "Constructor called." << std::endl;
        }
    };
    You then would have seen something like the following when you ran the program:
    Code:
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by hk_mp5kpdw View Post
    You then would have seen something like the following when you ran the program:
    Code:
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    Constructor called.
    What this fails to reveal, however, is that an optimizing compiler WILL omit the constructor calls, if the object is POD and has a trivial constructor, or if the compiler can otherwise prove that the constructors don't need to be called.

    In other words, the very act of printing the message from the constructor forces the compiler to make sure it is called. If you have a vector of simple structs holding POD, you don't pay a O(n) price to construct the vector.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,146
    What this fails to reveal, however, is that an optimizing compiler WILL omit the constructor calls, if the object is POD and has a trivial constructor, or if the compiler can otherwise prove that the constructors don't need to be called.

    In other words, the very act of printing the message from the constructor forces the compiler to make sure it is called. If you have a vector of simple structs holding POD, you don't pay a O(n) price to construct the vector.
    Hmm.. that is interesting to know. My next question would be, is it possible to force the compiler to not call the constructor. (in pure C++, without using malloc and friends)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, by making sure that the constructor doesn't do anything, and making the constructor known to the compiler before the code that "uses" the constructor, you can (for most compilers, most of the time) make the constructor call go away.

    Of course, the compiler can't avoid calling the constructor if the constructor does something "useful". It may inline the constructor in this case, but it can not avoid using the constructor, as that would be "Breaking the object rules".

    --
    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.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you want to ensure there is enough space allocated but not actually create the objects, then use reserve:
    Code:
    int main() {
         std::vector<MyClass> v;
         v.reserve(10);
    }
    Then, when you are ready use push_back to add constructed objects.

    Is there a reason you are so concerned about this?

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by Daved View Post
    If you want to ensure there is enough space allocated but not actually create the objects, then use reserve:
    Code:
    int main() {
         std::vector<MyClass> v;
         v.reserve(10);
    }
    Then, when you are ready use push_back to add constructed objects.
    That must be using a placement new somewhere under the hood...

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,146
    Is there a reason you are so concerned about this?
    Not really. Just out of curiosity =).

    Thanks for the replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resize a Statically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:32 AM
  2. Arrays and pointers to allocated memory
    By TriKri in forum C Programming
    Replies: 19
    Last Post: 07-23-2006, 10:52 AM
  3. 2D Dynamically allocated pointer arrays
    By Lionmane in forum C Programming
    Replies: 37
    Last Post: 06-11-2005, 10:39 PM
  4. Dynamically allocated arrays
    By axe786 in forum C Programming
    Replies: 6
    Last Post: 12-03-2004, 12:41 AM
  5. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 10:26 PM

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