struct initialization, pointers, doubles
- From: Noob <root@xxxxxxxxx>
- Date: Wed, 06 Oct 2010 11:31:09 +0200
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.
.
- Follow-Ups:
- Re: struct initialization, pointers, doubles
- From: Peter Nilsson
- Re: struct initialization, pointers, doubles
- From: Ben Bacarisse
- Re: struct initialization, pointers, doubles
- Prev by Date: Re: Passing intptr_t to a function with void * prototype.
- Next by Date: Re: Finding a word inside a string
- Previous by thread: Re: Finding a word inside a string
- Next by thread: Re: struct initialization, pointers, doubles
- Index(es):
Relevant Pages
|