Calling atexit

K

Kelvin Moss

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
 
D

dandelion

Kelvin Moss said:
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.
 
S

S.Tobias

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?
 
K

Keith Thompson

S.Tobias said:
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.
 
S

S.Tobias

Keith Thompson said:
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;
 
D

Dan Pop

In said:
S.Tobias said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top