Altair emulator code & C pointers
> > Mem[i+addr] = *((uchar *)(pstrBuffer+i));
> >
> > or even:
> >
> > Mem[i+addr] = ((uchar *)pstrBuffer)[i];
>
> This is not correct, the pstrBuffer element you are referring to is not a
> pointer but an uchar.
What? In the context of his example, pstrBuffer is a pointer to a signed
character. The trick is to cast that pointer to be a pointer to an unsigned
character. Then you can dereference the pointer using either the asterix
or the array index.
> The correct line should be:
>
> Mem[addr+i] = (uchar) *(pstrBuffer+i);
This will result in exactly the same potentially ambiguous behavior as his
original example. The compiler will probably deref the pointer into a
signed int with the twos complement sign bit extended into the higher bits.
Then the fun begins. If it takes the signed int and (logically) converts it
into an unsigned int and then tries to convert that to an unsigned char,
you potentially wind up with a significant overflow. The behavior at this
point is not well defined. You might get the desired answer, you might not.
Depends on who implemented the compiler, what the target architecture is,
etc.
Received on Mon Jun 12 2000 - 12:36:35 BST
This archive was generated by hypermail 2.3.0
: Fri Oct 10 2014 - 23:33:01 BST