NULL pointers

A

Ark Khasin

Consider, at file scope,
struct {void *foo, *bar;} X = {.foo=NULL,};
bar will get all-bits-zero which AFAIK officially ain't necessarily a NULL.
Now, how bad is the assumption that X.bar==NULL? Are there any platforms
where this is wrong?
Thanks,
 
M

Mark Bluemel

Ark said:
Consider, at file scope,
struct {void *foo, *bar;} X = {.foo=NULL,};
bar will get all-bits-zero which AFAIK officially ain't necessarily a NULL.
Now, how bad is the assumption that X.bar==NULL? Are there any platforms
where this is wrong?

Implicit addition to your question - "and the initialization syntax I
used is valid".

I'll let others tell you of platforms where all-bits zero isn't a NULL.

I think I'll also leave them to point out the dangers of implementing
code which just happens to work rather than being guaranteed to do so.
 
R

Richard Heathfield

Ark Khasin said:
Consider, at file scope,
struct {void *foo, *bar;} X = {.foo=NULL,};
bar will get all-bits-zero

Probably. In fact, it will be initialised to NULL, as required by the
default static initialisation rules. If NULL is all-bits-zero, then yes,
it'll get all-bits-zero. If NULL is SYS:$*3:X42J, it'll get SYS:$*3:X42J.
If NULL is Canterbury Cathedral, it'll get Canterbury Cathedral.
 
K

Kaz Kylheku

Consider, at file scope,
struct {void *foo, *bar;} X = {.foo=NULL,};
bar will get all-bits-zero

That is incorrect. Bar will be initialized to null, just like foo,
whose initializer is redundant since X is a static object.

Static objects with no explicit initializers are are given proper zero
values of their type. Pointers are null, floating-point objects have
value 0.0, and integers are 0.

Even if you move X to a block scope and give it automatic instead of
static storage, both foo and bar will still be initialized to null.
That's because aggregate such as a structure or array is either fully
initialized, or not at all; there is no partial initialization. If any
member has an initializer, whether designated or classic style, all of
the other members are also initialized. Any members which don't have
an initializer get a zero.

{
// no members are initialized
struct s a;

// first member gets explicit zero
// all other members, implicit zero
struct s b = { 0 };

// ...
}
which AFAIK officially ain't necessarily a NULL.

You mean ``null''. ``NULL'' is a preprocessor symbol in C.
Now, how bad is the assumption that X.bar==NULL?

It's a half-decent assumption as far as assumptions go.
 
A

Ark Khasin

Richard said:
Ark Khasin said:


Probably. In fact, it will be initialised to NULL, as required by the
default static initialisation rules. If NULL is all-bits-zero, then yes,
it'll get all-bits-zero. If NULL is SYS:$*3:X42J, it'll get SYS:$*3:X42J.
If NULL is Canterbury Cathedral, it'll get Canterbury Cathedral.
Oops. My bad, and thank you. [I must be sleeping instead of posting.] Sorry.
 
J

James Kuyper

Ark said:
Consider, at file scope,
struct {void *foo, *bar;} X = {.foo=NULL,};
bar will get all-bits-zero which AFAIK officially ain't necessarily a NULL.

Bar will be initialized to a null pointer (6.7.8p10). That would be true
even if X did not have static storage duration (6.7.8p19). However,
there's no guarantee that it will have all bits zero. Despite that fact,
you're correct that it isn't necessarily NULL. NULL is a macro whose
expansion is required to be a null pointer constant, and would therefore
produce a null pointer if it were assigned to bar, but not necessarily
the same null pointer that results from bar being implicitly initialized.
Now, how bad is the assumption that X.bar==NULL?

It's a perfectly valid assumption. All null pointers, regardless of
representation, are required to compare equal to each other and to all
null pointer constants.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top