struct initialization, pointers, doubles



Hello everyone,

Are the following snippets well-defined

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

and

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.

Consider

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?

Regards.
.



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. ...
    (comp.programming)
  • 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 ...
    (borland.public.delphi.non-technical)
  • 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 ...
    (comp.lang.cobol)
  • Re: why is there no one standard computer algebra language ?
    ... |> I would not be that quick at attributing a blame about the portability ... it does not really matter that GCC is fully ... | platforms with multiple compilers. ... |> A fully compliant C compiler can reject the simplest hello.c world ...
    (sci.math.symbolic)
  • Re: Using C to program the 8051 family
    ... C may be fine for larger platforms but it's ... > you forget to turn the optimizer off. ... You must either trust the compiler or learn to use a ... > language to debug by. ...
    (comp.arch.embedded)