473,750 Members | 5,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory address "value"

mdh
Quick question about pointers.
Wrote this "trying to understand this better" code:

int *ptr, x = 565;

ptr= &x;

printf("\n\n\nT he value of x is %d\n", x);
printf("Now: ptr= &x\n So:");
printf("The value of *ptr is %d\n", *ptr);
printf("And, straight ptr should give an address of \"x\"\n ");
printf("Ptr points at x whose address is is %d\n", ptr); /*<<<<<<<<
returns -1073743224<<<*/
return 0;
}
All except the last line work...or is the memory location indeed
"-1073743224"

Thanks in advance.

Nov 13 '06 #1
10 2191
In <11************ **********@h48g 2000cwc.googleg roups.com"mdh" <md**@comcast.n etwrites:
printf("Ptr points at x whose address is is %d\n", ptr); /*<<<<<<<<
All except the last line work...or is the memory location indeed
"-1073743224"
When you use %d, you're telling printf that it's printing an integer.
But you did not supply an integer argument; you used a pointer instead,
which Ain't The Same Thing.

Use %p to print pointer values. (But only void pointers, so you'll
have to cast to void first.)

--
John Gordon "... What with you being his parents and all,
go****@panix.co m I think that you could be trusted not to shaft
him." -- Robert Chang, rec.games.board

Nov 13 '06 #2
mdh wrote:
>
Quick question about pointers.

Wrote this "trying to understand this better" code:

int *ptr, x = 565;

ptr= &x;
[...]
printf("Ptr points at x whose address is is %d\n", ptr); /*<<<<<<<<
returns -1073743224<<<*/
[...]
All except the last line work...or is the memory location indeed
"-1073743224"
It could be.

First, "%d" expects an int, and you have pased an int*, so all bets
are off. However, given that your post says you're on an Intel Mac,
and most of those systems probably have sizeof(int)==si zeof(int*)==4,
it's probably sort-of okay on your platform.

Next, "%d" interprets the value as a signed integer, which makes for
strange interpretations for half of the address space.

Finally, -1073743224 decimal is 0xBFFFFA88 hex (assuming a 32-bit
signed int), which is certainly a reasonable address, especially if
the particular implementation starts the stack growing down from
0xC0000000.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 13 '06 #3
mdh:
int *ptr, x = 565;

ptr= &x;

While we're saving space, we may aswell write:

int x = 565, *ptr = &x;

This also saves us mixing declarations with statements.

--

Frederick Gotham
Nov 13 '06 #4
mdh wrote:
Quick question about pointers.
Wrote this "trying to understand this better" code:

int *ptr, x = 565;
ptr= &x;
[...]
printf("Ptr points at x whose address is is %d\n", ptr); /*<<<<<<<<
returns -1073743224<<<*/
The specifier for a pointer is %p and its corresponding argument is a
void *:
printf("Ptr points at x whose address is %p\n",
(void *) ptr);
Nov 13 '06 #5
Kenneth Brody <ke******@spamc op.netwrites:
[...]
First, "%d" expects an int, and you have pased an int*, so all bets
are off. However, given that your post says you're on an Intel Mac,
and most of those systems probably have sizeof(int)==si zeof(int*)==4,
.... *and* pointer and integer arguments are probably passed using the
same mechanism ...p
it's probably sort-of okay on your platform.
Right. On some platforms, though, integer and pointer arguments can
be passed using different mechanisms, so the location that printf()
looks at in response to a "%d" format might have nothing at all to do
with the pointer value that you passed to it. (For example, the 68k
has separate integer and pointer CPU registers; I don't know whether
parameter passing mechanisms typically take advantage of this.)

If something is "probably sort-of okay on your platform", that's
usually a sign that you should fix it so it's "certainly for-sure okay
on all platforms". (That's not always possible, but it is in this
case.)

(I'm just emphasizing the point here, not disagreeing with anything
Kenneth wrote.)

--
Keith Thompson (The_Other_Keit h) 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.
Nov 13 '06 #6
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>Right. On some platforms, though, integer and pointer arguments can
be passed using different mechanisms, so the location that printf()
looks at in response to a "%d" format might have nothing at all to do
with the pointer value that you passed to it. (For example, the 68k
has separate integer and pointer CPU registers; I don't know whether
parameter passing mechanisms typically take advantage of this.)
That would depend upon the ABI for that implementation. Some do,
some don't.
--
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
Nov 13 '06 #7
Keith Thompson wrote:
>
Kenneth Brody <ke******@spamc op.netwrites:
[...]
First, "%d" expects an int, and you have pased an int*, so all bets
are off. However, given that your post says you're on an Intel Mac,
and most of those systems probably have sizeof(int)==si zeof(int*)==4,

... *and* pointer and integer arguments are probably passed using the
same mechanism ...p
it's probably sort-of okay on your platform.

Right. On some platforms, though, integer and pointer arguments can
be passed using different mechanisms, so the location that printf()
looks at in response to a "%d" format might have nothing at all to do
with the pointer value that you passed to it. (For example, the 68k
has separate integer and pointer CPU registers; I don't know whether
parameter passing mechanisms typically take advantage of this.)
I know of at least one 68K platform that _returns_ pointers in a
different place than numbers. If the function returns a pointer,
it uses the A0 register. Otherwise, it uses the D0 register. This
is a perfect example of why "I'll just cast the return of malloc()
to the right type to shut up that stupid warning" is a bad idea.
If you haven't included the proper header, and the compiler assumes
that malloc() returns int by default, it will look at D0 rather than
A0 for the return value, and get total garbage.

Now, this particular platform used the "normal" stack method of
passing parameters. However, I have seen platforms that pass the
first few parameters in registers (though there is only one set of
registers on those platforms), so it's certainly possible that there
is a platform which has more than one set of registers, and that
uses the registers to pass parameters, which would use different
registers depending on whether the parameter is a pointer or not.
If something is "probably sort-of okay on your platform", that's
usually a sign that you should fix it so it's "certainly for-sure okay
on all platforms". (That's not always possible, but it is in this
case.)
True.
(I'm just emphasizing the point here, not disagreeing with anything
Kenneth wrote.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Nov 15 '06 #8
2006-11-15 <45************ ***@spamcop.net >,
Kenneth Brody wrote:
Now, this particular platform used the "normal" stack method of
passing parameters. However, I have seen platforms that pass the
first few parameters in registers (though there is only one set of
registers on those platforms), so it's certainly possible that there
is a platform which has more than one set of registers, and that
uses the registers to pass parameters, which would use different
registers depending on whether the parameter is a pointer or not.
Even for platforms that do this, it seems unlikely they'd do it for
variadic functions. There's a reason they need a prototype in scope.
Nov 15 '06 #9
Random832 wrote:
>
2006-11-15 <45************ ***@spamcop.net >,
Kenneth Brody wrote:
Now, this particular platform used the "normal" stack method of
passing parameters. However, I have seen platforms that pass the
first few parameters in registers (though there is only one set of
registers on those platforms), so it's certainly possible that there
is a platform which has more than one set of registers, and that
uses the registers to pass parameters, which would use different
registers depending on whether the parameter is a pointer or not.

Even for platforms that do this, it seems unlikely they'd do it for
variadic functions. There's a reason they need a prototype in scope.
True.

Yet another "real world" example of how calling a varadic function
without a prototype in scope is UB. (In this case, the compiler will
generate code to put the first several [or maybe even all] of the
parameters into registers, but the function itself assumes that all
of the parameters are on the stack.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Nov 15 '06 #10

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

Similar topics

8
3709
by: c | last post by:
RH9, Apache 2.0.49, php 4.3.6 been away from php for about 2 years and this way used to work, still does in older php versions, now it doesn't. is there a reason for this? a work around? Thanx in Advance!!
5
2362
by: lvcha.gouqizi | last post by:
I embed an applet in my php script which has a parameter providing an input file for the jar. Does this file has size limit? The code is as follows: <APPLET ARCHIVE="myapplet.jar" WIDTH=372 HEIGHT=360 VSPACE=0 HSPACE=0 ALIGN=middle> <?php echo "<PARAM NAME=\"data\" VALUE=\"myfile.\">";?> when "myfile" is beyond 15M, my applet cannot load sucessfully, but
9
6510
by: Russ Perry Jr | last post by:
I'm using "ID" and "Value" in the generic sense here... Let's say one page I had a <html:select> with a collection like this: <html:options collection="items" property="key" labelProperty="value"/> In this case "key" is what I mean by "ID", and "value" is what I mean by "Value" -- the "Value" is shown to the user, but the "ID" is what I plan to keep, to be saved to a database (as a reference in a table to a common table as a part of...
10
21168
by: PB | last post by:
Hi ! I have the following code, which I am using in an Embedded systems, c-compiler.. However I see the same problem with GCC too.. I need the last 10 bits of an address pointer, which is completly know at link time, but will be symbols at the compile time. I used the following code for this initalization.. uint32 data_ptr_mask = ((uint32) data) & 0x3ff;
2
1883
by: Ken Schutte | last post by:
Lets say I want an integer class that lets you attach arbitrary attributes. I can simply do: class foo(int): pass x = foo(5) x.text = "okay" print x, x.text # prints "5 okay" So, that's good. But, how can I change the value of x from 5 to
21
2416
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the C++ Standard: "" "...sequence of operators and operands that specifies a computation...". That means to me that an expression can be "executed". I am purposely
7
2158
by: rynato | last post by:
Simple question, probably a simple answer: I have a php-based email form. There are three hidden variables passed from the initial page to the php script which handles sending the message: <input type="hidden" name="ip" value="<?php echo $ipi ?>" /> <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" /> <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?
1
7084
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form action="http://cm1web1/WebSurveyComponents/script/ processform.asp" method="post">
0
9004
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9260
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6818
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6086
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4718
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4896
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.