Memory address and Pointer

E

erfan

vc++6.0,winxp.
I use this code to see the memory address and the real value`s
address below:
#include<stdio.h>
int main()
{
int a=22;
int *p;
int i;
p=&a;
printf("%x\n",&a);
printf("%p\n",*p);
}
the result is:
12ff7c
00000016
Press any key to continue

the pointer p is pointing to a value which is 22,and the address of
the value is 0012ff7c.
i want to compare the memory address with the value address to see
wheather they are the same.
So ,next,i use win Debug to help me.
-d 0012:ff7c
but the output is 00 00 00 00 00 00 00 00 00......
what`s wrong? as far as i know,the value of a,must have it`s address
in the memory,and when i search the address,why there is nothing left
in it. there must be some mistake in my understanding,i really expect
your words~~
 
M

Martin Ambuhl

erfan said:
vc++6.0,winxp.
I use this code to see the memory address and the real value`s
address below:

Those terms have no meaning and your code is severely broken. Notice
the changes below:

#include<stdio.h>

int main(void)
{
int a = 22;
int *p;
p = &a;
#if 0
/* mha: the line below is an error. &a has type 'int *', but "%x"
expects the very different type 'unsigned int' */
printf("%x\n", &a);
#endif
printf("%p\n", (void *) &a); /* mha: replacement for the above
erroneous statement */
#if 0
/* mha: the line below is an error. *p has type 'int', but "%p"
expects the very different type 'void *' */
printf("%p\n", *p);
#endif
printf("%p\n", (void *) p); /* mha: replacement for the above
erroneous statement */
return 0;
}

[OP's code snipped, being essentially included above]


the pointer p is pointing to a value which is 22,and the address of
the value is 0012ff7c.
i want to compare the memory address with the value address to see
wheather they are the same.

Those terms still mean nothing.
 
S

santosh

erfan said:
vc++6.0,winxp.

To this group it shouldn't matter.
I use this code to see the memory address and the real value`s
address below:
#include<stdio.h>
int main()
{
int a=22;
int *p;
int i;
p=&a;
printf("%x\n",&a);

The 'x' format specifier expects an unsigned int argument. The
expression '&a' yields a value of type int *. Therefore there is a
mismatch which leads to undefined behaviour. To print a pointer value
use the 'p' format specifier which just for this purpose. It expects a
corresponding argument of type void *, so you need to cast the
appropriate argument.

printf("&a = %p\n", (void *)&a);
printf("%p\n",*p);

The expression '*p' yields a value of type int. The format specifier 'p'
expects a corresponding argument of type void *, so once again there is
a mismatch. Do:

printf("*p = %d\n", *p);
}
the result is:
12ff7c
00000016
Press any key to continue

the pointer p is pointing to a value which is 22,and the address of
the value is 0012ff7c.

'p' holds the address of an int object, in this case 'a', which, in
turn, contains a value that is 22 in base 10.
i want to compare the memory address with the value address to see
wheather they are the same.

Which memory address? And the phrase "value address" is totally
meaningless. Only objects have addresses, not values. For example:

int foo = 10;
int bar;

bar = foo + 10;

Now in the last statement both 'foo' and '10' resolve to a value, but
only 'foo' is an object and hence has an address (unless it was
declared as a register object). The expression '10' is simply a source
code literal which has (from C's point of view) no address. Note though
that a string literal _does_ yield an address.
So ,next,i use win Debug to help me.
-d 0012:ff7c
but the output is 00 00 00 00 00 00 00 00 00......
what`s wrong? as far as i know,the value of a,must have it`s address
in the memory,and when i search the address,why there is nothing left
in it. there must be some mistake in my understanding,i really expect
your words~~

Try the suggested changes and see. I think you are confused between
memory addresses and memory content and the involvement of pointers in
both. If you can be more clear with your questions, we can give you
more helpful answers. Also be sure to see the group's de facto FAQ at:

<http://www.c-faq.com/>
 
K

Keith Thompson

erfan said:
vc++6.0,winxp.
I use this code to see the memory address and the real value`s
address below:
#include<stdio.h>
int main()
{
int a=22;
int *p;
int i;
p=&a;
printf("%x\n",&a);
printf("%p\n",*p);
}
the result is:
12ff7c
00000016
Press any key to continue

You're using incorrect formats in both your printf calls. "%x" prints
an unsigned int in hexadecimal; you're passing it a pointer to int.
"%p" expects a pointer, specifically a void*; you're passing it an
int.
the pointer p is pointing to a value which is 22,and the address of
the value is 0012ff7c.

Pointers point to objects, not to values; objects have values.

p points to an *object* whose value is 22. The address of that object
happens to be 0012ff7c (when displayed in hexadecimal).
i want to compare the memory address with the value address to see
wheather they are the same.

I don't know what you mean by "memory address" vs. "value address".
So ,next,i use win Debug to help me.
-d 0012:ff7c
but the output is 00 00 00 00 00 00 00 00 00......
what`s wrong? as far as i know,the value of a,must have it`s address
in the memory,and when i search the address,why there is nothing left
in it. there must be some mistake in my understanding,i really expect
your words~~

I've never used win Debug, so I can't help you with that.

Here's a corrected and expanded version of your program. I'm not sure
what you're asking, but perhaps this will help you answer it anyway.

#include <stdio.h>
int main(void)
{
int a = 22;
int *p = &a;
printf("a = %d\n", a);
printf("&a = %p\n", (void*)&a);
printf("p = %p\n", (void*)p);
printf("*p = %d\n", *p);
return 0;
}
 
M

Mark McIntyre

erfan said:
vc++6.0,winxp.
I use this code to see the memory address and the real value`s
address below:
#include<stdio.h>
int main()
{
int a=22;
int *p;
int i;
p=&a;
printf("%x\n",&a);
printf("%p\n",*p);
}
the result is:
12ff7c
So ,next,i use win Debug to help me.
-d 0012:ff7c
but the output is 00 00 00 00 00 00 00 00 00......

ignoring the errors in printf which others have commented on, the above
isn't likely to work anyway. In most modern OSes, programmes have their
own memory space and another application normally can't invade that.
Also the 'debug' app you're using seems to be a 16-bit app, and the
address format you're supplying looks very much like a 16-bit format.
Neither of these will be valid ways of accessing data in a 32-bit
programme in different memory space!
 
E

erfan

ignoring the errors in printf which others have commented on, the above
isn't likely to work anyway. In most modern OSes, programmes have their
own memory space and another application normally can't invade that.
Also the 'debug' app you're using seems to be a 16-bit app, and the
address format you're supplying looks very much like a 16-bit format.
Neither of these will be valid ways of accessing data in a 32-bit
programme in different memory space!- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

---------------------------
yeah,thank you all for helping.i am new here and happy to see that all
of you are warm hearted.
However,when i write a code ,and during the running of the code,as we
all know,the date is in memory to be deal with. i used pointer in the
code in order to gain the address to be checked. So may i see the
real value in the memory during that short time? if yes,how?
i am reading the Princisple of PC,so i just want to make it clear
when my code run,how does the registers get to work,and how does the
assmbeling tools work,such as the data goes from one register to
another. expecting your reply
 
S

santosh

erfan wrote:

---------------------------

*Please* snip unneccessary material like the signature above.
yeah,thank you all for helping.i am new here and happy to see that all
of you are warm hearted.
However,when i write a code ,and during the running of the code,as we
all know,the date is in memory to be deal with. i used pointer in the
code in order to gain the address to be checked. So may i see the
real value in the memory during that short time? if yes,how?
i am reading the Princisple of PC,so i just want to make it clear
when my code run,how does the registers get to work,and how does the
assmbeling tools work,such as the data goes from one register to
another. expecting your reply

My recommendation would be to select a good book on PC architecture and
x86 assembler programming and read it. They tend to go into such
details which Standard C is not concerned about and hence this is not
the right group to ask in.

Randall Hyde's Art of Assembler and his Write Great Code books may be
helpful. The former is freely available online and uses his HLA
language.

<http://webster.cs.ucr.edu/>
<http://webster.cs.ucr.edu/WriteGreatCode/index.html>
 
B

Barry Schwarz

vc++6.0,winxp.
I use this code to see the memory address and the real value`s
address below:
#include<stdio.h>
int main()
{
int a=22;
int *p;
int i;
p=&a;
printf("%x\n",&a);

This invokes undefined behavior. %x expects an unsigned int. If you
want to print an address, use %p and cast the address to void*.
printf("%p\n",*p);

And again. If you want to print the hex representation of an int, use
%x and cast the value to unsigned.
}
the result is:
12ff7c
00000016
Press any key to continue

Where did this line come from? It is not in your code.
the pointer p is pointing to a value which is 22,and the address of
the value is 0012ff7c.
i want to compare the memory address with the value address to see
wheather they are the same.

Then use
printf("&a=%p, p=%p\n", (void*)&a, (void*)p);
So ,next,i use win Debug to help me.

Questions about specific tools need to be asked in newsgroups that
deal with those tools.
-d 0012:ff7c

This does not look like the same address. Where did the colon come
from?
but the output is 00 00 00 00 00 00 00 00 00......
what`s wrong? as far as i know,the value of a,must have it`s address
in the memory,and when i search the address,why there is nothing left
in it. there must be some mistake in my understanding,i really expect
your words~~


Remove del for email
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top