when is the constructor of a global object called??

This is a discussion on when is the constructor of a global object called?? within the C++ Programming forums, part of the General Programming Boards category; sorry for my ignorance when is the constructor of a global object called?? at startup of the program? and if ...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    when is the constructor of a global object called??

    sorry for my ignorance
    when is the constructor of a global object called??
    at startup of the program?
    and if there are more threads it is called by the first thread ?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    It's called sometime before main(), I think.

    I don't see how you could get it to run in a different thread.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is called just before main() starts - in the same thread that will start main [at least that is the case in all OS's I'm familiar with].

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

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    More precisely, it will be called before the user code of main() is executed. On some implementations, if you examine the stack trace of these initialization, main() may actually be in there, because the standard is written such that it is allowed for an implementation to insert the initialization code at the start of main().

    (Specifically, that's why the standard disallows calling main() - on such an implementation, the initialization would be repeated.)
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    More precisely, it will be called before the user code of main() is executed. On some implementations, if you examine the stack trace of these initialization, main() may actually be in there, because the standard is written such that it is allowed for an implementation to insert the initialization code at the start of main().

    (Specifically, that's why the standard disallows calling main() - on such an implementation, the initialization would be repeated.)
    Sure, it's not guaranteed to be OUTSIDE of main - and in fact, I think gcc adds __main() for this particular purpose in main.

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

  6. #6
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    thank you!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Oh and a note of warning.
    The order of which constructors are called for global objects is not defined, so they can be called in any order. Beware.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 05:26 AM
  2. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  3. Global object
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2006, 06:38 AM
  4. Finding object code
    By kidburla in forum C Programming
    Replies: 3
    Last Post: 11-29-2005, 01:09 PM
  5. Help for the c error
    By robert_sun in forum C Programming
    Replies: 4
    Last Post: 05-13-2004, 02:38 PM

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