NULL pointer and zero value

V

vp

If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"

The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction for each member of array, is
it correct to do something like

memset( garMF, 0, sizeof(ModuleFunctions)*N );


Thanks for your help,

DT
 
R

Richard Heathfield

vp said:
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"

No. NULL is not necessarily all-bits-zero.

Your technique will, however, work by chance on quite a few platforms,
because quite a few platforms do use all-bits-zero for NULL. If you like
programming-by-casino, go ahead. :)

<snip>
 
M

Martin Dickopp

If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"

No. The former statement sets all bits of `p' to zero, which is not
necessarily a representation of the null pointer.
The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction for each member of array, is
it correct to do something like

memset( garMF, 0, sizeof(ModuleFunctions)*N );

If `garMF' has static storage duration, it is automatically initialized to
zero if you don't initialize it explicitly. "Initialized to zero" means
that each non-compound object is initialized as if you assigned `0' to it
(i.e. pointers are initialized to null pointers). For compound objects,
its members or elements are recursively initialized in this way.

Otherwise (if `garMF' doesn't have static storage duration) you'll have
to loop.

Martin
 
M

Mark A. Odell

No. NULL is not necessarily all-bits-zero.

Your technique will, however, work by chance on quite a few platforms,
because quite a few platforms do use all-bits-zero for NULL. If you like
programming-by-casino, go ahead. :)

So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */
{
free(pVar);
}

return EXIT_SUCCESS;
}
 
P

pete

Martin said:
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"

No. The former statement sets all bits of `p' to zero, which is not
necessarily a representation of the null pointer.
The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction
for each member of array, is it correct to do something like

memset( garMF, 0, sizeof(ModuleFunctions)*N );
Otherwise (if `garMF' doesn't have static storage duration)
you'll have to loop.

I disagree about the necessity of a loop.

ModuleFunction garMF[N] = {NULL};
 
M

Martin Dickopp

Mark A. Odell said:
So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */
{
free(pVar);
}

return EXIT_SUCCESS;
}

The program is correct (i.e. strictly conforming). The `if' statement
doesn't look at the representation (i.e. the bits) of `pVar', it simply
compares `pVar' to `0'. A comparison between an expression of pointer
type and `0', which is a null pointer constant in such a comparison, is
valid.

Martin
 
P

pete

Mark said:
So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */

Your code does not check to see if all bits are zero.

Your question and corresponding code example
are not relevant to what Richard Heathfield said.
 
P

pete

pete said:
Your code does not check to see if all bits are zero.

Your question and corresponding code example
are not relevant to what Richard Heathfield said.

The question is relevant, the answer is "no".
 
C

CBFalconer

vp said:
If I have a pointer char * p, is it correct to assign NULL to
this pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"

NO.

While it will often "work" on a particular system, it is not
portable and can lead to very mysterious bugs.
 
M

Mark A. Odell

The program is correct (i.e. strictly conforming). The `if' statement
doesn't look at the representation (i.e. the bits) of `pVar', it simply
compares `pVar' to `0'. A comparison between an expression of pointer
type and `0', which is a null pointer constant in such a comparison, is
valid.

Excellent. Thank you.
 
V

vp

If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"
[...]

Thanks for all of your replies and explanation. I see that I just can
not be lazy to make that memset. I don't want to put Las Vegas into my
app :)

DT
 
V

vp

Mark A. Odell said:
So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */
{
free(pVar);
}

return EXIT_SUCCESS;
}

I guess the compiler will do the necessary comparison in this case. I
often see this style of code "if ( pointer )", but personally I often
write it like "if ( p != NULL )" just to remind me or my co-workers
that p is of pointer-type.

DT
 
M

Mark A. Odell

(e-mail address removed) (vp) wrote in

I guess the compiler will do the necessary comparison in this case. I
often see this style of code "if ( pointer )", but personally I often
write it like "if ( p != NULL )" just to remind me or my co-workers
that p is of pointer-type.

I avoid explicit checks for boolean conditions and since all my pointer
vars have a lower-case p in front, e.g. pSomeName, this is never an issue
for me. Besides, if I want to know if 'foo' is a pointer then I just hover
over it with my mouse and CodeWright shows me the definition. I thought
all editors could do such simple things. ;-)
 
J

Jack Klein

Martin said:
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"

No. The former statement sets all bits of `p' to zero, which is not
necessarily a representation of the null pointer.
The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction
for each member of array, is it correct to do something like

memset( garMF, 0, sizeof(ModuleFunctions)*N );
Otherwise (if `garMF' doesn't have static storage duration)
you'll have to loop.

I disagree about the necessity of a loop.

ModuleFunction garMF[N] = {NULL};

I would much prefer {0} to {NULL} here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
K

Kevin Goodsell

Mark said:
So are you saying that this isn't safe?

#include <stdlib.h>

int main(void)
{
int *pVar = malloc(1024);

if (pVar) /* <--- Not safe? */
{
free(pVar);
}

return EXIT_SUCCESS;
}

Answers already given, but I wanted to mention that this is also valid:

int *p = 0;

regardless of the object-representation of the null pointer value. This
is somewhat obvious, because even on systems where the null-pointer
value is not represented by all-bits-zero, NULL must still be defined as
a constant expression with the value 0, possibly cast to void*, so this:

int *p = NULL;

is translated by the preprocessor to something equivalent to one of the
following:

int *p = 0;

or

int *p = (void *)0;

It follows somewhat logically that things like these:

if (p)
if (p == 0)

are actually testing p against the null pointer value, whatever its
representation is.

-Kevin
 
P

Peter Nilsson

Kevin Goodsell said:
Jack said:
ModuleFunction garMF[N] = {NULL};


I would much prefer {0} to {NULL} here.

Is {NULL} even correct here if NULL happens to be #defined with a cast
to void*?

Yes. Since NULL is a null pointer constant.

6.3.2.3p4: "Conversion of a null pointer to another pointer type yields a
null pointer of that type. ...". [There is no limitation with respect to
function pointers in this regard.]

6.7.8p11 Initialization: "...the same type constraints and conversions as
for simple assignment apply,..."

6.5.16.1p1 Simple Assignment - Constraints: "... - the left operand is a
pointer and the right is a null pointer constant; ..."

6.5.16.1p2 Simple Assignment - Semantics: "In simple assignment (=), the
value of the right operand is converted to the type of the assignment
expression and replaces the value stored in the object designated by the
left operand."

I think Jack prefers {0} because it's a more consistent (and well
understood) paradigm in general.
 
P

pete

Jack said:
typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions;

#define N 100
ModuleFunction garMF[N];
ModuleFunction garMF[N] = {NULL};

I would much prefer {0} to {NULL} here.

All of the members of all of the elements of the array
will be initialized to NULL. Why do you like {0} better ?
 

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
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top