|
From: John R. <jr...@bi...> - 2013-02-11 01:06:13
|
On 02/04/2013, Patrick J. LoPresti wrote:
> Finally, as long as I am on my soap box... In my humble opinion, it
> is generally a bad idea to use memcmp(), memcpy(), or memset() on
> anything other than actual byte arrays. To zero-initialize a
> structure, instead of using memset(), just use:
>
> struct HReg foo = { };
>
> This is shorter to write, easier to read, and it will properly
> initialize floats/doubles/pointers, even on platforms that do not use
> all-zero as the byte representation for 0.0 or NULL.
>
> To reinitialize a structure to zero:
>
> static const struct HReg empty = { };
> struct HReg foo;
> ...
> foo = empty;
>
> Any compiler worth its salt will generate optimal code for these.
How do you recommend to (re-)initialize an array of structs to zero,
where the length of the array varies at runtime?
Which platforms do not use all-zero as the representation of 0.0 ?
In IEEE 794, -0.0 uses 0x800...; but -0.0 causes various trouble.
The last instance I know where memset(,0,) did not work was the Transputer,
which had signed pointers and used 0x80000000 as NULL. The 4KB of
on-chip RAM was 0x80000000 to 0x80001000.
The Transputer died in the early 1990s.
--
|