Executing functions before main.

I just read here: http://www.cplusplus.com/forum/beginner/1988/7/#msg26845

that its not a good idea. I never knew that before. Can anyone explain whY?
One reason is that the order of initialization is not defined. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

extern int a,b;
//EDIT:
int f(const char *,int);
//~EDIT

int a=f("a",b);
int b=f("b",a);

int f(const char *s,int i){
    std::cout <<s<<": "<<i<<std::endl;
    return 0;
}

int main(){}
If you try running that, you'll see that the output is either
a: <garbage>
b: 0
or
b: <garbage>
a: 0
But it's not possible to know ahead of time which one.
In fact, just using std::cout in that function is unsafe.
Last edited on
Just out of curiosity, is there any logic as to why so much is left undefined in c++?
Many reasons. One of my favourites is the principle that you shouldn't have to pay for what you don't use.
Also:
* Simpler implementations.
* More optimization opportunities.
* More portability.
@Moschops: Another reason: What I don't pay for shouldn't be of top quality.
@helios: I tried compiling that code and I get:
5: error ´f´ was not declared in this scope
6: error 'f' was not declared in this scope.

but I added:
 
extern int f(const char * s, int i);

to above the assignment of a and b, and i get output:

a: 0
b: 0

Last edited on
put a prototype for f above the calls.
i get output:
a: 0
b: 0
And your point?
now I tried putting the function implementation of f to after main, and the assignments of a & b to inside main but I still get error: undefined reference to _a

but I cannot recreate the error as described by helios.
I never said you'd get an error. Undefined behavior is not an error. It's undefined behavior; anything can happen.
My point is, at least as long as I keep using codeblocks and mingw I'm not going to bother editing my functions called b4 main because my compiler at least seems to handle those situations nicely.
Try this instead:

1
2
3
4
5
6
7
int a=f("a",b);
int b=f("b",a);

int f(const char *s,int i){
    std::cout <<s<<": "<<i<<std::endl;
    return 10;
}


Now "correct" output will be 10, 10 instead of 0, 0

But you won't get 10, 10


I'm not going to bother editing my functions called b4 main because my compiler at least seems to handle those situations nicely


Sure you say that now.

But 6 months from now you'll suddenly find that all of your programs that used to be working fine start breaking and you have no idea why.

Or you try to give your code to someone else and have them compile it to help you debug or test something and they're unable to.


Relying on undefined behavior to behave a certain way is bad. Don't do it. You'll regret it later, even if it seems it's fine now.
Last edited on
Suit yourself. I'm not the one who'll have to debug your programs.
well all i'm saying is undefined behaviour is undefined behaviour for c++ ingeneral, what is undefined behaviour for c++ in general might be defined for my particular compiler.
You can't know that by just observing its behavior under particular circumstances. You'd have to look at the manual.
By the way, GCC is known to have no qualms about breaking semantics between versions when they're supposed to be undefined anyway. Have fun.

Have fun.

LOL. I'll try.
Topic archived. No new replies allowed.