Re: pointer q




"Flash Gordon" <spam@xxxxxxxxxxxxxxxxxx>
Joe Smith wrote:
"Flash Gordon" <spam@xxxxxxxxxxxxxxxxxx>
Joe Smith wrote:
[snip]
*sp = (short)*lp; /* ill-advised, but Let's Pretend */

This is very different. If you were trying to fix the code further up so
it would compile what you wanted was:
short *sp = (short*)*lp; /* ill-advised, but Let's Pretend */
How thick is my head ^ ?

We all make mistakes.
See below.

Am I correct to think that a type of masking occurs here? joe
No, 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.
Am I not reading, with correction, the number that a smaller type thinks
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 */
It's a little hard to discern what is a mistake when the premise is that
what we're doing is ill-advised or illegal. I do think the above line is
miscast.


/* Use of *sp */

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.
I think if a fellow were serious, ie, trying not to do illegal or
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) for
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.
Writing to something unitialized is going to be trouble every day of the
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 point
that the C standard does not care what happens, so anything your compiler
does, including something completely unexpected
Easy for you to say! Mr Knudds ate my reuben on rye.

[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


.



Relevant Pages

  • Re: Unformatted, big-endian files and fseek
    ... as it is still a compiler used by several. ... F2003 I/O will improve the reading speed versus reading the whole ... most of them have the functionality in one form or other. ... I/O syntax (I think, for example, that the latest version of the Intel ...
    (comp.lang.fortran)
  • Re: msvcrt fflush bug?
    ... >read/write mode you are supposed to either flush or seek the file when ... I spun my brain reading the standard for a while, ... depending on your specific compiler settings. ... You should post standard C questions where standard C experts know ...
    (microsoft.public.vc.language)
  • Re: Pointer Casting
    ... Nope it is trully what I write, that is put in ESI the value 0xB8000. ... This allow your compiler to ... Thanks for your advice. ... but having a feeling of incompleteness after reading this ...
    (alt.lang.asm)
  • Re: Sizes of Integer Types
    ... If your C99 implementation has a type of the kind you want, ... unsigned type at least 32 bits long, and more importantly, I'd know the ... So now I use a uint32_t and my code fails on compiler X because the type ... the point to writing portable code, that you don't _care_ what compiler it ...
    (comp.lang.c)
  • Re: How to avoid undesired default values?
    ... nospam@xxxxxxxxxxxxx (Richard Maine) writes: ... stating as apparently absolute fact that this isn't what the OP meant. ... My reading skills are apparently not up to the level of yours... ... The OP did not ask "Is there an option for the compiler" ...
    (comp.lang.fortran)