struct initialization, pointers, doubles

Hello everyone,

Are the following snippets well-defined

unsigned u;
memset(&u, 0, sizeof u); /* now u == 0 */


int i;
memset(&i, 0, sizeof i); /* now i == 0 */

Is the answer the same in C90 and C99?

AFAIU, on some (many) platforms, the representation of NULL
and 0.0 is all-bits 0, but there is no such guarantee.


struct foo { int i; void *p; double d; };

struct foo bar = { 0 };

On a platform where NULL and 0.0 are all-bits 0, the compiler
is free to change the statement to

memset(&bar, 0, sizeof bar);

But if that were not the case, the compiler would have to
output the machine-code equivalent of

bar.i = 0;
bar.p = NULL;
bar.d = 0.0;

(which might be much slower if the struct holds e.g. arrays of
pointers and doubles).

Is my understanding correct?


Relevant Pages

  • Re: "Sorting" assignment
    ... issue on some ancient compiler doesn't make a lot of sense. ... to his on a few commonly used platforms and compilers, ...  Be sure and call the swap ... reason to find algorithms which operate independent of it. ...
  • Re: Noob question about the >> operator
    ... Better (using 'int' rather than the platform-dependent 'int32_t'): ... I aim for the code to work on different platforms ... it is only as small and fast when optimisation is enabled (not ... their part, but it is still true), and with a good compiler (I have used ...
  • Re: Death of Kylix and migrating towards Java
    ... But it can't beat Delphi's compiler speed! ... app developers on non-Windows platforms. ... It is NOT Delphi. ... > I do agree that Kylix was a very good promise and concept, that Borland ...
  • Re: Need help with REDEFINES (I think)....
    ... preclude the compiler optimizing it. ... (a compiler generated subroutine) ... generated subroutine) because it uses a simple compare (or series of ... would be fairly consistent across platforms, but even if I'm wrong and there ...
  • Re: Noob question about the >> operator
    ... Better (using 'int' rather than the platform-dependent 'int32_t'): ... on platforms of interest. ... My version has no need of either preprocessor tests or static ... compiler was done after 1999. ...