Beginner's question

W

Wong

Hi gurus,

I have some codes like this:

char whatIsThis;
*(int *)whatIsThis = SOME_PARAMETERS; // SOME_PARAMETERS is a value

For the second line, is this a type casting or pointer to pointer ?
Thanks in advance for your feedback.
 
I

infobahn

Wong said:
Hi gurus,

I have some codes like this:

char whatIsThis;
*(int *)whatIsThis = SOME_PARAMETERS; // SOME_PARAMETERS is a value

For the second line, is this a type casting or pointer to pointer ?

whatIsThis is a char.

(int *) is a cast, which attempts to interpret whatIsThis as if
it were a pointer. Even if whatIsThis were a pointer, which it
isn't, it doesn't point anywhere.

The behaviour of the code is undefined.
 
D

Default User

Wong said:
Hi gurus,

I have some codes like this:

char whatIsThis;
*(int *)whatIsThis = SOME_PARAMETERS; // SOME_PARAMETERS is a
value

For the second line, is this a type casting or pointer to pointer ?
Thanks in advance for your feedback.

What are trying to do?




Brian
#! rnews 1489
Xref: xyzzy rec.music.makers.percussion:464892
Newsgroups: rec.music.makers.percussion
Path: xyzzy!nntp
From: JaKe <[email protected]>
Subject: Re: A good laugh from a student
X-Nntp-Posting-Host: e274993.nw.nos.boeing.com
Content-Type: text/plain; charset=us-ascii
Message-ID: <[email protected]>
Sender: (e-mail address removed) (Boeing NNTP News Access)
Content-Transfer-Encoding: 7bit
Organization: none
X-Accept-Language: en
References: <[email protected]> <[email protected]> <[email protected]>
Mime-Version: 1.0
Date: Fri, 25 Feb 2005 18:00:35 GMT
X-Mailer: Mozilla 4.79 [en]C-CCK-MCD Boeing Kit (Windows NT 5.0; U)
source: http://www.scathe.demon.co.uk/name.htm

Origins of the term "Goth"

Who the hell said The Doors music was gothic rock? That's BS.
 
K

Keith Thompson

Hi gurus,

I have some codes like this:

char whatIsThis;
*(int *)whatIsThis = SOME_PARAMETERS; // SOME_PARAMETERS is a value

For the second line, is this a type casting or pointer to pointer ?
Thanks in advance for your feedback.

Are you sure that's what the code looks like? If the first line were

char *whatIsThis;

it might make some sense.

The code you've shown converts the char value of whatIsThis (promoted
to int) to a pointer-to-int. It's vanishingly unlikely that the
result is meaningful. It then assigns the value SOME_PARAMETERS to
whatever this pointer points to. This invokes undefined behavior, and
will most likely crash your program (if you're lucky).

If whatIsThis were declared as a char*, this would probably be an
attempt to use char* as a generic pointer type. This can work (if it
points to a chunk of memory of appropriate size and alignment), but
it's probably better to use void* as a generic pointer type.
 
W

Wong

Keith Thompson said:
Are you sure that's what the code looks like? If the first line were

char *whatIsThis;

it might make some sense.

The code you've shown converts the char value of whatIsThis (promoted
to int) to a pointer-to-int. It's vanishingly unlikely that the
result is meaningful. It then assigns the value SOME_PARAMETERS to
whatever this pointer points to. This invokes undefined behavior, and
will most likely crash your program (if you're lucky).

If whatIsThis were declared as a char*, this would probably be an
attempt to use char* as a generic pointer type. This can work (if it
points to a chunk of memory of appropriate size and alignment), but
it's probably better to use void* as a generic pointer type.

OK. To ease the pain. Let's define this,

*(int *) 0x0100 = SOME_PARAMETERs; // SOME_PARAMETERS is a value

What's now ?
 
M

Mark McIntyre

OK. To ease the pain. Let's define this,

*(int *) 0x0100 = SOME_PARAMETERs; // SOME_PARAMETERS is a value

What's now ?

Now you have a bunch of illegal code. You're making a pointer out of an
integer. Pointers aren't integers, so don't do that.

<OT>
On some older operating systems where users were allowed to directly access
hardware, this might have worked. Today it would most likely just cause
your programme to terminate on any modern OS. If you wnat to know more, ask
in a DOS programming group.
</OT>
 
W

Walter Roberson

:On 27 Feb 2005 02:19:12 -0800, in comp.lang.c , (e-mail address removed)
:(Wong) wrote:

:>OK. To ease the pain. Let's define this,

:> *(int *) 0x0100 = SOME_PARAMETERs; // SOME_PARAMETERS is a value

:Now you have a bunch of illegal code. You're making a pointer out of an
:integer. Pointers aren't integers, so don't do that.

I'm going by memory here, but if I recall correctly,
casting an integral value to a void pointer is not illegal: instead,
if memory servers, it has "implimentation defined results".

I seem to recall that K&R used to guarantee that a void* could be
converted to an unsigned long and back again, but that direct conversion
to/from any other kind of pointer was not part of the language specs.

To this end, note that printf("%p") and scanf("%p") are defined in
the standard as having implimentation defined interpretation but that,

If the input item is a value converted
earlier during the same program execution, the pointer that results
shall compare equal to that value.

The point here being that the barrier between numbers and pointers
is at least slightly porous, rather than the conversion being
strictly illegal as implied by your reply.

To be conforming [but with implimentation defined behaviour] I
believe the code would have to be written as

*(int *)(void *) 0x0100 = SOME_PARAMETERs;


Certainly this could be expected to memory fault in any modern
multi-user "hosted" implimentation... but back in the PDP RT11/RSX11M
and Motorolal 68000 days, it would have done something useful
[i.e., set up a trap vector.] I seem to recall also having encountered
microprocessors that did console UART I/O at something pretty close
to that address location; I've forgotten now which architecture that was.
 
D

DHOLLINGSWORTH2

Wong said:
OK. To ease the pain. Let's define this,

*(int *) 0x0100 = SOME_PARAMETERs; // SOME_PARAMETERS is a value

What's now ?

One quick question. What happens when you pull the Cast out? (int *) ?

Your now trying to multiply an immediate value. This isn't good .
you can, and I have, Not just in dos, done this:

(int *) 0x0100 = SOME_VALUE;

and this :

int * some_index = (int *) 0x100;
some_index[0] = SOME_VALUE;

I hope this helps.
 
W

Walter Roberson

::> *(int *) 0x0100 = SOME_PARAMETERs; // SOME_PARAMETERS is a value

:One quick question. What happens when you pull the Cast out? (int *) ?

:Your now trying to multiply an immediate value.

No you aren't. It's a unary * and that does not mean multiply.
 
J

Joe Wright

DHOLLINGSWORTH2 wrote:
[ snip ]
One quick question. What happens when you pull the Cast out? (int *) ?

Your now trying to multiply an immediate value. This isn't good .
you can, and I have, Not just in dos, done this:

(int *) 0x0100 = SOME_VALUE;

and this :

int * some_index = (int *) 0x100;
some_index[0] = SOME_VALUE;

I hope this helps.

No you haven't. The first cannot work because (int *) 0x0100 is not an
lvalue and cannot be assigned to. That the second works on your
implementation (and mine) is just dumb luck. Bad luck probably.

The memory at 0x0100 does not belong to us and we have no idea how it's
being used. Assigning an arbitrary value to it may well cause the end of
life on Earth as we know it, or do nothing or anything at all.

Classic Undefined Behavior.
 
D

DHOLLINGSWORTH2

Joe Wright said:
DHOLLINGSWORTH2 wrote:
[ snip ]
One quick question. What happens when you pull the Cast out? (int *) ?

Your now trying to multiply an immediate value. This isn't good .
you can, and I have, Not just in dos, done this:

(int *) 0x0100 = SOME_VALUE;

and this :

int * some_index = (int *) 0x100;
some_index[0] = SOME_VALUE;

I hope this helps.

No you haven't. The first cannot work because (int *) 0x0100 is not an
lvalue and cannot be assigned to. That the second works on your
implementation (and mine) is just dumb luck. Bad luck probably.

The memory at 0x0100 does not belong to us and we have no idea how it's
being used. Assigning an arbitrary value to it may well cause the end of
life on Earth as we know it, or do nothing or anything at all.

Classic Undefined Behavior.

your correct:
(int *) 0x0100 = SOME_VALUE;
doesn't work, becuase x0100 is an imediate value and cannot be assigned
anything.
What I meant to say was :
(int *) 0x0100[0] = SOME_VALUE;

And this does work. But as joe points out, it is not a good practice to get
into.

Dan
 
T

Thomas Matthews

Mark said:
Now you have a bunch of illegal code. You're making a pointer out of an
integer. Pointers aren't integers, so don't do that.

<OT>
On some older operating systems where users were allowed to directly access
hardware, this might have worked. Today it would most likely just cause
your programme to terminate on any modern OS. If you wnat to know more, ask
in a DOS programming group.
</OT>

Actually, it is legal and used in embedded environments
to write to devices:
*((int *) 0x10000) = 0x25; /* write 0x25 to address 0x10000 */


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
T

Thomas Matthews

Wong said:
OK. To ease the pain. Let's define this,

*(int *) 0x0100 = SOME_PARAMETERs; // SOME_PARAMETERS is a value

What's now ?

This statement is writing the value SOME_PARAMETERs
to the address 0x0100, treating the value at
the address as an integer.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
K

Keith Thompson

DHOLLINGSWORTH2 said:
your correct:
(int *) 0x0100 = SOME_VALUE;
doesn't work, becuase x0100 is an imediate value and cannot be assigned
anything.
What I meant to say was :
(int *) 0x0100[0] = SOME_VALUE;

And this does work. But as joe points out, it is not a good practice to get
into.

What do you mean it works? It's equivalent to

(int*) (0x0100[0]) = SOME_VALUE;

You can't index an integer literal, and you can't assign to a cast.

This:

((int *) 0x0100)[0] = SOME_VALUE;

is legal (but, again, almost certainly meaningless).
 
M

Mark McIntyre

I'm going by memory here, but if I recall correctly,
casting an integral value to a void pointer is not illegal: instead,
if memory servers, it has "implimentation defined results".

You're technically right (6.3.2.3 p6) but since the result might be
misaligned, not point to the right type of thing, or be a trap, its
essentially a bad idea. On any modern OS, as my reply went on to note, it
would almost certainly trap in some way.
I seem to recall that K&R used to guarantee that a void* could be
converted to an unsigned long and back again, but that direct conversion
to/from any other kind of pointer was not part of the language specs.

Its worth remembering that what K&R guaranteed may or may not have made it
into ISO C.
 
W

Walter Roberson

:>I seem to recall that K&R used to guarantee that a void* could be
:>converted to an unsigned long and back again, but that direct conversion
:>to/from any other kind of pointer was not part of the language specs.

:Its worth remembering that what K&R guaranteed may or may not have made it
:into ISO C.

Ah my memory isn't completely shot: K&R2 A6.6 does guarantee that a pointer
converted to a sufficiently-wide integer may be converted back again
to the same pointer.

Just as you hinted, C89 does not appear to offer the same guarantee:
it allows conversion pointers to numbers, and numbers to pointers,
with "undefined" or "implimentation-defined" results; the section on
cast operators does not promise that going back and forth will result
in the same pointer.
 

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

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top