473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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
11 1801
bi*********@gmail.com 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.
May 18 '06 #2
bi*********@gmail.com 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
May 18 '06 #3

bi*********@gmail.com 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...)

May 18 '06 #4
"Nelu" <ta********@gmail.com> writes:
bi*********@gmail.com 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) ks***@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

Keith Thompson wrote:
"Nelu" <ta********@gmail.com> 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...)

May 18 '06 #6
"pete" <pf*****@mindspring.com> wrote in message
news:44***********@mindspring.com...
bi*********@gmail.com 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 ***
May 18 '06 #7
In article <11**********************@i40g2000cwc.googlegroups .com>,
Nelu <ta********@gmail.com> 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
May 18 '06 #8
ro******@ibd.nrc-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[]="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
On 2006-05-18, Ben Pfaff <bl*@cs.stanford.edu> wrote:
ro******@ibd.nrc-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.
May 18 '06 #10
In article <87************@benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.edu> wrote:
ro******@ibd.nrc-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
May 18 '06 #11
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11**********************@i40g2000cwc.googlegroups .com>,
Nelu <ta********@gmail.com> 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) ks***@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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Desi Haker | last post by:
Hi there, I am trying to compile free C code from internet using cl(MSVC 7.1). This program use graphics.h header file. Unfortunatly this file is not standard C file. So, can any one help to...
27
by: Adam Warner | last post by:
Hi all, In the code snippet below I successfully determine the address of val1:* struct o val1=l_SYM_2B(&a).o; print_aesthetic(&val1); The structure o is heavyweight. I understand...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
4
by: Frank Rizzo | last post by:
I have a batch script that is supposed to compile this script: vbc.exe /main:Form1 /target:winexe...
18
by: anand | last post by:
*********************************************************************************************************** #include<stdio.h> #include<conio.h> #include<math.h> void main() { double...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
24
by: arnuld | last post by:
I want to create a new array of the same size of an array already available: #include <stdio.h> #include <string.h> int main(void) { char arrc = "URI";
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.