main is not the first function

This is a discussion on main is not the first function within the C++ Programming forums, part of the General Programming Boards category; Say I create a global object of a class. The constructor of this class does every thing. Initializing. Performing operations. ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    main is not the first function - bad idea!

    Say I create a global object of a class. The constructor of this class does every thing.
    Initializing. Performing operations. Everything needed. And then exit(0)s.

    Is it okay? Recommended? or bad? very bad? or hardly matters!

    For example:
    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    class foo
    {
    public:
    	foo()
    	{
    		cout << "Your name please: ";
    		string name;
    		getline(cin, name);
    	
    		time_t now;
    		time(&now);
    		tm* tm = localtime(&now);
    		string salute = "";
    		if (tm->tm_hour < 12)
    		{
    			salute = "Morning";
    		}
    		else if (tm->tm_hour < 15)
    		{
    			salute = "Afternoon";
    		}
    		else
    		{
    			salute = "Evening";
    		}
    
    		cout << endl << "Good " << salute << " " << name << " !" << endl;
    		exit(0);
    	}
    };
    
    foo foo;
    
    int main()
    {
    	cout << "Eeek killed before entering me!" << endl;
    	return 0;
    }
    Last edited by manav; 12-05-2007 at 10:55 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Why do you want to do this, and what's the point of doing this?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    VERY VERY BAD!

    Why do you want to do things that way? main() is where your program starts, everything before that should be "irrellevant".

    Global objects (or global variables) in themselves is not a "good" thing.

    If you have global objects, the constructor should be kept to a minimum.

    Note also that there's no guarantee about the order of execution of constructors if you create a second global object - so:
    Code:
    class A { A() { }; ... } ;
    class B { B() {}; ... };
    
    A a;
    B b;
    Logically, a.A() will be called first, but I beleive there is no guarantee that this is the case.

    More importantly, if you put your
    Code:
    B b;
    in a different source file, it's absolutely uncertain which constructor gets called first - for example, it may depend on the order that the object files are listed to the linker, or alphabetical order of filenames, or some other random order.

    --
    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
    Banned
    Join Date
    Nov 2007
    Posts
    678
    I remember my old C days. To figure out a program I would start from the main.
    It was a definite start point. Everything would follow thereafter.
    But in C++ it is not required at all. And if there are global objects here or there, then you may
    not be starting from the start. If you start at main?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Don't put any code other than initialization (and keep that code to the specific object only!) in global object constructors.
    Start from main and work your way from there.
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    I remember my old C days. To figure out a program I would start from the main.
    It was a definite start point. Everything would follow thereafter.
    But in C++ it is not required at all. And if there are global objects here or there, then you may
    not be starting from the start. If you start at main?
    Yes, of course, there is, as you have shown, a technical possibility to create something where large amounts of code is executed before the first statement in main() [1]. There is of course, if you like to do things that way, also the possibility of re-writing the C runtime startup code so that it doesn't call main, but some other function first.

    [1] At least in gcc, the construction of global objects is actually done as part of main - but it's done BEFORE the code you have supplied in main happens.

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

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by manav View Post
    Say I create a global object of a class. The constructor of this class does every thing.
    Initializing. Performing operations. Everything needed. And then exit(0)s.
    Interesting. I'm not sure if a call to exit() prior to invoking main() is a defined operation or not. I would expect that the standard library is already fully initialized by the time that global constructors begin running, but I'm not sure what the standard says about calling exit().

    Is it okay? Recommended? or bad? very bad? or hardly matters!
    Global objects on the whole are "not recommended" but in some cases are unavoidable. I would suggest NOT performing complex operations in the constructors of global objects, though.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    Interesting. I'm not sure if a call to exit() prior to invoking main() is a defined operation or not. I would expect that the standard library is already fully initialized by the time that global constructors begin running, but I'm not sure what the standard says about calling exit().
    The only implementation I've looked into is g++'s constructor calls, and those are, for global objects, made by "main" itself, inserted between the entry of main itself and the "first code in main" [by calling ___main from _main in assembler].

    I tried to prove this, but I wasn't able to call printf() from my constructor, so I couldn't get the dump of the stack working at all... Will look again tomorrow, perhaps.

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

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Honestly, if you're doing globally accessible objects, I'd recommend you use the Singleton pattern and lazy instantiation. Then the constructor doesn't happen before main() at all.

    Of course, this assumes there's a good reason to have this object globally accessible in the first place. Sometimes this makes a lot of sense, and that's what Singleton is for, but often it's a poor hack to make poorly designed code work.

    Oh and if you're doing globally accessible objects but DON'T want the uniqueness imposed by Singleton (that is, you want multiple instances of the class, with one instance global) this is a very strong sign you're writing bad code.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by matsp View Post
    Note also that there's no guarantee about the order of execution of constructors if you create a second global object - so:
    Dynamic initializations in any given source file are executed in the order they appear. That's guaranteed. It's only inter-module order that isn't specified.

    Bad enough, though.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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