Re: Use of memcpy() to transfer from memory to a variable



Richard Heathfield writes:
Martin said:
For reasons I won't go into, I need to transfer from 1 to 3 bytes to a
variable that I know is 4 bytes long. Bytes not written to in the
4-byte target variable must be zero. Is the following use of memcpy()
a well-defined way of so doing?

No, but it's not exactly undefined either.

It is if it produces a trap representation in the 'long' variable. Due
to either padding bits in the long type, or on a sign/magnitude host
where it can produce negative zero, which can be a trap representation.

Not exactly common, but it's possible. And if you use unsigned long
instead, it's possible to trap it at compile time by checking that
ULONG_MAX uses all the bits in a long.

if ( rem )
{
value = 0;
memcpy( &value, pAddress, rem );
}

/* for demo - shows values in pAddress have been transferred */
printf("value = 0x%0lX\n", value);

Note that you need to look out for C's aliasing rules in code like that.
I _think_ it's safe when pAddress holds a malloced char array, but
otherwise the compiler would be allowed to "know" that pAddress does not
hold a long and 'value' thus is not set to a long after 'value = 0;'.
Then it could optimize away the memcpy, since it can tell the
destination value is not used (validly).

Compilers get smarter all the time, I remember a recent comment from the
gcc guys about some other hack: "We are working on a feature which will
break your hack."

Sigh. I guess we'll have to throw away hash functions which read
aligned data in larger chunks than byte by byte soon... Or maybe it
would help to declare the input values volatile.

--
Regards,
Hallvard
.



Relevant Pages

  • Re: D3/Linux compile question
    ... 02 equ a to x ... Variable has not been assigned a value; zero used. ... Based on the actual compile I'm not sure. ... base frame not 1 past the base frame. ...
    (comp.databases.pick)
  • Re: Two problems
    ... uninitialised variables that I really should be clearing to zero ... I don't need a statement clearing the variable ... In one case they are set to zero or some other ... enabled and turn on all of the compile time warnings you can. ...
    (comp.lang.fortran)
  • Re: Two problems
    ... uninitialised variables that I really should be clearing to zero ... I don't need a statement clearing the variable ... In one case they are set to zero or some other ... enabled and turn on all of the compile time warnings you can. ...
    (comp.lang.fortran)
  • Re: Division by zero
    ... when Y is a static expression equal to 0. ... The reason is that it simplifies the language definition. ... - always detect this error at compile time ... For division by zero, it is believed to be too restrictive to detect it ...
    (comp.lang.ada)
  • Re: Help for a FORTRAN N00b
    ... Will it just report back zero? ... Examples might be "unable to compile with ... gfortran for windows, put this source file in the bin with gfortran.exe, and ...
    (comp.lang.fortran)

Loading