473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Invoking a function before execution of main()

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
Jul 19 '05 #1
6 4594
> 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++.
Jul 19 '05 #2
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" <ma*******@hotmail.com> wrote in message
news:b7**************************@posting.google.c 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


Jul 19 '05 #3
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" <ma*******@hotmail.com> wrote in message
news:b7**************************@posting.google.c 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

Jul 19 '05 #4
ma*******@hotmail.com (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.
Jul 19 '05 #5
"Victor Bazarov" <v.********@attAbi.com> wrote in
news:vh************@corp.supernews.com:
"Andre Kostur" <nn******@kostur.net> wrote...
ma*******@hotmail.com (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).
Jul 19 '05 #6
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.



Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Elven | last post by:
Hi! I've been trying to figure this out, but can't seem to find an answer. Most likely it's some weird bug in my code, but since I don't have access to the Standard, I wanted to cover my bases.....
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
25
by: MuZZy | last post by:
Hi, I'm currently rewriting some functionality which was using multithredaing for retrieving datasets from database and updating a grid control. I found that the grids (Infragistics UltraGrid,...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
0
by: Jon | last post by:
Hi, "THREAD_UI" = thread under which the UI controls were created. "THREAD_XX" = another thread. "FUNC_UI" = function that needs to run on THREAD_UI. From THREAD_XX I need to start the...
2
AmberJain
by: AmberJain | last post by:
HELLO, Wikipedia defines return statement as: Now there's a C program I have coded---->
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.