473,836 Members | 1,330 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 6544
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
"Lukasz Wawrzyniak" <lw*********@ho tmail.com> wrote:
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?
No it's not. In the absence of a printf() format specifier, you need

unsigned char buff[sizeof( fptr )]; /* will it choke on sizeof(main)?*/


Yes, you cannot do sizeof on a function pointer.
memcpy(buff, main, sizeof(buff)); ^^^^^^^
for(i=0;i<sizeo f(buff);i++)
printf("%02x", (int) buff[i]);


void *memcpy(void *, void *, size_t);

You cannot convert a function pointer to a void pointer portably,
as discussed earlier in this thread. I think you will either have
to rely on implementations allowing this conversion, or not print
out function addresses.

BTW - are there any known implementations where function pointers
cannot be converted to (void *) and back ?
Nov 14 '05 #61
On Thu, 08 Apr 2004 03:19:27 GMT, CBFalconer <cb********@yah oo.com>
wrote:
Leor Zolman wrote:

... snip ...

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


It is not permitted. The printf has no knowledge of what is
passed as parameters, other than what it is told in the format
string. So there can be no implicit conversions there. Thus it
is imperative to pass things that agree with the format string,
and therefore you must cast the pointer to a void* at function
call time.

The normal integer promotions are another matter, and occur for
all parameters passed to any functions. They can NEVER lose
information, and serve to keep the stack (assuming there is a
stack) properly aligned, among other things. Thus you cannot pass
a char, it is always promoted to int. You can, however, tell
printf how to interpret that int. Similarly you can't pass a
short, or a float, without promotions.


Are you saying that calling a von-variadic function with a proper
prototype in scope will still have char, short, and float promoted?
<<Remove the del for email>>
Nov 14 '05 #62
On Thu, 08 Apr 2004 01:43:44 GMT, Keith Thompson <ks***@mib.or g>
wrote:
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. Where this works, it works; where it doesn't, nothing else is
likely to.


You can interpret it as an array of unsigned char and print the values
(in, say, hexadecimal).


If you can't cast it to void*, now can you cast it to unsigned char *?
<<Remove the del for email>>
Nov 14 '05 #63
CBFalconer <cb********@yah oo.com> writes:
[...]
The normal integer promotions are another matter, and occur for
all parameters passed to any functions. They can NEVER lose
information, and serve to keep the stack (assuming there is a
stack) properly aligned, among other things. Thus you cannot pass
a char, it is always promoted to int. You can, however, tell
printf how to interpret that int. Similarly you can't pass a
short, or a float, without promotions.


You can never pass a char, short, or float to printf, but the default
argument promotions are not performed for calls to prototyped
functions (except for arguments corresponding to a "..." in the
prototype).

For example, given the following:

void func(char c) { /* blah blah */ }
char arg = 'x';
func(arg);

no conversion is performed on the argument. (It may be that the
argument-passing convention passes the argument in, say, a 32-bit
register, but there's no conversion as far as the language is
concerned.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #64
In article <news:84******* *************** ****@posting.go ogle.com>
Old Wolf <ol*****@inspir e.net.nz> writes:
Yes, you cannot do sizeof on a function pointer.
Er, you mean "on a function" -- it is OK to apply it to a value of
type "pointer to function ..." (where ... is some valid type).

In other words, given:

int f(void);

the sequence:

sizeof f

is an error (and requires a diagnostic), while:

sizeof &f

is OK and produces the same result as sizeof(int (*)(void)).
BTW - are there any known implementations where function pointers
cannot be converted to (void *) and back ?


IBM AS/400; IBM PC ("compact" model if I remember the name right
-- 16-bit data pointers, 32-bit function pointers). There are no
doubt others, although finding compilers that even claim conformance
(much less achieve it) seems less likely for those. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #65
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
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. Where this works, it works; where it doesn't, nothing else is
likely to.


You can interpret it as an array of unsigned char and print the values
(in, say, hexadecimal).


But the output need not be meaningful. The trivial case is when you get
the byte order wrong, but the infamous (and mythical) padding bits can
turn the output into pure garbage (in theory, at least).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #66
On Thu, 8 Apr 2004 03:46:49 +0000 (UTC), "John Tsiombikas (Nuclear / the
Lab)" <nu*****@siggra ph.org> wrote:
On 2004-04-07, Leor Zolman <le**@bdsoft.co m> wrote:
Actually, it /is/ rather unintuitive to me that in the modern world of the
flat memory model, you can't just pass a foo * in order to print its value
(and be blessed by the Standard) without first casting it to void *.
I think that qualifies as an instance of the old "All world is a VAX"
syndrom :)


Note I did (reluctantly) accept reality two sentences later.

Do VAX people really think that way? The only time I've worked near a VAX
was at MIT LCS, and while folks there were excited to get one (1978), I
think they did pretty well keeping an open mind about other architectures.
Sure, our personal computers are blessed by flat memory models the last
few years but C is not a language for PC-or-greater programming. A lot
of embedded systems still use segmented memory models.


Okay, but I guess the point I was trying to make is that it may not be
"intuitive" for everyone that segmented memory models and void * -> Foo*
conversions represent something akin to an irresistable force meeting an
immovable object. I'm sure it /is/ intuitive, now, for everyone who's
already dealt with the issue in one way or another, but I hadn't ;-)
-leor
--
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 #67
In <c5**********@n ewsg4.svr.pol.c o.uk> "Malcolm" <ma*****@55bank .freeserve.co.u k> writes:

"Lukasz Wawrzyniak" <lw*********@ho tmail.com> wrote in message
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?
No it's not.


Of course it is.
In the absence of a printf() format specifier, you need

unsigned char buff[sizeof( fptr )]; /* will it choke on sizeof(main)?*/
Yes, of course it chokes on sizeof(main): it's a constraint violation.

Constraints

1 The sizeof operator shall not be applied to an expression that
has function type or an incomplete type, to the parenthesized
name of such a type, or to an expression that designates a
bit-field member.
memcpy(buff, main, sizeof(buff));

^^^^
Nonsense. Even on platforms defining the conversion between function
pointers and void pointers, this doesn't yield the expected result: you're
copying sizeof buff bytes from the main function body (on von Neumann
architectures: single address space for code and data, and random
data bytes on Harvard architectures: code and data lie in different
address spaces).

Your ignorance is only matched by your stupidity.

The right code for what you want to do is:

unsigned char buff[sizeof fptr ];
fptr foo = main;
memcpy(buff, &foo, sizeof buff);

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #68
In <c5*********@ne ws3.newsguy.com > Chris Torek <no****@torek.n et> writes:
In article <news:84******* *************** ****@posting.go ogle.com>
Old Wolf <ol*****@inspir e.net.nz> writes:
BTW - are there any known implementations where function pointers
cannot be converted to (void *) and back ?


IBM AS/400; IBM PC ("compact" model if I remember the name right
-- 16-bit data pointers, 32-bit function pointers).


Nope, that's the "medium" memory model. The "compact" model has little
code and lots of data.

The IBM PC has another "interestin g" memory model, the "small" memory
model, which behaves like a Harvard architecture where both data and
code address spaces have the same size, converting function pointers
to data pointers and back works, but using the result of converting
a function pointer to a data pointer to access the function's code bytes
doesn't yield the (naively) expected results (you're reading from the data
address space, not from the code address space). The high end PDP-11
models behaved the same way (the I and D address spaces).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #69
In <c5**********@2 16.39.135.164> Barry Schwarz <sc******@deloz .net> writes:
On Thu, 08 Apr 2004 01:43:44 GMT, Keith Thompson <ks***@mib.or g>
wrote:
rl*@hoekstr a-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. Where this works, it works; where it doesn't, nothing else is
likely to.


You can interpret it as an array of unsigned char and print the values
(in, say, hexadecimal).


If you can't cast it to void*, now can you cast it to unsigned char *?


You can, if you assign it first to a pointer to function, which is an
ordinary object whose address can be converted to unsigned char * (or
even to void *, but this doesn't help, in context):

fptr foo = main;
unsigned char *p = (unsigned char *)&foo;

Now, you can use p to access the representation of the value of foo.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #70

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
9665
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
10835
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...
0
10541
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...
1
10585
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
10249
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
9367
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...
0
6976
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
5818
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4447
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.