NULL
NULL

Reputation: 41

Why give a variable a null value when declaring it?

I noticed that many times when declaring variables/arrays/etc, people give it a null value or 0.

for example, take a look at the following snippet:

int nNum = 0;
char cBuffer[10] = { 0 };
int *nPointer = NULL;

and etc.

Untill asking this question I figured it would be for debugging purposes, since when I was debugging a program with Visual Studio I noticed that variables that had no value had undefined numbers as their value, whilst with 0 they had... 0.

Upvotes: 0

Views: 355

Answers (3)

John Bode
John Bode

Reputation: 123508

There are a number of reasons to initialize variables to 0 or NULL.

First of all, remember that unless it's declared at file scope (outside of any function) or with the static keyword, a variable will contain an indeterminate value; it may be 0, it may be 0xDEADBEEF, it may be something else.

For sums, counters, and the like, you want to make sure you start from 0, otherwise you will get an invalid result:

int sum = 0;
while ( not_at_end_of_things_to_sum )
  sum += next_thing_to_sum;

int count = 0;
while ( there_is_another_thing_to_count )
  count++;

etc.

Granted, you don't have to initialize these kinds of variables as part of the declaration; you just want to make sure they're zeroed out before you use them, so you could write

int count;
...
count = 0;
while ( there_is_another_thing_to_count )
  count++;

it's just that by doing it as part of the declaration, you don't have to worry about it later.

For arrays intended to hold strings, it's to make sure that there's a 0 terminator if you're building a string without using strcpy or strcat or scanf or similar:

char buf[N] = { 0 }; // first element is *explicitly* initialized to 0, 
                     // remaining elements are *implicitly* initialized to 0

while ( not_at_end_of_input && i < N - 1 )
  buf[i++] = next_char;

You don't have to do it this way, but otherwise you'd have to be sure to add the 0 terminator manually:

buf[i] = 0;

For pointers, it's to make it easy to test if a pointer is valid or not. A NULL pointer is a well-defined invalid pointer value that's easy to test against:

char *p = NULL;
...
if ( !p ) // or p == NULL )
{
  // p has not yet been assigned or allocated
  p = malloc( ... );
}

Otherwise, it's effectively impossible to tell if the pointer value is valid (points to an object or memory allocated with malloc) or not.

Upvotes: 1

Zander Rootman
Zander Rootman

Reputation: 2208

Depending on the language, you would use NULL or just int *nPointer;

it's called initializing a variable, i.e. you're creating it. This is extremely helpful if you want your program to remain at a constant state, knowing that "un-initialized" variables won't cause an exception.

If you're inializing a varibale inside a loop or function, and you want to use it outside that loop/function and the loop/function only executes when there's a condition attached to it e.g:

if(nNum != 0){
    int *nPointer = NULL;
    for(int i=0; i<10; i++){
        *nPointer++;
    }
}

In this case if you did not initialize your variable, and you try and use it later down the line, your program might break. However, if it has been initialized, your safe, knowing it exists, but is still NULL.

SAFER CODE:

int *nPointer = NULL; //Or this will be a class member
if(nNum != 0){
     for(int i=0; i<10; i++){
         *nPointer++;
     }
 }

Upvotes: 0

digital_revenant
digital_revenant

Reputation: 3324

An uninitialized pointer variable may contain a garbage value. The purpose of initializing a pointer with NULL is that if you inadvertently use it without assigning a proper address, you do not end up modifying the contents at a random memory address.

Upvotes: 0

Related Questions