Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to find out the address of a variable after I compile the C file

Reply
Thread Tools

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

 
 
bijayadipti@gmail.com
Guest
Posts: n/a
 
      05-18-2006
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-18-2006
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.
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      05-18-2006
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
 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      05-18-2006

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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-18-2006
"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) kst- <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.
 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      05-18-2006

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

 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      05-18-2006
"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 ***
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      05-18-2006
In article < .com>,
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
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      05-18-2006
(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[]="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;}
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      05-18-2006
On 2006-05-18, Ben Pfaff <> wrote:
> (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
address array by variable variable mps@webmind.nl Javascript 9 07-20-2004 06:48 AM
How do I find out my 2950 ip address. Mark Cisco 3 12-31-2003 07:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57