Re: pointer q
- From: "Joe Smith" <grumpy196884@xxxxxxxxxxx>
- Date: Tue, 16 May 2006 12:22:05 -0400
"Flash Gordon" <spam@xxxxxxxxxxxxxxxxxx>
Joe Smith wrote:[snip]
"Flash Gordon" <spam@xxxxxxxxxxxxxxxxxx>
Joe Smith wrote:
*sp = (short)*lp; /* ill-advised, but Let's Pretend */
This is very different. If you were trying to fix the code further up soHow thick is my head ^ ?
it would compile what you wanted was:
short *sp = (short*)*lp; /* ill-advised, but Let's Pretend */
We all make mistakes.See below.
It's a little hard to discern what is a mistake when the premise is thatAm I not reading, with correction, the number that a smaller type thinksAm I correct to think that a type of masking occurs here? joeNo, not masking. More a case of only reading part of the data with my
"fix". Or with your code writing to an uninitialised pointer and causing
half of your socks to migrate to Alaska.
a larger type was? My socks are well accounted for, but Scott Knuds not
only flew out of my hard drive, but is eating my lunch.
Your version of the code:
#include <stdio.h>
int main ( void )
{long l = 420000;
long *lp = &l;
short *sp;
*sp = (short)*lp; /* ill-advised, but Let's Pretend */
Writes through an uninitialised pointer which is undefined behaviour.
My partial correction:
#include <stdio.h>
int main ( void )
{long l = 420000;
long *lp = &l;
short *sp = (short)*lp; /* ill-advised, but Let's Pretend */
what we're doing is ill-advised or illegal. I do think the above line is
miscast.
/* Use of *sp */I think if a fellow were serious, ie, trying not to do illegal or
Tries to read a short from where a long was stored. On normal 2s
complement systems the part of the long that is read will depend on the
endianness of the system.
The standard also says:
| An object shall have its stored value accessed only by an lvalue
| expression that has one of the following types:74)
| ? a type compatible with the effective type of the object,
| ? a qualified version of a type compatible with the effective type of
| the object,
| ? a type that is the signed or unsigned type corresponding to the
| effective type of the object,
| ? a type that is the signed or unsigned type corresponding to a
| qualified version of the effective type of the object,
| ? an aggregate or union type that includes one of the aforementioned
| types among its members (including, recursively, a member of a
| subaggregate or contained union), or
| ? a character type.
I.e. don't do what the above code does.
ill-advised things, and he wanted to "pick apart" something that the
compiler has stored as a long at an address accessible to the source, he
would go after it with the last of those options, unsigned chars, and make
the necessary conversions to make zeroes and ones.
It is also possibly in general (though unlikely in this specific case) forWriting to something unitialized is going to be trouble every day of the
short to have trap representations. If the part of the long that is read
as a short happens to be a trap representation for short (e.g. -0 on a 1s
complement system where -0 is a trap) then you have again invoked
undefined behaviour.
week, but again, I would say unsigned chars are the safest way to go.
The comments about ridiculous things happening are just to make the pointEasy for you to say! Mr Knudds ate my reuben on rye.
that the C standard does not care what happens, so anything your compiler
does, including something completely unexpected
[snip]
As to trolls, I don't know enough about the phenomenon to fill a sacrament
cup. I've witnessed enough dehumanizing to have no pallette for it.
There's something driving me bats right now. How does one complete this
table:
long * | short | long ***
-------------------------------------------------
type is | | |
----------------------------------
type-specifier is | | |
Thanks for your time. joe
.
- Follow-Ups:
- Re: pointer q
- From: CBFalconer
- Re: pointer q
- From: Flash Gordon
- Re: pointer q
- References:
- pointer q
- From: Joe Smith
- Re: pointer q
- From: Keith Thompson
- Re: pointer q
- From: Joe Smith
- Re: pointer q
- From: Eric Sosman
- Re: pointer q
- From: RSoIsCaIrLiIoA
- Re: pointer q
- From: Joe Smith
- Re: pointer q
- From: Flash Gordon
- Re: pointer q
- From: Joe Smith
- Re: pointer q
- From: Flash Gordon
- pointer q
- Prev by Date: Re: va_list: how to produce warnings at compile time if called incorrectly?
- Next by Date: Re: int fact( int n )
- Previous by thread: Re: pointer q
- Next by thread: Re: pointer q
- Index(es):
Relevant Pages
|