473,890 Members | 1,397 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 6561
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 #41

On Wed, 7 Apr 2004, Leor Zolman wrote:

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?


I don't understand what you mean. Pointer-to-foo and pointer-to-void
can be implicitly "inter-converted" like this:

foo *pf;
void *pv;

pf = pv; pv = pf;

Likewise, 'pf' passed to a function prototyped as expecting a void
pointer will be implicitly converted. And vice versa.
Variadic functions are by definition not prototyped as expecting
anything in particular in the "..." part. So when you have

printf("foo", pf);

the value of 'pf' is passed to 'printf' as a pointer to foo, no
matter what the function actually expects. Likewise, in

printf("foo", pv);

'pv' is passed as a pointer to void.
Does that clear up your doubts?

HTH,
-Arthur

Nov 14 '05 #42

On Wed, 7 Apr 2004, Leor Zolman wrote:

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?


I don't understand what you mean. Pointer-to-foo and pointer-to-void
can be implicitly "inter-converted" like this:

foo *pf;
void *pv;

pf = pv; pv = pf;

Likewise, 'pf' passed to a function prototyped as expecting a void
pointer will be implicitly converted. And vice versa.
Variadic functions are by definition not prototyped as expecting
anything in particular in the "..." part. So when you have

printf("foo", pf);

the value of 'pf' is passed to 'printf' as a pointer to foo, no
matter what the function actually expects. Likewise, in

printf("foo", pv);

'pv' is passed as a pointer to void.
Does that clear up your doubts?

HTH,
-Arthur

Nov 14 '05 #43
> 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


Can a pointer to a function be converted to an integer type such as
size_t such that one could use printf("0x%x\n" , (size_t)main); ?

Hmmm, now that I think of it, is it legal (according to the standard)
to convert between pointer and integer types at all?
Nov 14 '05 #44
> 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


Can a pointer to a function be converted to an integer type such as
size_t such that one could use printf("0x%x\n" , (size_t)main); ?

Hmmm, now that I think of it, is it legal (according to the standard)
to convert between pointer and integer types at all?
Nov 14 '05 #45
On Wed, 7 Apr 2004 16:04:10 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.and rew.cmu.edu> wrote:

On Wed, 7 Apr 2004, Leor Zolman wrote:

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?
I don't understand what you mean. Pointer-to-foo and pointer-to-void
can be implicitly "inter-converted" like this:

foo *pf;
void *pv;

pf = pv; pv = pf;

Yes, of course.
Likewise, 'pf' passed to a function prototyped as expecting a void
pointer will be implicitly converted. And vice versa.
No questions there.
Variadic functions are by definition not prototyped as expecting
anything in particular in the "..." part. So when you have

printf("foo", pf);

the value of 'pf' is passed to 'printf' as a pointer to foo, no
matter what the function actually expects.
And therein lies my question. If pf has type pointer-to-foo, but is printed
using a %p format conversion, that represents a foo* -> void* conversion
without the compiler's "knowledge" ... when the internals of printf extract
that argument, it will at some point be "converted" into a void *, but
without any knowledge of what it was "before". So is such an implicit
conversion officially permitted? I've been writing code that does that
forever, and it makes me wonder...

Lew pointed out 7.19.6.1, where it says "the argument [to %p] shall be a
pointer to void. Pete above points out how (explicit) conversion of a
pointer-to-function into a pointer-to-void is a "common extension" (so it
is definitely bad karma to omit the cast with a pointer-to-function)...but
what is the Standard's take on performing no cast whatsoever on a
pointer-to-something-/else/ (not a function, and not void)? IOW, is:
int i, *pi = &i;
printf("%p\n", pi);
conformant?
Likewise, in

printf("foo", pv);

'pv' is passed as a pointer to void.
This is the case that seems well-defined to me, in the case of a %p
conversion.
Does that clear up your doubts?
Does that clear up my question? :-)

I'm quite a newbie at Standard-reading, my implementation experience
limited to a very crude subset of pre-C89 C. So please view my questions as
/questions/, rather than any deliberate attempt to suggest a problem with
the Standard. If I do happen to stumble across any problems (and I guess I
may have come close on one or two occasions, even if they weren't
original), that's purely by accident.
-leor

HTH,
-Arthur


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #46
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

El Wed, 07 Apr 2004 10:07:15 +0200, Martin Dickopp escribió:

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 *'.


Hi, I'm new to this group, just a couple of days reading. I can see you
are constantly referring to the "Standard" and I've seen in the FAQ that
you must pay for it ($18 electronic document).

All of you have payed for it? shouldn't a "standard" be free?

Thanks in advance and sorry for my english
- --
Alberto Giménez, SimManiac en el IRC
http://www.almorranasozial.es.vg
GNU/Linux Debian Woody 3.0 GnuPG ID: 0x3BAABDE1
Linux registered user #290801
WinError 01E: Timing error - Please wait. And wait. And wait. And wait.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAdHAp0ke CtzuqveERAuOuAJ 0cl6hlfaq+ikGVV KTthj/DUgaJVwCaAiRb
4mdlpHHPze7fArd yUEuEe5U=
=uj/E
-----END PGP SIGNATURE-----
Nov 14 '05 #47
On Wed, 7 Apr 2004 23:18:33 +0200, Alberto Giménez <al****@telelin e.es>
wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

El Wed, 07 Apr 2004 10:07:15 +0200, Martin Dickopp escribió:

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 *'.
Hi, I'm new to this group, just a couple of days reading. I can see you
are constantly referring to the "Standard" and I've seen in the FAQ that
you must pay for it ($18 electronic document).

All of you have payed for it? shouldn't a "standard" be free?


I tried using the free version of the "Draft" Standard that's floating
around, and which I guess is legitimately downloadable (although I couldn't
find it last time I looked) for free. But there are enough differences that
I soon (happily) forked over the $18 for my electronic copy of the Real
McCoy.

Compared to what the committee members had to pay in order to cover their
own expenses just to be on the Standard committee, we're getting a bargain
;-)
-leor

Thanks in advance and sorry for my english


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #48

On Wed, 7 Apr 2004, Leor Zolman wrote:

Arthur O'Dwyer <aj*@nospam.and rew.cmu.edu> wrote:
Variadic functions are by definition not prototyped as expecting
anything in particular in the "..." part. So when you have

printf("foo", pf);

the value of 'pf' is passed to 'printf' as a pointer to foo, no
matter what the function actually expects.
And therein lies my question. If pf has type pointer-to-foo, but is printed
using a %p format conversion, that represents a foo* -> void* conversion
without the compiler's "knowledge" ... when the internals of printf extract
that argument, it will at some point be "converted" into a void *, but
without any knowledge of what it was "before". So is such an implicit
conversion officially permitted? I've been writing code that does that
forever, and it makes me wonder...


If you pass a value of type T to a variadic function, and that function
is expecting a value of type U instead, then you have undefined behavior,
unless <some legalese involving cv-qualification>. It's not terribly
unintuitive, is it?
[I think N869 section 7.15.1.1 is wrong when it uses the word
"compatible " to describe what I think is really "having the same
alignment and size restrictions" or something like that. Unless
it was the Committee's intent to have all implementations pass
pointers into variadic functions *as void pointers*, and have an
implicit conversion take place inside 'va_arg'. Experts, please?]
All the exceptions I'm aware of are covered by N869 section 6.2.5.27,
which allows things like

struct foo p;
my_vfunc("actua lly retrieves a struct bar", &p);
unsigned char *p = <something>;
printf("%p", p);

Since 'unsigned char *' and 'void *' are guaranteed to have the same
representation (at least, according to my interpretation of the Standard;
I know some people think *all* character types are similar to 'void *',
and others think that only *at least one* character type is similar
to 'void *'), this is conforming code.
But it's so easy to add the cast every time and make sure, that it's
just not worth fiddling around with special cases. :)
Lew pointed out 7.19.6.1, where it says "the argument [to %p] shall be a
pointer to void. Pete above points out how (explicit) conversion of a
pointer-to-function into a pointer-to-void is a "common extension" (so it
is definitely bad karma to omit the cast with a pointer-to-function)...but
what is the Standard's take on performing no cast whatsoever on a
pointer-to-something-/else/ (not a function, and not void)? IOW, is:
int i, *pi = &i;
printf("%p\n", pi);
conform[ing]?


No. See above; 'int *' and 'void *' do not necessarily have the
same representation or alignment requirements.

-Arthur

Nov 14 '05 #49

On Wed, 7 Apr 2004, Alberto [iso-8859-1] Giménez wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
It would be nice if you could figure out some way to balance your
authentication interests against the readability of your message.
PGP signatures in the text of your message are ugly. (But attachments
are just evil.) Please consider turning off your PGP signature when
posting to Usenet.
El Wed, 07 Apr 2004 10:07:15 +0200, Martin Dickopp escribió:

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 *'.
Hi, I'm new to this group, just a couple of days reading. I can see you
are constantly referring to the "Standard" and I've seen in the FAQ that
you must pay for it ($18 electronic document).

All of you have payed for it? shouldn't a "standard" be free?


That's what I think. ;-) But it costs a lot of money to fly
international experts out to ISO committee meetings to discuss the
state of the C language and suchlike, and it costs a lot of money
to print copies of the standard, and maintain web servers, and pay
salaries to ISO people, and convene meetings to deal with errata
and corrigenda. So I suppose ISO (and member organizations) have
a good reason to charge for copies of the Standard.

If you don't want to buy a copy -- I haven't, and I think I'm
not alone -- you can find electronic versions of N869, the last
public draft of the C99 standard, online somewhere. Google for
"N869".
- --


That is not a proper sig delimiter, as far as I know. The standard
signature delimiter on Usenet is two hyphens followed by a single space
and a newline, like this:

--
Arthur
Nov 14 '05 #50

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

Similar topics

11
6925
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
3730
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
9829
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
11236
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10926
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9643
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
8018
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
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6064
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4278
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3287
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.