Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > execute a different function before main()

Reply
Thread Tools

execute a different function before main()

 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      03-11-2008
Hi all,
recently i read the following passage in a book

it is a compiler dependent thing if some feature is available that
allows you to execute some function before main() is called.
In turbo C compiler, there's one such fearture....the #pragma startup
directive.
I m giving an example of its use:-

#include<stdio.h>
void fun();
#pragma startup fun
int main()
{
printf("Arun");
}
void fun()
{
printf("Anand \n");
}

The o/p of the program given above will be :- Anand

my question is there a standard way in ANSI-C
to execute a different function before main() is called....??????

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      03-11-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> my question is there a standard way in ANSI-C
> to execute a different function before main() is called....??????


No. But why would you? Call that function as the first thing you do in
main() itself. The only reason I can think of why that wouldn't work is
when you call main() recursively, and
a) that's a very bad idea in the first place;
b) the trivial workaround is left as an exercise for the reader.

Richard
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-11-2008
(E-Mail Removed) wrote:
> Hi all,
> recently i read the following passage in a book
>
> it is a compiler dependent thing if some feature is available that
> allows you to execute some function before main() is called.
> In turbo C compiler, there's one such fearture....the #pragma startup
> directive.
> I m giving an example of its use:-
>
> #include<stdio.h>
> void fun();
> #pragma startup fun
> int main()
> {
> printf("Arun");
> }
> void fun()
> {
> printf("Anand \n");
> }
>
> The o/p of the program given above will be :- Anand
>
> my question is there a standard way in ANSI-C
> to execute a different function before main() is called....??????


Yes. Two ways, in fact:

1) Run your program in a freestanding environment, where
"the name and type of the function called at program startup
are implementation-defined."

2) Use an editor to change the name of your existing
main() to something else, and to change the name of your
desired startup function to main().

(Yes, this is somewhat tongue in cheek. But there's a
serious point behind it: If you can modify the program source,
you are in control of what main() does. You can therefore
make it do whatever you wanted this other, magical function
to do -- so you don't need any magic.)

--
(E-Mail Removed)
 
Reply With Quote
 
Morris Dovey
Guest
Posts: n/a
 
      03-11-2008
(E-Mail Removed) wrote:

> is it possible to modify the start up code in a hosted environment
> so as to change the name and type of the function called at program
> startup..???


Perhaps, but your co-workers may not be happy...

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
 
Reply With Quote
 
aarklon@gmail.com
Guest
Posts: n/a
 
      03-11-2008
On Mar 11, 9:34 pm, Eric Sosman <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Hi all,
> > recently i read the following passage in a book

>
> > it is a compiler dependent thing if some feature is available that
> > allows you to execute some function before main() is called.
> > In turbo C compiler, there's one such fearture....the #pragma startup
> > directive.
> > I m giving an example of its use:-

>
> > #include<stdio.h>
> > void fun();
> > #pragma startup fun
> > int main()
> > {
> > printf("Arun");
> > }
> > void fun()
> > {
> > printf("Anand \n");
> > }

>
> > The o/p of the program given above will be :- Anand

>
> > my question is there a standard way in ANSI-C
> > to execute a different function before main() is called....??????

>
> Yes. Two ways, in fact:
>
>1) Run your program in a freestanding environment, where
> "the name and type of the function called at program startup
> are implementation-defined."


is it possible to modify the start up code in a hosted environment
so as to change the name and type of the function called at program
startup..???





 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-11-2008
(E-Mail Removed) wrote:

> On Mar 11, 9:34 pm, Eric Sosman <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > Hi all,
>> > recently i read the following passage in a book

>>
>> > it is a compiler dependent thing if some feature is available that
>> > allows you to execute some function before main() is called.
>> > In turbo C compiler, there's one such fearture....the #pragma
>> > startup directive.
>> > I m giving an example of its use:-

>>
>> > #include<stdio.h>
>> > void fun();
>> > #pragma startup fun
>> > int main()
>> > {
>> > printf("Arun");
>> > }
>> > void fun()
>> > {
>> > printf("Anand \n");
>> > }

>>
>> > The o/p of the program given above will be :- Anand

>>
>> > my question is there a standard way in ANSI-C
>> > to execute a different function before main() is called....??????

>>
>> Yes. Two ways, in fact:
>>
>>1) Run your program in a freestanding environment, where
>> "the name and type of the function called at program startup
>> are implementation-defined."

>
> is it possible to modify the start up code in a hosted environment
> so as to change the name and type of the function called at program
> startup..???


If you have the source, yes. But you'll need to recompile and reinstall
the library. Just what is your actual problem that apparently seems to
be forcing you to massive hacks?

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-11-2008
In article <(E-Mail Removed)>,
<(E-Mail Removed)> wrote:

> is it possible to modify the start up code in a hosted environment
> so as to change the name and type of the function called at program
>startup..???


It depends on the environment.

[OT]

For example on SGI IRIX, there is the following linker option:

-init ifuncsymbol
Puts a call to ifuncsymbol into the code that rld(5)
exercises before the main program begins. The ifuncsymbol
contains initialization code to be called before control is
passed to main. In the case of a DSO opened with dlopen(3C)
or sgidladd(3C) an initialization function is called after
the DSO is loaded but before it is referenced.

--
"No sincere artist was ever completely satisfied with his labour."
-- Walter J. Phillips
 
Reply With Quote
 
George Peter Staplin
Guest
Posts: n/a
 
      03-12-2008
(E-Mail Removed) wrote:
> On Mar 11, 9:34 pm, Eric Sosman <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > Hi all,
>> > recently i read the following passage in a book

>>
>> > it is a compiler dependent thing if some feature is available that
>> > allows you to execute some function before main() is called.
>> > In turbo C compiler, there's one such fearture....the #pragma startup
>> > directive.
>> > I m giving an example of its use:-

>>
>> > #include<stdio.h>
>> > void fun();
>> > #pragma startup fun
>> > int main()
>> > {
>> > printf("Arun");
>> > }
>> > void fun()
>> > {
>> > printf("Anand \n");
>> > }

>>
>> > The o/p of the program given above will be :- Anand

>>
>> > my question is there a standard way in ANSI-C
>> > to execute a different function before main() is called....??????

>>
>> Yes. Two ways, in fact:
>>
>>1) Run your program in a freestanding environment, where
>> "the name and type of the function called at program startup
>> are implementation-defined."

>
> is it possible to modify the start up code in a hosted environment
> so as to change the name and type of the function called at program
> startup..???
>


Yes, it's possible with a hosted environment but not from standard C.

When a program is loaded with most (Windows and Unix-like) systems, the
OS jumps to an offset for _start in assembly code.

If you're using gcc in Windows or a POSIX system you can see what
startup files gcc links with via: gcc -v somefile.c

The startup files are typically named like crt0.o. They are implemented
in assembly code usually and the various crt*.o do tasks like
implementing exit(main(argc,argv)); and setting up the environment,


George
 
Reply With Quote
 
aarklon@gmail.com
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 7:28 am, George Peter Staplin
<(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Mar 11, 9:34 pm, Eric Sosman <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >> > Hi all,
> >> > recently i read the following passage in a book

>
> >> > it is a compiler dependent thing if some feature is available that
> >> > allows you to execute some function before main() is called.
> >> > In turbo C compiler, there's one such fearture....the #pragma startup
> >> > directive.
> >> > I m giving an example of its use:-

>
> >> > #include<stdio.h>
> >> > void fun();
> >> > #pragma startup fun
> >> > int main()
> >> > {
> >> > printf("Arun");
> >> > }
> >> > void fun()
> >> > {
> >> > printf("Anand \n");
> >> > }

>
> >> > The o/p of the program given above will be :- Anand

>
> >> > my question is there a standard way in ANSI-C
> >> > to execute a different function before main() is called....??????

>
> >> Yes. Two ways, in fact:

>
> >>1) Run your program in a freestanding environment, where
> >> "the name and type of the function called at program startup
> >> are implementation-defined."

>
> > is it possible to modify the start up code in a hosted environment
> > so as to change the name and type of the function called at program
> > startup..???

>
> Yes, it's possible with a hosted environment but not from standard C.
>
> When a program is loaded with most (Windows and Unix-like) systems, the
> OS jumps to an offset for _start in assembly code.
>
> If you're using gcc in Windows or a POSIX system you can see what
> startup files gcc links with via: gcc -v somefile.c
>
> The startup files are typically named like crt0.o. They are implemented
> in assembly code usually and the various crt*.o do tasks like
> implementing exit(main(argc,argv)); and setting up the environment,


Can any one the general outline of the life cycle of the execution a
C program, in similar lines to the "LInux boot sequence"
given here
http://www.debianhelp.co.uk/boot.htm

 
Reply With Quote
 
aarklon@gmail.com
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 11:34 am, WANG Cong <(E-Mail Removed)> wrote:
> On Tue, 11 Mar 2008 18:52:57 +0000,Walter Roberson wrote:
>
> > In article
> > <(E-Mail Removed)>,
> > <(E-Mail Removed)> wrote:

>
> >> is it possible to modify the start up code in a hosted environment so
> >> as to change the name and type of the function called at program
> >>startup..???

>
> > It depends on the environment.

>
> Sure. In gcc you can use __attribute__((constructor)).


Can any one explain the general outline, of the execution a
C program, in similar lines to the "LInux boot sequence"
given here
http://www.debianhelp.co.uk/boot.htm
 
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
execute a function before and after any method of a parent class TP Python 6 10-07-2008 09:44 PM
Can I execute a javascript action before uploading a file? =?Utf-8?B?TWFyayBNaWxsZXI=?= ASP .Net 4 08-23-2005 02:35 AM
Can i execute aspx file in my asp pag with server.execute method(sorry) Savas Ates ASP General 1 08-17-2004 04:52 PM
Javascript function works but C# function doesn't execute Jim Hammond ASP .Net 2 11-14-2003 08:43 PM
generator function within a generator function doesn't execute? TheDustbustr Python 1 07-25-2003 10:45 AM



Advertisments