main function address

S

Sweety

hi,
Is main function address is 657.
its show in all compiler.
try it & say why?
bye,
 
J

Joona I Palaste

Sweety said:
hi,
Is main function address is 657.
its show in all compiler.
try it & say why?
bye,

No, the main function's address may be whatever your implementation
wants it to be. You can't rely on it being any particular numeric value,
actually you can't rely it on being a numeric value at all.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sweety wrote:
| hi,
| Is main function address is 657.

Perhaps, perhaps not. It depends on your compiler, linker, operating system, and
CPU.

| its show in all compiler.

No, it doesn't.

~ ~/code $ cat printmain.c
~ #include <stdio.h>
~ #include <stdlib.h>

~ int main(void)
~ {
~ printf("main() at %p\n",(void *)&main);

~ return EXIT_SUCCESS;
~ }

~ ~/code $ cc -o printmain printmain.c

~ ~/code $ printmain
~ main() at 0x8048328

~ ~/code $


| try it & say why?

I did, and it' didn't, so I can't say why 657.

| bye,


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFAc16aagVFX4UWr64RAvsiAJ9Tq3RBO4marGAqz3RWYxjcebFfuwCgry/M
5kAKhQe0/0006+/H6+GJp9I=
=qU+e
-----END PGP SIGNATURE-----
 
E

E. Robert Tisdale

Lew said:
Sweety wrote:
|
| Is main function address is 657.

Perhaps, perhaps not.
It depends on your compiler, linker, operating system, and CPU.

| its show in all compiler.

No, it doesn't.

~ ~/code $ cat printmain.c
~ #include <stdio.h>
~ #include <stdlib.h>

~ int main(void)
~ {
~ printf("main() at %p\n",(void *)&main);

~ return EXIT_SUCCESS;
~ }

~ ~/code $ cc -o printmain printmain.c

~ ~/code $ printmain
~ main() at 0x8048328

~ ~/code $


| try it & say why?

I did, and it' didn't, so I can't say why 657.
> cat printmain.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
printf("main() at %p\n",(void *)&main);

return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o printmain printmain.c
> ./printmain
main() at 0x8048328

Why 0x8048328? :)
 
?

=?iso-8859-1?q?Jos=E9_de_Paula?=

Em Tue, 06 Apr 2004 21:51:22 -0400, Lew Pitcher escreveu:
~ printf("main() at %p\n",(void *)&main);
<snip>

Wouldn't simply
printf("main() at %p\n", (void *)main);
do? Notice the prunning of the & operator. For what I know,
the name of a function is a pointer to that function, am I right?
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

José de Paula wrote:
| Em Tue, 06 Apr 2004 21:51:22 -0400, Lew Pitcher escreveu:
| <snip>
|
|>~ printf("main() at %p\n",(void *)&main);
|
| <snip>
|
| Wouldn't simply
| printf("main() at %p\n", (void *)main);
| do? Notice the prunning of the & operator. For what I know,
| the name of a function is a pointer to that function, am I right?

You are correct. The & was unnecessary.


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFAc2o1agVFX4UWr64RAofEAKCScgoEuAKn8ci/cjeimsG01iyWXACgznfY
/2jtHAGq0Fpm6dHKZ1ZdP7E=
=ra40
-----END PGP SIGNATURE-----
 
E

E. Robert Tisdale

José de Paula said:
Lew Pitcher escreveu:


<snip>

Wouldn't simply
printf("main() at %p\n", (void *)main);
do? Notice the pruning of the & operator. For what I know,
the name of a function is a pointer to that function. Am I right?

Yes. There is an implicit conversion
from the name of a function to a pointer to the function.
 
M

Martin Dickopp

Lew Pitcher said:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("main() at %p\n",(void *)&main);

return EXIT_SUCCESS;
}

Is the cast to `void *' valid? I cannot find anything in the standard
which allows a pointer to a function to be converted to type `void *'.

Martin
 
P

pete

Martin said:
Is the cast to `void *' valid?

No.

In N869, it's one of the common extensions.

J.5.7 Function pointer casts
[#2] A pointer to a function may be cast to a pointer to an
object or to void, allowing a function to be inspected or
modified (for example, by a debugger) (6.5.4).

.... which makes it more obviously not part of standard C.
 
V

Vijay Kumar R Zanvar

Lew Pitcher said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sweety wrote:
| hi,
| Is main function address is 657.

Perhaps, perhaps not. It depends on your compiler, linker, operating system, and
CPU.

| its show in all compiler.

No, it doesn't.

~ ~/code $ cat printmain.c
~ #include <stdio.h>
~ #include <stdlib.h>

~ int main(void)
~ {
~ printf("main() at %p\n",(void *)&main);

~ return EXIT_SUCCESS;
~ }

~ ~/code $ cc -o printmain printmain.c

~ ~/code $ printmain
~ main() at 0x8048328

F:\Vijay\C> bcc32 printmain.c
F:\Vijay\C> printmain
main() at 00401150


F:\Vijay\C> gcc printmain.c
F:\Vijay\C> a
main() at 15ee
 
K

Keith Thompson

Lew Pitcher said:
José de Paula wrote:
| Em Tue, 06 Apr 2004 21:51:22 -0400, Lew Pitcher escreveu:
| <snip>
|
|>~ printf("main() at %p\n",(void *)&main);
|
| <snip>
|
| Wouldn't simply
| printf("main() at %p\n", (void *)main);
| do? Notice the prunning of the & operator. For what I know,
| the name of a function is a pointer to that function, am I right?

You are correct. The & was unnecessary.

It's unnecessary, but harmless, and some might argue that it's
clearer, since it makes it more explicit that the expression is an
address.

A function name is implicitly converted to a function pointer unless
it's the operand of a sizeof or unary & operator. (In the case of
sizeof, the resulting expression is illegal.)
 
R

Richard Bos

Martin Dickopp said:
Is the cast to `void *' valid?

No. Mind you, there is no better way to print the address of a function,
either. Where this works, it works; where it doesn't, nothing else is
likely to.

Richard
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pete wrote:
| Martin Dickopp wrote:
|
|>
|>
|>>#include <stdio.h>
|>>#include <stdlib.h>
|>>
|>>int main(void)
|>>{
|>> printf("main() at %p\n",(void *)&main);
|>>
|>> return EXIT_SUCCESS;
|>>}
|>
|>Is the cast to `void *' valid?
|
|
| No.
|
| In N869, it's one of the common extensions.
|
| J.5.7 Function pointer casts
| [#2] A pointer to a function may be cast to a pointer to an
| object or to void, allowing a function to be inspected or
| modified (for example, by a debugger) (6.5.4).
|
| ... which makes it more obviously not part of standard C.

In 9989-1999 (admittedly, just the draft C99 standard, and not the
/actual standard itself), the printf() function documentation in
7.19.6.3 refers the reader to the fprintf() documentation for a
description of it's input. The fprintf() documentation in 7.19.6.1 says
of the %p format

~ p The argument shall be a pointer to void. The value of the pointer is
~ converted to a sequence of printing characters, in an
~ implementation-defined manner.

So, to satisfy the %p format character, the argument to
fprintf()/printf() /must/ be a "pointer to void". Since main is a
"pointer to function returning int", and not a "pointer to void", I
interpreted the documentation as requiring a cast to void pointer.



- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAc+o1agVFX4UWr64RAgMKAJ9jpwGgLD0Ib6+SrFveX/B2DiSkQQCgn1b9
hcqV26vMzT9PW6/myM6CbsQ=
=jmLW
-----END PGP SIGNATURE-----
 
P

pete

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pete wrote:
| Martin Dickopp wrote:
|
|>
|>
|>>#include <stdio.h>
|>>#include <stdlib.h>
|>>
|>>int main(void)
|>>{
|>> printf("main() at %p\n",(void *)&main);
|>>
|>> return EXIT_SUCCESS;
|>>}
|>
|>Is the cast to `void *' valid?
|
|
| No.
|
| In N869, it's one of the common extensions.
|
| J.5.7 Function pointer casts
| [#2] A pointer to a function may be cast to a pointer to an
| object or to void, allowing a function to be inspected or
| modified (for example, by a debugger) (6.5.4).
|
| ... which makes it more obviously not part of standard C.

In 9989-1999 (admittedly, just the draft C99 standard, and not the
/actual standard itself), the printf() function documentation in
7.19.6.3 refers the reader to the fprintf() documentation for a
description of it's input. The fprintf() documentation in 7.19.6.1 says
of the %p format

~ p The argument shall be a pointer to void. The value of the pointer is
~ converted to a sequence of printing characters, in an
~ implementation-defined manner.

So, to satisfy the %p format character, the argument to
fprintf()/printf() /must/ be a "pointer to void". Since main is a
"pointer to function returning int", and not a "pointer to void", I
interpreted the documentation as requiring a cast to void pointer.

I interpret it as meaning that printing the address of a function
isn't something that you are guaranteed to be able to do.
 
L

Leor Zolman

In 9989-1999 (admittedly, just the draft C99 standard, and not the
/actual standard itself), the printf() function documentation in
7.19.6.3 refers the reader to the fprintf() documentation for a
description of it's input. The fprintf() documentation in 7.19.6.1 says
of the %p format

~ p The argument shall be a pointer to void. The value of the pointer is
~ converted to a sequence of printing characters, in an
~ implementation-defined manner.

So, to satisfy the %p format character, the argument to
fprintf()/printf() /must/ be a "pointer to void". Since main is a
"pointer to function returning int", and not a "pointer to void", I
interpreted the documentation as requiring a cast to void pointer.

Curious -- I've never considered how implicit conversion rules ought to
play out in the arena of variadic functions... on one hand, pointers to
/anything/ implicitly convert to pointer-to-void, but on the other hand
there's no declaration for the receiving pointer-to-void.

Or, does the implicit conversion apply when the pointer value is extracted
and cast to void * in the receiving function?
-leor
 
D

Dan Pop

In said:
~ printf("main() at %p\n",(void *)&main);
^^^^^^^^^^^^^
Undefined behaviour. The standard doesn't define conversions between
function pointers and incomplete or object pointer types. And there is no
guarantee that the type pointer to void is wide enough to be able to
represent the result of such a conversion.

6.3.2.3 Pointers

1 A pointer to void may be converted to or from a pointer to any
incomplete or object type...
^^^^^^^^^^^^^^^^^^^^^^^^^

7 A pointer to an object or incomplete type may be converted to
a pointer to a different object or incomplete type...

8 A pointer to a function of one type may be converted to a pointer
to a function of another type and back again; the result shall
compare equal to the original pointer...

Dan
 
D

Dan Pop

In said:
Is the cast to `void *' valid? I cannot find anything in the standard
which allows a pointer to a function to be converted to type `void *'.

It is syntactically valid, but devoid of any semantics, therefore it is
a case of undefined behaviour due to lack of specification.

One can replace unconditionally invoking undefined behaviour by
conditionally invoking undefined behaviour this way:

unsigned long address = (unsigned long)main;
printf("main() at %lx\n", address);

This invokes undefined behaviour *only* if the address of main cannot
be represented as an unsinged long:

6 Any pointer type may be converted to an integer type. Except as
previously specified, the result is implementation-defined. If the
result cannot be represented in the integer type, the behavior
is undefined. The result need not be in the range of values of
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
any integer type.
^^^^^^^^^^^^^^^^^

C99 users may want to use unsigned long long for this purpose, to increase
their chances of avoiding undefined behaviour. However, the language
doesn't guarantee the existence of a solution to this problem (the AS/400
programmers know why ;-)

Dan
 
D

Dan Pop

In said:
No. Mind you, there is no better way to print the address of a function,
either.

There is, even if you can't figure it out...
Where this works, it works; where it doesn't, nothing else is likely to.

How do you know?

Dan
 
M

Martin Dickopp

pete said:
Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pete wrote:
| Martin Dickopp wrote:
|
|>
|>
|>>#include <stdio.h>
|>>#include <stdlib.h>
|>>
|>>int main(void)
|>>{
|>> printf("main() at %p\n",(void *)&main);
|>>
|>> return EXIT_SUCCESS;
|>>}
|>
|>Is the cast to `void *' valid?
|
|
| No.
|
| In N869, it's one of the common extensions.
|
| J.5.7 Function pointer casts
| [#2] A pointer to a function may be cast to a pointer to an
| object or to void, allowing a function to be inspected or
| modified (for example, by a debugger) (6.5.4).
|
| ... which makes it more obviously not part of standard C.

In 9989-1999 (admittedly, just the draft C99 standard, and not the
/actual standard itself), the printf() function documentation in
7.19.6.3 refers the reader to the fprintf() documentation for a
description of it's input. The fprintf() documentation in 7.19.6.1 says
of the %p format

~ p The argument shall be a pointer to void. The value of the pointer is
~ converted to a sequence of printing characters, in an
~ implementation-defined manner.

So, to satisfy the %p format character, the argument to
fprintf()/printf() /must/ be a "pointer to void". Since main is a
"pointer to function returning int", and not a "pointer to void", I
interpreted the documentation as requiring a cast to void pointer.

I interpret it as meaning that printing the address of a function
isn't something that you are guaranteed to be able to do.

So do I. You certainly cannot do it with the `%p' specifier. 6.3.2.3#1
makes it quite clear that only pointers to incomplete or object type can
be converted to `void *'.

Martin
 
Y

Yakov Lerner

Richard said:
No. Mind you, there is no better way to print the address of a function,
either. Where this works, it works; where it doesn't, nothing else is
likely to.

Yes there is a better way.
The code below will still print the binary representation of the
address of the function where %p won't. Even if
sizeof(int(*)()) is bigger than sizeof(void*).

The trick is that even when you might not convert function ptr
to void*, nobody said you cannot convert the (&fptr)
to the (void*) :), see below :

/* dump address of the function no matter whether it can be
* converted to void* or not */
#include <stdio.h>
#include <stdlib.h>

typedef int (*fptr_t)();
void dump_hex( FILE* out, void* p, int len)
{
unsigned char *pb = p;
int i;
for(i=0; i < len; i++)
printf("%02X", 0xFF & (unsigned int)pb );
}

void dump_function_addr(const char* fname, fptr_t fptr) {
printf("function %s() is at [", fname);
dump_hex( stdout, (void*)&fptr, sizeof(fptr) );

/* nb: you might not convert &main to void*, but */
/* nobody said we cannot convert '&fptr' to void* :) */

printf("]\n");
}

int main(void)
{
dump_function_addr("main", main );

return EXIT_SUCCESS;
}
 

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

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top