1

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

Hristo
  • 45,559
  • 65
  • 163
  • 230

6 Answers6

4

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 --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • So I can check if(ptr == 0) just by having it declared in the header file? – Hristo Feb 20 '10 at 21:51
  • 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 Feb 20 '10 at 22:06
  • NULL is #defined to be 0, so they do the same thing. It's a style choice as to which to prefer. – Alan Feb 20 '10 at 22:18
  • @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 Feb 20 '10 at 22:41
  • Always use `NULL`, so there is no confusion, and you are guaranteed to get the appropriate value for either C or C++. – David R Tribble Feb 20 '10 at 22:53
1

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
  • 922,412
  • 146
  • 1,693
  • 2,536
1

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
  • 223,662
  • 58
  • 417
  • 506
0

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.

Community
  • 1
  • 1
AndiDog
  • 68,631
  • 21
  • 159
  • 205
  • Um, look at: http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html. – bmargulies Feb 20 '10 at 21:53
  • @bmargulies: pointing at c++ docs for a c question is risky. The languages diverge in details here and there. Compare to the excerpt from K&R that I posted. – dmckee --- ex-moderator kitten Feb 20 '10 at 21:56
  • @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 Feb 20 '10 at 21:58
  • @bmargulies: And what were you trying to say with that article? A pointer is a POD-type and thus initialized. – AndiDog Feb 20 '10 at 22:04
  • There is a specific statement to the contrary. Initialization A non-const POD object declared with no initializer has an "indeterminate initial value" [§8.5, ¶9]. – bmargulies Feb 20 '10 at 22:30
0

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 Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • 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 Feb 20 '10 at 21:59
  • 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 Fetissov Feb 21 '10 at 01:05
0

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
  • 10,352
  • 6
  • 33
  • 43