364,036 Members | 5027 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

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

bijayadipti@gmail.com
P: n/a
bijayadipti@gmail.com
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 '06 #1
Share this Question
Share on Google+
11 Replies


Ian Collins
P: n/a
Ian Collins
bijayadipti@gmail.com wrote:[color=blue]
> 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.
>[/color]
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.
May 18 '06 #2

pete
P: n/a
pete
bijayadipti@gmail.com wrote:[color=blue]
>
> 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.[/color]

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
May 18 '06 #3

Nelu
P: n/a
Nelu

bijayadipti@gmail.com wrote:[color=blue]
> 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.[/color]

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

May 18 '06 #4

Keith Thompson
P: n/a
Keith Thompson
"Nelu" <tandauioan@gmail.com> writes:[color=blue]
> bijayadipti@gmail.com wrote:[color=green]
>> 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.[/color]
>
> 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.[/color]

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) kst-u@mib.org <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.
May 18 '06 #5

Nelu
P: n/a
Nelu

Keith Thompson wrote:[color=blue]
> "Nelu" <tandauioan@gmail.com> writes:[/color]
<snip>[color=blue][color=green]
> > 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.[/color]
>
> 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);[/color]

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

May 18 '06 #6

Stephen Sprunk
P: n/a
Stephen Sprunk
"pete" <pfiland@mindspring.com> wrote in message
news:446BC3FE.3BB4@mindspring.com...[color=blue]
> bijayadipti@gmail.com wrote:[color=green]
>> 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.[/color]
>
> 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.[/color]

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 ***
May 18 '06 #7

Walter Roberson
P: n/a
Walter Roberson
In article <1147924751.175341.216340@i40g2000cwc.googlegroups .com>,
Nelu <tandauioan@gmail.com> wrote:
[color=blue]
>If you want to display the adress of a variable from the program you
>can use %p for printf format,[/color]

%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
May 18 '06 #8

Ben Pfaff
P: n/a
Ben Pfaff
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[color=blue]
> %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.[/color]

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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
May 18 '06 #9

Jordan Abel
P: n/a
Jordan Abel
On 2006-05-18, Ben Pfaff <blp@cs.stanford.edu> wrote:[color=blue]
> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>[color=green]
>> %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.[/color]
>
> 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.[/color]

He could mean it places it in a hash table and prints out the
(arbitrary) key.
May 18 '06 #10

Walter Roberson
P: n/a
Walter Roberson
In article <87u07nb9a0.fsf@benpfaff.org>,
Ben Pfaff <blp@cs.stanford.edu> wrote:[color=blue]
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:[/color]
[color=blue][color=green]
>> %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.[/color][/color]
[color=blue]
>That uses a funny definition of "hash". In my experience, a hash
>loses information irreversibly.[/color]

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

[color=blue]
>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.[/color]

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
May 18 '06 #11

Keith Thompson
P: n/a
Keith Thompson
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:[color=blue]
> In article <1147924751.175341.216340@i40g2000cwc.googlegroups .com>,
> Nelu <tandauioan@gmail.com> wrote:
>[color=green]
>>If you want to display the adress of a variable from the program you
>>can use %p for printf format,[/color]
>
> %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.[/color]

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) kst-u@mib.org <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.
May 18 '06 #12

Post your reply

Help answer this question



Didn't find the answer to your C / C++ question?

You can also browse similar questions: C / C++