how to find out the address of a variable after I compile the C file

Discussion in 'C Programming' started by bijayadipti@gmail.com, May 18, 2006.

  1. Guest

    Hi,

    I have a C program. I have compiled it uisng gcc and also avr-gcc. Now
    after compiling, I want to know the addresses of the two variables in
    my program. Is there any options that I can use to find that out? Is
    there any way at all to find that out? Someone told that it was
    possible but I am not able to find out.

    Thank you in advance,
    priya
    , May 18, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    Re: how to find out the address of a variable after I compile theC file

    wrote:
    > Hi,
    >
    > I have a C program. I have compiled it uisng gcc and also avr-gcc. Now
    > after compiling, I want to know the addresses of the two variables in
    > my program. Is there any options that I can use to find that out? Is
    > there any way at all to find that out? Someone told that it was
    > possible but I am not able to find out.
    >

    Sounds like you are looking for a map file, check the compiler
    documentation. If avr-gcc is for embedded targets, you should be able
    to generate one, but the details are off topic here.

    --
    Ian Collins.
    Ian Collins, May 18, 2006
    #2
    1. Advertising

  3. pete Guest

    wrote:
    >
    > Hi,
    >
    > I have a C program. I have compiled it uisng gcc and also avr-gcc. Now
    > after compiling, I want to know the addresses of the two variables in
    > my program. Is there any options that I can use to find that out? Is
    > there any way at all to find that out? Someone told that it was
    > possible but I am not able to find out.


    If a variable is local to a function other than main,
    then it may or may not have the same address,
    each time the function is called during the run of the program.

    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    double x;

    printf("The address of x is %p\n", (void *)&x);
    return 0;
    }

    /* END new.c */


    --
    pete
    pete, May 18, 2006
    #3
  4. Nelu Guest

    wrote:
    > Hi,
    >
    > I have a C program. I have compiled it uisng gcc and also avr-gcc. Now
    > after compiling, I want to know the addresses of the two variables in
    > my program. Is there any options that I can use to find that out? Is
    > there any way at all to find that out? Someone told that it was
    > possible but I am not able to find out.


    If you want to display the adress of a variable from the program you
    can use %p for printf format, e.g. printf("Address var1=%p,
    var2=%p",&v1, &v2); (where v1 and v2 are your variables).
    If you want to find the address of the variables in the compiled
    program you may have a problem. Consider the case of a recursive
    function. The local variables are going to be created at different
    addresses with each recursive call.

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
    Nelu, May 18, 2006
    #4
  5. Re: how to find out the address of a variable after I compile the Cfile

    "Nelu" <> writes:
    > wrote:
    >> Hi,
    >>
    >> I have a C program. I have compiled it uisng gcc and also avr-gcc. Now
    >> after compiling, I want to know the addresses of the two variables in
    >> my program. Is there any options that I can use to find that out? Is
    >> there any way at all to find that out? Someone told that it was
    >> possible but I am not able to find out.

    >
    > If you want to display the adress of a variable from the program you
    > can use %p for printf format, e.g. printf("Address var1=%p,
    > var2=%p",&v1, &v2); (where v1 and v2 are your variables).
    > If you want to find the address of the variables in the compiled
    > program you may have a problem. Consider the case of a recursive
    > function. The local variables are going to be created at different
    > addresses with each recursive call.


    The "%p" format requires an argument of type void*. The printf call
    above should be:

    printf("Address var1=%p, var2=%p", (void*)&v1, (void*)&v2);

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 18, 2006
    #5
  6. Nelu Guest

    Keith Thompson wrote:
    > "Nelu" <> writes:

    <snip>
    > > If you want to display the adress of a variable from the program you
    > > can use %p for printf format, e.g. printf("Address var1=%p,
    > > var2=%p",&v1, &v2); (where v1 and v2 are your variables).
    > > If you want to find the address of the variables in the compiled
    > > program you may have a problem. Consider the case of a recursive
    > > function. The local variables are going to be created at different
    > > addresses with each recursive call.

    >
    > The "%p" format requires an argument of type void*. The printf call
    > above should be:
    >
    > printf("Address var1=%p, var2=%p", (void*)&v1, (void*)&v2);


    I keep forgetting to cast. It happened again a few days back (with
    sizeof) :). Sorry about that.

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
    Nelu, May 18, 2006
    #6
  7. "pete" <> wrote in message
    news:...
    > wrote:
    >> I have a C program. I have compiled it uisng gcc and also avr-gcc. Now
    >> after compiling, I want to know the addresses of the two variables in
    >> my program. Is there any options that I can use to find that out? Is
    >> there any way at all to find that out? Someone told that it was
    >> possible but I am not able to find out.

    >
    > If a variable is local to a function other than main,
    > then it may or may not have the same address,
    > each time the function is called during the run of the program.


    On some (many?) architectures, even globals, statics, and main()'s locals
    will vary across runs. In the case of recursive functions, different
    instances of the same locals may exist in several places.

    The only decent answers are "find out with the & operator at runtime" or
    "use a debugger".

    S

    --
    Stephen Sprunk "Stupid people surround themselves with smart
    CCIE #3723 people. Smart people surround themselves with
    K5SSS smart people who disagree with them." --Aaron Sorkin


    *** Posted via a free Usenet account from http://www.teranews.com ***
    Stephen Sprunk, May 18, 2006
    #7
  8. In article <>,
    Nelu <> wrote:

    >If you want to display the adress of a variable from the program you
    >can use %p for printf format,


    %p merely promises a reversible representation, not an address
    or anything necessarily meaningful to humans. It would be valid
    for %p to hash the internal address before printing it out, as long
    as the scanner knows how to reverse the hash upon input.
    The %p output could even be the uuencoding of the instruction
    sequence that would be needed to be executed to recreate the pointer value.
    --
    "It is important to remember that when it comes to law, computers
    never make copies, only human beings make copies. Computers are given
    commands, not permission. Only people can be given permission."
    -- Brad Templeton
    Walter Roberson, May 18, 2006
    #8
  9. Ben Pfaff Guest

    -cnrc.gc.ca (Walter Roberson) writes:

    > %p merely promises a reversible representation, not an address
    > or anything necessarily meaningful to humans. It would be valid
    > for %p to hash the internal address before printing it out, as long
    > as the scanner knows how to reverse the hash upon input.


    That uses a funny definition of "hash". In my experience, a hash
    loses information irreversibly. I would suggest that you really
    mean that %p may print an encrypted version of a pointer as long
    as it can be decrypted on input.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, May 18, 2006
    #9
  10. Jordan Abel Guest

    On 2006-05-18, Ben Pfaff <> wrote:
    > -cnrc.gc.ca (Walter Roberson) writes:
    >
    >> %p merely promises a reversible representation, not an address
    >> or anything necessarily meaningful to humans. It would be valid
    >> for %p to hash the internal address before printing it out, as long
    >> as the scanner knows how to reverse the hash upon input.

    >
    > That uses a funny definition of "hash". In my experience, a hash
    > loses information irreversibly. I would suggest that you really
    > mean that %p may print an encrypted version of a pointer as long
    > as it can be decrypted on input.


    He could mean it places it in a hash table and prints out the
    (arbitrary) key.
    Jordan Abel, May 18, 2006
    #10
  11. In article <>,
    Ben Pfaff <> wrote:
    >-cnrc.gc.ca (Walter Roberson) writes:


    >> %p merely promises a reversible representation, not an address
    >> or anything necessarily meaningful to humans. It would be valid
    >> for %p to hash the internal address before printing it out, as long
    >> as the scanner knows how to reverse the hash upon input.


    >That uses a funny definition of "hash". In my experience, a hash
    >loses information irreversibly.


    Not if it is a "perfect hash".

    Besides, on some systems, usermode programs effectively cannot be
    run in some address spaces, so discarding some of the information
    before printing is not necessarily a problem, since the information
    can be rebuilt (if you aren't in usermode then you are in
    implementation territory where the promises of the standard library
    are not required to hold...)


    >I would suggest that you really
    >mean that %p may print an encrypted version of a pointer as long
    >as it can be decrypted on input.


    You could encrypt, sure, but I did mean hash. There is an overlap
    between the definition of "hash" and "encrypt".
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
    Walter Roberson, May 18, 2006
    #11
  12. Re: how to find out the address of a variable after I compile the Cfile

    -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > Nelu <> wrote:
    >
    >>If you want to display the adress of a variable from the program you
    >>can use %p for printf format,

    >
    > %p merely promises a reversible representation, not an address
    > or anything necessarily meaningful to humans. It would be valid
    > for %p to hash the internal address before printing it out, as long
    > as the scanner knows how to reverse the hash upon input.
    > The %p output could even be the uuencoding of the instruction
    > sequence that would be needed to be executed to recreate the pointer value.


    Sure, a sufficiently perverse implementation could do something like
    that. But realistically, implementers are motivated both to make "%p"
    useful, and to avoid the extra work needed to make it less that
    useful.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 18, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. UJ
    Replies:
    2
    Views:
    706
  2. UJ
    Replies:
    4
    Views:
    686
    John Rivers
    Sep 2, 2005
  3. =?Utf-8?B?QW5kcmV3?=

    Can we find out IP address?

    =?Utf-8?B?QW5kcmV3?=, Mar 17, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    641
    =?Utf-8?B?RWx0b24gVw==?=
    Mar 18, 2006
  4. Daniel Czajko
    Replies:
    3
    Views:
    13,428
    kesahey
    Oct 21, 2009
  5. Nagaraj
    Replies:
    1
    Views:
    822
    Lionel B
    Mar 1, 2007
Loading...

Share This Page