Re: NULL and zeros
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 03 Oct 2006 08:07:57 -0400
Yevgen Muntyan wrote:
Eric Sosman wrote:
Yevgen Muntyan wrote:
Hey,
Are there not highly specialized and still existing and used platforms
where NULL is not a sequence of zeros, so that "char *a = NULL;" and
"char *a; memset (&a, 0, sizeof (a));" are not equivalent?
In other words, do I care about possible difference between NULL and
zero-filled pointer if e.g. I assume the target platform can run X
or it is Windows?
Have you read Question 5.17 in the comp.lang.c Frequently
Asked Questions (FAQ) list at <http://c-faq.com/>?
Also, have you read the tenth of the Ten Commandments for
C Programmers at <http://www.plethora.net/~seebs/c/10com.html>?
I have read those, and it's why I'm asking. Question is: is true
*hypothetical* portability more important than convenience in my
specific case?
It comes down to how highly you value convenience and how
highly you value portability. That is, it comes down to two
matters of your own tastes and desires, not to the experiences
of other people. So why ask at all? It's like asking "Should
I prefer apples or oranges?"
It's lot of lines of obj->this = NULL; obj->that = NULL;
I get zero-initialized structures for free and would really love
not to have to initialize all pointers inside. Besides, lot of
NULL initializations makes code actually harder to modify - you need
to consult header to know whether you got all should-be-NULL actually
NULL's.
If you want to initialize a struct without knowing what elements
the struct contains, you're on shaky ground already. It seems you
may be worried about
void init_struct(struct s *ptr) {
ptr->p1 = NULL;
ptr->p2 = NULL;
}
.... where somebody might come along at a later date and add a p3
element, which would then go uninitialized unless he also remembered
to modify init_struct(). If that's the issue, I've got two answers:
First, somebody who modifies a data structure should also take the
responsibility of keeping its "constructors" and "renderers" and so
on up-to-date, and the code should be arranged in such a way that
the data type's "fundamental operators" are easy to find -- in fact,
hard for him to avoid. That's really a social problem, not a
programming problem, and you need to employ social pressures like
coding standards to keep the problem under control.
Second, a technical solution for the special case where you'd
like to initialize to "all members zero" (which may or may not be
"all bits zero") without worrying about the elements individually:
void init_struct(struct s *ptr) {
static const struct s all_zeroes;
*ptr = all_zeroes;
}
Which do you like more? Or do you prefer calloc()? Apples,
oranges, or pears?
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: NULL and zeros
- From: Yevgen Muntyan
- Re: NULL and zeros
- References:
- NULL and zeros
- From: Yevgen Muntyan
- Re: NULL and zeros
- From: Eric Sosman
- Re: NULL and zeros
- From: Yevgen Muntyan
- NULL and zeros
- Prev by Date: Re: About Visual C++ 2005 Express Edition
- Next by Date: Re: Debugging standard C library routines
- Previous by thread: Re: NULL and zeros
- Next by thread: Re: NULL and zeros
- Index(es):
Relevant Pages
|