473,385 Members | 1,647 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,385 software developers and data experts.

Memory address and Pointer

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~~
Nov 30 '07 #1
5 2388
erfan wrote:
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.

Nov 30 '07 #2
erfan wrote:
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/>

Nov 30 '07 #3
erfan <zh*******@gmail.comwrites:
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;
}

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 30 '07 #4
erfan wrote:
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!

Nov 30 '07 #5
On Thu, 29 Nov 2007 21:55:33 -0800 (PST), erfan <zh*******@gmail.com>
wrote:
>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
Dec 2 '07 #6

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

Similar topics

32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
20
by: sethukr | last post by:
hi, i need to store a value to a particular memory location without having a variable. So, how can i access a 'memory address' without using variables?? Is it possible in C??? Plz, help...
3
by: garyusenet | last post by:
Some time ago I enquired about how I interface with a program written in an old version of C++ Any terms i use like list that follow are used in their common everyday usuage! One of the...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
41
by: simonl | last post by:
Hi, I've been given the job of sorting out a crash in our product for which we have the crash information and an avi of the event (which can't possibly match but more of that later...) (btw this...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.