Trying to understand NULL pointers
c++ - How to assign a value to a pointer that points to NULL - Stack Overflow
Why do we use Null pointers?
function - C Assign Pointer to NULL - Stack Overflow
What is a null pointer?
Can a null pointer be compared with other pointers?
Does a null pointer always point to address zero in memory?
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!
A pointer must point to a valid object in order to something meaningful and it is your responsibility to make sure that the object it points to remains valid.
If the object it points to is on stack, and becomes invalid once its scope is over You should allocate the object on heap using dynamic memory. There is too little information however to know what you are doing and if you need this here.
int *ptr = new int;
*ptr = value;
...
...
delete ptr;
Pointers refer to a location in memory (RAM). When you have a null pointer it is pointing to null, meaning that it isn't pointing to location in memory. As long as a pointer is null it can't be used to store any information, as there is no memory backing it up.
To use a null pointer you must first allocate memory and then have the pointer point to that newly allocated memory.
*int p = null;
*p = 10; //this won't work as there is no memory backing up this pointer
p = new int;
*p = 10;
If we want to "Nullify" something why dont we create a pointer in a controlled way that points to value 0?
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;
}
Well, the null pointer value has the remarkable property that, despite it being a well-defined and unique constant value, the exact value depending on machine-architecture and ABI (on most modern ones all-bits-zero, not that it matters), it never points to (or just behind) an object.
This allows it to be used as a reliable error-indicator when a valid pointer is expected (functions might throw an exception or terminate execution instead), as well as a sentinel value, or to mark the absence of something optional.
On many implementations accessing memory through a nullpointer will reliably cause a hardware exception (some even trap on arithmetic), though on many others, especially those without paging and / or segmentation it will not.
Generally it's a placeholder. If you just declare a pointer, int *a;, there's no guarantee what is in the pointer when you want to access it. So if your code may or may not set the pointer later, there's no way to tell if the pointer is valid or just pointing to garbage memory. But if you declare it as NULL, such as int *a = NULL; you can then check later to see if the pointer was set, like if(a == NULL).
Most of the time during initialization we assign null value to a pointer so that we can check whether it is still null or a address has been assign to it or not.