main function address

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sweety

    #1

    main function address

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

    #2
    Re: main function address

    Sweety <sweety_elegant @yahoo.co.in> scribbled the following:[color=blue]
    > hi,
    > Is main function address is 657.
    > its show in all compiler.
    > try it & say why?
    > bye,[/color]

    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.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Keep shooting, sooner or later you're bound to hit something."
    - Misfire

    Comment

    • Old Wolf

      #3
      Re: main function address

      Dan.Pop@cern.ch (Dan Pop) wrote:[color=blue]
      >
      > The easy way out for printf would be something like %q which expects a
      > pointer to a void function taking no arguments. However, given that %p
      > itself is virtually never used in real world programs, and that
      > %lx or %llx do work on the pointer converted to the appropriate integer
      > type, there is little point on adding %q.[/color]

      %p is indispensable[*] on systems without flat address space
      (for example, on MS-DOS it prints segment:offset, on 8051 it tells you
      which page the pointer points to, and presumably on these "semi-64 bit"
      contraptions that are coming out, it will print segment:offset) .
      Admittedly I've never used it except in recording of debug information.

      Windows 3.1 crash messages printed their addresses in the same format,
      so I would conjecture that Windows still uses "%p" to print the
      address of where programs crashed (virtually every day, in real world PCs)
      [*] Of course you can do the (unsigned char *)&ptr thing but that is tedious,
      and using %lx is relying on undefined behaviour.

      Comment

      • Lew Pitcher

        #4
        Re: main function address

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

        iD8DBQFAc16aagV FX4UWr64RAvsiAJ 9Tq3RBO4marGAqz 3RWYxjcebFfuwCg ry/M
        5kAKhQe0/0006+/H6+GJp9I=
        =qU+e
        -----END PGP SIGNATURE-----

        Comment

        • José de Paula

          #5
          Re: main function address

          Em Tue, 06 Apr 2004 21:51:22 -0400, Lew Pitcher escreveu:
          <snip>[color=blue]
          > ~ printf("main() at %p\n",(void *)&main);[/color]
          <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?

          --
          José de Paula Rodrigues Neto Assis Linux User 175920
          Brasília - DF - Brasil counter.li.org

          Comment

          • Lew Pitcher

            #6
            Re: main function address

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

            iD8DBQFAc2o1agV FX4UWr64RAofEAK CScgoEuAKn8ci/cjeimsG01iyWXAC gznfY
            /2jtHAGq0Fpm6dHK Z1ZdP7E=
            =ra40
            -----END PGP SIGNATURE-----

            Comment

            • Dan Pop

              #7
              Re: main function address

              In <843a4f78.04041 41850.166ab63b@ posting.google. com> oldwolf@inspire .net.nz (Old Wolf) writes:
              [color=blue]
              >Dan.Pop@cern.c h (Dan Pop) wrote:[color=green]
              >>
              >> The easy way out for printf would be something like %q which expects a
              >> pointer to a void function taking no arguments. However, given that %p
              >> itself is virtually never used in real world programs, and that
              >> %lx or %llx do work on the pointer converted to the appropriate integer
              >> type, there is little point on adding %q.[/color]
              >
              >%p is indispensable[*] on systems without flat address space
              >(for example, on MS-DOS it prints segment:offset, on 8051 it tells you
              >which page the pointer points to, and presumably on these "semi-64 bit"
              >contraptions that are coming out, it will print segment:offset) .
              >Admittedly I've never used it except in recording of debug information.
              >
              >Windows 3.1 crash messages printed their addresses in the same format,
              >so I would conjecture that Windows still uses "%p" to print the
              >address of where programs crashed (virtually every day, in real world PCs)[/color]

              Have I ever advocating dropping %p from the standard? If not, what
              exactly is your point?
              [color=blue]
              >[*] Of course you can do the (unsigned char *)&ptr thing but that is tedious,
              >and using %lx is relying on undefined behaviour.[/color]

              You got it wrong: using %p for function pointers *is* relying on undefined
              behaviour, using %lx for function pointers converted to unsigned long is
              NOT undefined behaviour (such conversions are *explicitly* allowed by the
              standard).

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              • E. Robert Tisdale

                #8
                Re: main function address

                Lew Pitcher wrote:
                [color=blue]
                > 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.[/color]
                [color=blue]
                > cat printmain.c[/color]
                #include <stdio.h>
                #include <stdlib.h>

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

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

                Why 0x8048328? :-)

                Comment

                • E. Robert Tisdale

                  #9
                  Re: main function address

                  José de Paula wrote:
                  [color=blue]
                  > Lew Pitcher escreveu:
                  >
                  > <snip>[color=green]
                  >>~ printf("main() at %p\n",(void *)&main);[/color]
                  > <snip>
                  >
                  > Wouldn't simply[/color]
                  [color=blue]
                  > printf("main() at %p\n", (void *)main);[/color]
                  [color=blue]
                  > 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?[/color]

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



                  Comment

                  • Martin Dickopp

                    #10
                    Re: main function address

                    Lew Pitcher <lpitcher@sympa tico.ca> writes:
                    [color=blue]
                    > #include <stdio.h>
                    > #include <stdlib.h>
                    >
                    > int main(void)
                    > {
                    > printf("main() at %p\n",(void *)&main);
                    >
                    > return EXIT_SUCCESS;
                    > }[/color]

                    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


                    --
                    ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
                    / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
                    \ `-' `-'(. .)`-'
                    `-. Debian, a variant of the GNU operating system. \_/

                    Comment

                    • pete

                      #11
                      Re: main function address

                      Martin Dickopp wrote:[color=blue]
                      >
                      > Lew Pitcher <lpitcher@sympa tico.ca> writes:
                      >[color=green]
                      > > #include <stdio.h>
                      > > #include <stdlib.h>
                      > >
                      > > int main(void)
                      > > {
                      > > printf("main() at %p\n",(void *)&main);
                      > >
                      > > return EXIT_SUCCESS;
                      > > }[/color]
                      >
                      > Is the cast to `void *' valid?[/color]

                      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.

                      --
                      pete

                      Comment

                      • Vijay Kumar R Zanvar

                        #12
                        Re: main function address


                        "Lew Pitcher" <lpitcher@sympa tico.ca> wrote in message news:W6Jcc.5205 $BF2.654200@new s20.bellglobal. com...[color=blue]
                        > -----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
                        >[/color]

                        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


                        Comment

                        • Keith Thompson

                          #13
                          Re: main function address

                          Lew Pitcher <lpitcher@sympa tico.ca> writes:[color=blue]
                          > 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.[/color]

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

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@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"

                          Comment

                          • Richard Bos

                            #14
                            Re: main function address

                            Martin Dickopp <expires-2004-05-31@zero-based.org> wrote:
                            [color=blue]
                            > Lew Pitcher <lpitcher@sympa tico.ca> writes:
                            >[color=green]
                            > > #include <stdio.h>
                            > > #include <stdlib.h>
                            > >
                            > > int main(void)
                            > > {
                            > > printf("main() at %p\n",(void *)&main);
                            > >
                            > > return EXIT_SUCCESS;
                            > > }[/color]
                            >
                            > Is the cast to `void *' valid?[/color]

                            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

                            Comment

                            • Lew Pitcher

                              #15
                              Re: main function address

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

                              pete wrote:
                              | Martin Dickopp wrote:
                              |
                              |>Lew Pitcher <lpitcher@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.



                              - --

                              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+o1agV FX4UWr64RAgMKAJ 9jpwGgLD0Ib6+Sr FveX/B2DiSkQQCgn1b9
                              hcqV26vMzT9PW6/myM6CbsQ=
                              =jmLW
                              -----END PGP SIGNATURE-----

                              Comment

                              Working...