473,836 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

main function address

hi,
Is main function address is 657.
its show in all compiler.
try it & say why?
bye,
Nov 14 '05
89 6546
In <W6************ *******@news20. bellglobal.com> Lew Pitcher <lp******@sympa tico.ca> writes:
~ 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #31
In <cu************ *@zero-based.org> Martin Dickopp <ex************ ****@zero-based.org> writes:
Lew Pitcher <lp******@sympa tico.ca> writes:
#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 *'.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #32
In <cu************ *@zero-based.org> Martin Dickopp <ex************ ****@zero-based.org> writes:
Lew Pitcher <lp******@sympa tico.ca> writes:
#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 *'.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #33
In <40************ ****@news.indiv idual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Martin Dickopp <ex************ ****@zero-based.org> wrote:
Lew Pitcher <lp******@sympa tico.ca> writes:
> #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. 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #34
In <40************ ****@news.indiv idual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Martin Dickopp <ex************ ****@zero-based.org> wrote:
Lew Pitcher <lp******@sympa tico.ca> writes:
> #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. 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #35
pete <pf*****@mindsp ring.com> writes:
Lew Pitcher wrote:

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

pete wrote:
| Martin Dickopp wrote:
|
|>Lew Pitcher <lp******@sympa tico.ca> writes:
|>
|>
|>>#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
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #36
pete <pf*****@mindsp ring.com> writes:
Lew Pitcher wrote:

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

pete wrote:
| Martin Dickopp wrote:
|
|>Lew Pitcher <lp******@sympa tico.ca> writes:
|>
|>
|>>#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
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #37
Richard Bos wrote:
Martin Dickopp <ex************ ****@zero-based.org> wrote:
Lew Pitcher <lp******@sympa tico.ca> writes:
#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. 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[i] );
}

void dump_function_a ddr(const char* fname, fptr_t fptr) {
printf("functio n %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_a ddr("main", main );

return EXIT_SUCCESS;
}
--
Yakov
Nov 14 '05 #38
Richard Bos wrote:
Martin Dickopp <ex************ ****@zero-based.org> wrote:
Lew Pitcher <lp******@sympa tico.ca> writes:
#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. 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[i] );
}

void dump_function_a ddr(const char* fname, fptr_t fptr) {
printf("functio n %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_a ddr("main", main );

return EXIT_SUCCESS;
}
--
Yakov
Nov 14 '05 #39
Lew Pitcher wrote:
pete wrote:
| Martin Dickopp wrote:
|>Lew Pitcher <lp******@sympa tico.ca> writes:
|>
|>> #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.


A better interpretation is that you may not be able to pass the
address of a function to printf. What if you are executing on a
system that dynamically loads and unloads functions, for example.
That address might be a tape volume name and offset, and require
operator intervention to resolve. The data just does not fit into
a void*.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #40

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

Similar topics

11
6922
by: Sweety | last post by:
hello to all members, i have strange question in C. main() { printf("%d",main) ; } here o/p is same for all m/c in TC++ version 3.0 i.e 657. I think this is not garbage.
75
596
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
9
3725
by: DanielJohnson | last post by:
I am wondering where does the value returned by main goes. Suppoes main returns some number say 42, where is it stored. Supposed a shell script call a C program and the program returns the value 42 where is this value stored in memory ?
1
1802
by: davidcollins001 | last post by:
Hi, I am trying to write a stack walking program to trace back along the stack for a process that the trace function is called from. I am doing it the cheating way, in that I have a ruby program (not mine) that fills in an array with the names of functions and arguments, arg types and arguments offset from that frames ebp for all of the functions on the stack. So far I have been able to get it working correctly for all functions except for...
0
9818
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9668
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4015
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3112
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.