Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Invoking a function before execution of main()

Reply
Thread Tools

Invoking a function before execution of main()

 
 
Manuel
Guest
Posts: n/a
 
      07-18-2003
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
 
Reply With Quote
 
 
 
 
Christian W.
Guest
Posts: n/a
 
      07-18-2003
> 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++.


 
Reply With Quote
 
 
 
 
John Isaacks
Guest
Posts: n/a
 
      07-18-2003
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.




"Manuel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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




 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      07-18-2003
John Isaacks wrote:
> 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.
>
>
>
>
> "Manuel" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>
>>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


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

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      07-18-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Manuel) wrote in news:b753d529.0307180418.3c976985
@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.
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      07-18-2003
"Victor Bazarov" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> "Andre Kostur" <(E-Mail Removed)> wrote...
>> (E-Mail Removed) (Manuel) wrote in
>> news:b753d529.0307180418.3c976985 @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.

>
> 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).
 
Reply With Quote
 
John Isaacks
Guest
Posts: n/a
 
      07-19-2003
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.







 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking c function in a c plus plus function... Rahul C++ 9 03-25-2008 05:24 PM
Invoking instance before main and memory allocation for static member Alex Vinokur C++ 1 07-24-2003 12:46 PM
Re: Invoking fuction before main Chris Torek C Programming 5 07-23-2003 01:08 AM
Re: Invoking fuction before main Dan Pop C Programming 8 07-21-2003 03:28 PM
Re: Invoking fuction before main Joona I Palaste C Programming 5 07-21-2003 12:37 PM



Advertisments