0

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?

5
  • 4
    Uninitialized holds unspecified value. That's not the same as NULL. Commented Aug 10, 2015 at 6:41
  • "an unitialized pointer holds the same address as a NULL pointer" Is it so? Then what is initializing it to NULL Commented Aug 10, 2015 at 6:41
  • 1
    BTW: It is not nessary to set the last pointer to NULL. It must be any value that shall indicate the end of the list. This can be e.g. the address of the list anchor or if you like a pointer with the value 0xffffffff. But in most cases NULL is used by convention. This allows a syntax like while (p->next) p=p->next. Commented Aug 10, 2015 at 7:00
  • 1
    The short answer is NO, but you must understand there are 2 types of linked lists (generally). The first being the traditional head/tail list where the last node is NULL to serve as a sentinel for the end of list. The second type of linked list is a circular linked list where the ptr->next pointer for the last node always holds the address of the start of the list. There no NULL is needed because iteration continues until the first node is reached again. (there are many list types in between) Depending on your requirements one may work better than the other. Commented Aug 10, 2015 at 7:12
  • @harper: No, it has to be NULL or a the address of a real object, not just some arbitrary value like 0xffffffff. Otherwise even comparing it with another pointer is undefined behavior. Commented Aug 10, 2015 at 10:55

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

6 Comments

"The memory points to some bytes" depending on the platform it might even point to no memory at all!-)
This "... 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." somehow is not (fully?) clear to me. You might like to take your time to clarify what you want to express.
I did clarify what I wanted to say.
"Let's say that in this case, NULL is the '\0' of the array/list." What does that mean? NULL is a null-pointer macro, whereas '\0' is a null-character - they are not the same thing.
Can you read my post please, straight after this I'm writing an example. Null is the terminating pointer of an array, as \0 is the terminating byte of a string
|
0

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.

Comments

0

An uninitialized pointer can hold any garbage value.

Comments

0

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.

Comments

0

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.

5 Comments

Even in the case of allocation, malloc doesn't guarantee zero initialisation. Only calloc does.
@KDM : I don't see your point? I did not mention dynamic allocation. Static allocation is that the compiler generates when you declare a variable static; it clearly has nothing to do with malloc().
Missed the statically in the statically allocated. Sorry.
And even 0ed memory does not guarantee, that a pointer "living" in there carries the NULL value. Please see footnote 296 here: iso-9899.info/n1570.html#7.22.3.2 (same for floating point variables)
@alk : Strictly true - in C NULL is allowed to be a non-zero value, but in practice it is unlikely on most architectures. However "usually getting away with it" is no excuse for bad code of course. I think it is largely academic however; it will depend on the linked list implementation, but in the case where the caller provides nodes to be added to the list, there are no guarantee that the node's link-pointers are in any kind of "initial" state in any case - it may have been reused and contain the links for whatever list it was on previously for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.