473,569 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2397
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*******@gmai l.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_Keit h) <ks***@mib.or g>
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*******@gmai l.com>
wrote:
>vc++6.0,winx p.
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
3806
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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my...
30
3702
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 = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
72
3556
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
2760
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: "At a certain point in the code you may be unsure if a particular block is no longer needed. If you free() this piece of memory, but continue to...
62
17674
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(); img.src = ; Then I use the image a few more times by adding it into an Array object:
26
3033
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on...
20
2842
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 me..
3
1568
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 programmes features is that it displays a list. The contents of this list are the names of people that are logged into the programme.
11
3337
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 ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this...
41
2810
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 is a single threaded VC9 / win32 app) The call stack for the bug effectively goes void* myBuf;
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.