Trying to understand NULL pointers
function - C Assign Pointer to NULL - Stack Overflow
Why am I being asked to set pointer to Null after freeing?
Is it possible to initialize a C pointer to NULL? - Stack Overflow
Can a null pointer be compared with other pointers?
What is the difference between a null pointer and an uninitialized pointer?
What is the purpose of a null pointer in programming?
Videos
Hello all again,
I have another stupid question here lol, so I'm trying to wrap my head around NULL. Im currently under the impression that NULL is a built in constant that has a value of zero, but what does that actually mean? When would it be appropriate to use null? If someone could explain it in layman's terms that would be super helpful!
It's because the pointer is passed by value and not by reference. If you want to change the pointer inside the function you need to pass the actual pointer as a pointer, i.e. a pointer to a pointer:
Copyvoid my_function(char **a)
{
*a = NULL;
}
Use the address-of operator & when you call the function to get the address of the pointer:
Copymy_function(&ptr);
Your statement a=NULL in my_function() indeed sets the value of a to NULL, but a is a local variable of that function.When you passed ptr to my_function() in main(), the value of ptr was copied to a.I suppose your whole confusion arose from the * used before a in the definition of my_function().
Pointers are generally passed to functions when we want to manipulate the original values which those pointers point to, from the called function, and this is done by dereferencing those pointers from the called functions.In this case, had you used this:
Copy*a= blah blah;
it would have reflected in the value at the address pointed to by ptr in main().But since you want to change the value of ptr itself, you need to be able to have a way to manipulate it from my_function().For this you use a pointer-to-pointer,ie of type char**.You pass such a char** as argument to my_function(() and use it to alter the value of ptr.Here's the variation to your code that would do it for you:
Copy#include <stdlib.h>
#include <stdio.h>
void my_function(char **); // Change char* to char**
int main(int argc, char *argv[]) {
char *ptr;
ptr = malloc(10);
if(ptr != NULL) printf("FIRST TEST: ptr is not null\n");
else printf("FIRST TEST: ptr is null\n");
my_function(&ptr); //You pass a char**
if(ptr != NULL) printf("SECOND TEST: ptr is not null\n");
else printf("SECOND TEST: ptr is null\n");
}
void my_function(char **a) { //Change char* to char** here
*a = NULL;
}
During an school excersise one of the requirements is to free and then set to null all mallocs in case of failure.
I fail to understand the logic in this, I thought freeing a variable already "deleted" the information in it, so setting it to Null doesn't seem to be working as a way to delete the memory, right?
And its being asked for when the code fails, like when a malloc fails for one variable I'd have to free and null point the other variables before leaving the program.
Could you help me understand the logic behind this? Thenks!
Edit: thank you so much for everyone that took the time to explain it! Learned alot about houses... I mean frees and Nulls!
Is it possible to initialize a C pointer to NULL?
TL;DR Yes, very much.
The actual claim made on the guide reads like
On the other hand, if you use just the single initial assignment,
int *my_int_ptr = 2;, the program will try to fill the contents of the memory location pointed to bymy_int_ptrwith the value 2. Sincemy_int_ptris filled with garbage, it can be any address. [...]
Well, they are wrong, you are right.
For the statement, (ignoring, for now, the fact that pointer to integer conversion is an implementation-defined behaviour)
Copyint * my_int_ptr = 2;
my_int_ptr is a variable (of type pointer to int), it has an address of its own (type: address of pointer to integer), you are storing a value of 2 into that address.
Now, my_int_ptr, being a pointer type, we can say, it points to the value of "type" at the memory location pointed by the value held in my_int_ptr. So, you are essentially assigning the value of the pointer variable, not the value of the memory location pointed to by the pointer.
So, for conclusion
Copy char *x=NULL;
initializes the pointer variable x to NULL, not the value at the memory address pointed to by the pointer.
This is the same as
Copy char *x;
x = NULL;
Expansion:
Now, being strictly conforming, a statement like
Copy int * my_int_ptr = 2;
is illegal, as it involves constraint violation. To be clear,
my_int_ptris a pointer variable, typeint *- an integer constant,
2has typeint, by definition.
and they are not "compatible" types, so this initialization is invalid because it's violating the rules of simple assignment, mentioned in chapter ยง6.5.16.1/P1, described in Lundin's answer.
In case anyone's interested how initialization is linked to simple assignment constraints, quoting C11, chapter ยง6.7.9, P11
The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.
The tutorial is wrong. In ISO C, int *my_int_ptr = 2; is an error. In GNU C, it means the same as int *my_int_ptr = (int *)2; . This converts the integer 2 to a memory address, in some fashion as determined by the compiler.
It does not attempt to store anything in the location addressed by that address (if any). If you went on to write *my_int_ptr = 5;, then it would try to store the number 5 in the location addressed by that address.
If we want to "Nullify" something why dont we create a pointer in a controlled way that points to value 0?