nsun
nsun

Reputation: 159

Linked Lists in C, last node point to NULL?

In C, must I always initialize my last node pointing to NULL? I generally see this happening in tutorials and in books. But to my understanding, an unitialized pointer holds the same address as a NULL pointer. So why initialize?

Upvotes: 0

Views: 5366

Answers (6)

Xcrowzz
Xcrowzz

Reputation: 192

When you create a pointer without initializing it, let's say

char *ptr;

The memory points to some bytes (depending on your pointer type). Those bytes, may be anything. Like some previous initialized unfreed pointer, or, to some garbage.

Which is not the same as a NULL pointer.

To avoid that, you should take the habit of initializing every of your pointers (except for globals and statics which are initialized to 0) and, add a NULL every-time you work with arrays or anything containing multi-elements. Let's say that in this case, NULL is the '\0' of the array/list. For example, if you want to create an array containing a few char *, do it this way :

void function()
{
 char *array[4];

 array[0] = strdup("Jason");
 array[1] = strdup("Mike");
 array[2] = strdup("Nicole");
 array[3] = NULL;
}

It helps you not to go further than the memory you previously allocated for this pointer, avoiding memory fails, and segmentation faults. In case you're using a counter to get through every case of this array.

Otherwise, don't use NULL everywhere, if you allocated a string, don't fill it with NULL after on.

int main()
{
    char *ptr;
    if ((ptr = malloc(sizeof(char) * 42)) == NULL)
        return (-1);
    ptr = NULL;
 return (0);
}

This won't work because you're sending NULL into the bytes you cleaned straight before. If you want to properly use a pointer and clean it before using it, you can use memset. Which will send a x amount of the same byte into the pointer, cleaning the garbage that might be in it by the bytes you passed into memset parameters.

Upvotes: 1

alk
alk

Reputation: 70931

But to my understanding, an uninitialized pointer holds the same address as a NULL pointer

It might, by (bad) luck.

In any case reading out an uninitialised pointer invokes undefined behaviour. Whether this would lead to a crash (or any other "strange" behaviour) sooner or later is unknown, as by the nature of undefined behaviour.

However depending on how the memory the pointer variable "lives" in is allocated, the memory might come as "already" initialised, if allocating

Both methods allocate memory and 0 it out in advance. Which, for most recent systems, is sufficient to have a pointer variable (living inside this memory) carrying the value of NULL.

But please note that there still might be implementations around that use a different bit pattern for NULL than just 0-bits. So to stay 100% portable it is good advice to always initialise with NULL explicitly and not rely on implicitly cleared out memory. And btw, this also helps the potential reader of your sources.

Upvotes: 2

Clifford
Clifford

Reputation: 93476

Contrary to your understanding, the content of an uninitialized pointer is undefined. Only if the object is statically allocated is it guaranteed to be zero initialised.

Upvotes: 0

RyanShao
RyanShao

Reputation: 425

an uninitialized local variable's value is undefined. while the static or global will grantee to be 0.

it is a good practice to initialize all variable you defined, otherwise, it may result very very trick bug.

Upvotes: 0

Viktor Simkó
Viktor Simkó

Reputation: 2627

An uninitialized pointer can hold any garbage value.

Upvotes: 0

Neil
Neil

Reputation: 1066

You might think that null pointer is the pointer that is uninitialized, but it's wrong. Null pointer is a special pointer which doesn't points to anywhere.

//  To declare a null pointer.
int *ptr=NULL;


if(ptr==NULL) //..do something..

Functions like malloc() and getchar() returns null pointer in case they are unable to perforn their task or complete the task.

Upvotes: 0

Related Questions