473,408 Members | 2,813 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,408 software developers and data experts.

Calling atexit

Hi all,

The prototype for atexit is 'int atexit(void (*function)(void))';

If I have a function foo defined as
void foo(void)
{

}
then what is the correct way to register the handler -

atexit(&foo); // I think this one
OR
atexit(foo);
Any references to Standard would be appreciated.

Thanks,
Sharad

Nov 14 '05 #1
5 2030

"Kelvin Moss" <km**********@yahoo.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hi all,

The prototype for atexit is 'int atexit(void (*function)(void))';

If I have a function foo defined as
void foo(void)
{

}
then what is the correct way to register the handler -

atexit(&foo); // I think this one
OR
atexit(foo);


The latter. The name of a function evaluates to a pointer to that function.
Much like the name of an array evaluates to a pointer to (the first element
of) that array.

There is a *terribly* good FAQ around.

http://www.eskimo.com/~scs/C-faq/top.html

Eventhough it's not "The Standard" it will answer quite a lot of questions.

http://www.eskimo.com/~scs/C-faq/q4.12.html

and

http://www.eskimo.com/~scs/C-faq/q1.34.html

will probably be of interest to you.
Nov 14 '05 #2
dandelion <da*******@meadow.net> wrote:
"Kelvin Moss" <km**********@yahoo.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
atexit(&foo); // I think this one
OR
atexit(foo);

The latter. The name of a function evaluates to a pointer to that function.
Much like the name of an array evaluates to a pointer to (the first element
of) that array.


I think both are correct, see 6.3.2.1#4 and 6.5.3.2#1.

+++++

int a[10];
int *pi;
pi = a; //(1) okay
pi = &a; //(2) *wrong!*, diagnostics

I think it was allowed to use (2) in the distant past (I vaguely remember
even seeing code with such construct). What's the story behind this?

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #3
"S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> writes:
dandelion <da*******@meadow.net> wrote:
"Kelvin Moss" <km**********@yahoo.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

> atexit(&foo); // I think this one
> OR
> atexit(foo);

The latter. The name of a function evaluates to a pointer to that function.
Much like the name of an array evaluates to a pointer to (the first element
of) that array.


I think both are correct, see 6.3.2.1#4 and 6.5.3.2#1.

+++++

int a[10];
int *pi;
pi = a; //(1) okay
pi = &a; //(2) *wrong!*, diagnostics

I think it was allowed to use (2) in the distant past (I vaguely remember
even seeing code with such construct). What's the story behind this?


Note that you're talking about two different things here, the behavior
of unary "&" when applied to a function, and the behavior of unary "&"
when applied to an array. Presumably the "+++++" marks a change of
topic.

Any occurrence of a function name, except as the operand of a unary
"&" or sizeof operator, is converted to a pointer to the named
function. (The sizeof exception makes "sizeof func" illegal rather
than making it yield the size of a function pointer.) So "func" and
"&func" are equivalent (as expressions, not necessarily as token
sequences). This applies even to function calls; in a call like
foo(42), you're applying the "()" function call operator to the
address of foo.

As for arrays, "a" is converted to a pointer to the first element of a
(a pointer to int), whereas "&a" gives you the address of the array (a
pointer to an array of 10 ints). These are going to be the same
address, and will probably have the same representation. Older
(pre-ANSI) compilers with weaker type checking might treat them as
interchangeable.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Keith Thompson <ks***@mib.org> wrote:
"S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> writes:
int a[10];
int *pi;
pi = a; //(1) okay
pi = &a; //(2) *wrong!*, diagnostics

I think it was allowed to use (2) in the distant past (I vaguely remember
even seeing code with such construct). What's the story behind this?

Note that you're talking about two different things here, the behavior
of unary "&" when applied to a function, and the behavior of unary "&"
when applied to an array.
I saw distant similarity.
Presumably the "+++++" marks a change of
topic.
Is there a better way?

As for arrays, "a" is converted to a pointer to the first element of a
(a pointer to int), whereas "&a" gives you the address of the array (a
pointer to an array of 10 ints). These are going to be the same
address, and will probably have the same representation. Older
(pre-ANSI) compilers with weaker type checking might treat them as
interchangeable.


Thanks. Actually I confused something else (temporary brain black-out).
Array is a non-modifiable (I knew there was something "wrong" with arrays)
lvalue, but still an lvalue and can be an operand to &.

This code is correct:
int a[10];
int (*p)[10] = &a;

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #5
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
"S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> writes:

int a[10];
int *pi;
pi = a; //(1) okay
pi = &a; //(2) *wrong!*, diagnostics

I think it was allowed to use (2) in the distant past (I vaguely remember
even seeing code with such construct). What's the story behind this?


As for arrays, "a" is converted to a pointer to the first element of a
(a pointer to int), whereas "&a" gives you the address of the array (a
pointer to an array of 10 ints). These are going to be the same
address, and will probably have the same representation. Older
(pre-ANSI) compilers with weaker type checking might treat them as
interchangeable.


Older compilers either didn't allow the & operator to take an array or
function argument or ignored it in such contexts.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #6

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

Similar topics

8
by: JKop | last post by:
Let's say that when your program ends (no matter how) that you want a certain block of code to be executed at the end. Here's the code: std::cout << "The program will now end.\n";...
2
by: Steve Lambert | last post by:
Hi, Is it possible for the functions registered with atexit() to have access to parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE? I'd like to register a set of routines to be executed on...
7
by: David Rushby | last post by:
Consider the following program (underscores are used to force indentation): ------------------------------------------------ import atexit, threading, time def atExitFunc(): ____print...
5
by: prouleau001 | last post by:
Hi all! Since that the decorator syntax is upon us, I think it would be good if atexit.register() was returning the function passed as argument. This simple change to the library would solve a...
1
by: Parapura Rajkumar | last post by:
hey all I was wondering if there a way to override atexit in my own module. The problem I was facing is that the VS2005 seems to ignore functions registered with atexit once it starts processing...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
4
by: Dan | last post by:
Hi All, I've got a problem with my C++ application that calls a Java class that I've built with GCJ, I can't run it because I get errors: multiple definition of `atexit' first defined here...
2
by: Christopher Pisz | last post by:
I am attempting to write a "Phoenix Singleton" using the book "Modern C++ Design" by Alexandrescu I do not understand his use of... #ifndef ATEXIT_FIXED std::atexit(Kill); #endif ....in 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.