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

B

bijayadipti

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
 
I

Ian Collins

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

pete

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 */
 
N

Nelu

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

Keith Thompson

Nelu said:
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);
 
N

Nelu

Keith said:
"Nelu" <[email protected]> writes:

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

Stephen Sprunk

pete said:
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


*** ***
 
W

Walter Roberson

Nelu said:
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.
 
B

Ben Pfaff

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

Jordan Abel

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

Walter Roberson

(e-mail address removed)-cnrc.gc.ca (Walter Roberson) writes:
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".
 
K

Keith Thompson

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top