tags:

views:

117

answers:

6

If I declare a pointer to a struct in .h for example:

my_struct_t *ptr;

... and then I check if(ptr==NULL) in the code, without actually setting ptr to NULL or allocating memory for it, can I do that check to see if its equal to NULL?

essentially what I'm asking is, by having ptr in the .h, does it get set to NULL automatically, or do I have to do that?

Thanks, Hristo

revisiont: this is done in C

+4  A: 

From K&R2nd:

In the absense of explicit initializations, external and static variables are guaranteed to be initialized to zero.

So, yes.

That appears to be in section A8.7 of the 1990 standard. Don't know where to look in the 1999 standard.

dmckee
So I can check if(ptr == 0) just by having it declared in the header file?
Hristo
Thanks for the explanation. I did a check for if(ptr==0) and if(ptr==NULL) and they both passed. So which of these checks is more "correct" to use?
Hristo
NULL is #defined to be 0, so they do the same thing. It's a style choice as to which to prefer.
Alan
Thanks for your help!
Hristo
@Alan, in c++ NULL is defined to be 0, but in C it is 'an implementation defined null pointer constant' which in gcc is `(void*)0` (note that the standard requires the type to be a pointer, even if 0 can be converted to (void*) implicitly)
David Rodríguez - dribeas
Always use `NULL`, so there is no confusion, and you are guaranteed to get the appropriate value for either C or C++.
Loadmaster
+1  A: 

It is not a proper declaration, you'd have to declare it like this:

 extern my_struct_t *ptr;

And somewhere in a .c source code file actually define it:

 my_struct_t *ptr;  

Which will make it zero initialized.

Hans Passant
the problem is, the .c file I need to edit is just a file of functions that will be used from another c. file that I didn't write and cannot modify.
Hristo
If that header is only included in one compilation unit, you can get away with the form the OP suggested, but this is the *right* way to do it.
dmckee
@hristo - any .c file, it doesn't matter which one you pick.
Hans Passant
Who the heck downvoted this?
Hans Passant
A: 

Global variables are automatically initialized to zero. See the cases in my answer to another question.

Mind that defining variables in a header file is very bad practice - if multiple source files include that header, you'll have duplicate symbols and won't be able to link the program. Better declare that variable as extern in the header and define it only in one source file.

AndiDog
Um, look at: http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html.
bmargulies
dmckee
@dmckee It seemed to be saying that C++ inherited this one from C, but the C standard is surely more reliable. I did put it in a comment instead of an answer for a reason.
bmargulies
@bmargulies: And what were you trying to say with that article? A pointer is a POD-type and thus initialized.
AndiDog
There is a specific statement to the contrary.InitializationA non-const POD object declared with no initializer has an "indeterminate initial value" [§8.5, ¶9].
bmargulies
+1  A: 

To answer your question, yes it will be set to NULL. The variable will have global scope, and variables with global lifetime gets initialized to NULL (for pointers).

However, you should not place a variable definition in a .h file. If you include that .h file in more than 1 .c file, you will have multiple definitions of that variable, which is undesirable.

You should place a declaration in the header file, e.g.

extern my_struct_t *ptr;

And then in just one of your .c files, place a definition:

my_struct_t *ptr;
nos
A: 

extern struct foo* pfoo; in a header file and struct foo* pfoo; in one of the .c files outside of any function will get you static storage for the pointer and thus automatic initialization to zero.

extern qualifier is key here. It tells the compiler not to allocate storage for the pointer in every translation unit.

Nikolai N Fetissov
if I were to do this, I would have to put struct foo *pfoo; inside a function that will be called multiple times. So will that mean that my pointer will keep getting initialized to zero every time the function is called, or only once? btw... once I know that the ptr is NULL, I will reassign it, so it won't be NULL anymore.
Hristo
Two different points here - 1. putting "struct foo* pfoo;" into a function makes it a **local variable**, so no zero initialization (unless you prefix it with **static**) 2. Zero-initialization is one-time only, it's usually done by the OS allocating zero-filled page(s) for static program data. Once you assign it - it's your responsibility.
Nikolai N Fetissov
A: 

Here's what K&R says:

External and static variables are initialized to zero by default. Automatic variables for which is no explicit initializer have undefined (i.e., garbage) values.

Automatic variables are the ones declared inside a function. Since you said .h file, yours is probably outside and therefore static.

Otherwise, I believe you could add " = NULL" to the declaration and so you don't have to remember to do it elsewhere.

I try not to create any allocations or code in a .h file.

gbarry