Invoking a function before execution of main()

M

Manuel

Can I invoke a function before main
I could do it by invoking it in a Global object's constructor .
Is there any other method other than this.

Manuel
 
C

Christian W.

Can I invoke a function before main
I could do it by invoking it in a Global object's constructor .
Is there any other method other than this.

Well, IMHO there is no other way to realize that in C++.
 
J

John Isaacks

If you think on it for a little while you should be able to eliminate the
need with a simple redesign.

What's the difference between
foo() begin called before main() and

main()
{
foo();
// rest of code here
}

global objects are in general not a good idea,
keep them to simple data types like int,long,char,etc.

The debug code on some machines (Microsoft Visual C++)
checks for memory leaks prior to destructing global objects
so objects like CString's will look like memory leaks
when they aren't.
 
T

Thomas Matthews

John said:
If you think on it for a little while you should be able to eliminate the
need with a simple redesign.

What's the difference between
foo() begin called before main() and

main()
{
foo();
// rest of code here
}

global objects are in general not a good idea,
keep them to simple data types like int,long,char,etc.

The debug code on some machines (Microsoft Visual C++)
checks for memory leaks prior to destructing global objects
so objects like CString's will look like memory leaks
when they aren't.

1. Don't top-post, replies are either interspersed or
appended at the bottom.

There is much difference between a function being called
before main() and after. There is a lot that goes on
before the main() function is executed. For example,
constructors of global objects get called before the
main() function; but their order is unspecified.

The common method to have code executed before the
main() function is to consult your compiler or linker
documentation and see what function(s) get called
before main(). Many compilers for embedded systems
have "hooks" for functions to be called before
main(). If you have a function you want called
before main(), it may have to be written in assembly
depending on when the C and C++ initialization
takes place.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
A

Andre Kostur

(e-mail address removed) (Manuel) wrote in @posting.google.com:
Can I invoke a function before main
I could do it by invoking it in a Global object's constructor .
Is there any other method other than this.

Individual implementations may have extensions to do it. However, from a
strictly C++ standpoint, all you can do is rely on a global object's
constructor. However one thing to keep in mine is what the order of global
object constructors is.... within a single translation unit (generally
speaking, 1 .cpp file plus everything it includes) global objects are
constructed in the order that they are declared. The twist is that
_between_ translation units, it is undefined as to what order the
translation units are constructed in. So if you have two translation
units, with two global objects each (say A1, A2, B1, and B2), the order of
construction could be either A1 - A2 - B1 - B2, or B1 - B2 - A1 - A2. It's
up to the compiler to make that decision.
 
A

Andre Kostur

You say it like all objects are constructed in one unit and
then all objects are constructed in the other unit. I cannot
find anything to substantiate that, can you? Therefore, it
seems that the orders A1-B1-B2-A2, B1-A1-B2-A2, etc., are just
as possible.

True... I suppose theoretically their constructors could be interleaved.
About all you can rely on is that A1 will be constructed before A2 (and
likewise for B1 and B2).
 
J

John Isaacks

All things have a beginning and that is the reason for main().

It's not good practice to have items as global because you can't
control the order in which the items are constructed.

If you remove your global items and have them created ( with the "new" )
from instead of main()
you can control the order of things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top