Reputation: 1137
Let's say you have defined a (global or local) pointer variable such as this:
void *this_is_a_pointer;
without giving it the value " = NULL; "
Would this automatically give you a NULL ptr (a pointer that points to NULL)?
* Also, what does it mean for a null pointer to point to NULL? Is that saying that it could point to literally anywhere? (basically, what really is the "address location of NULL"?)
Upvotes: 0
Views: 131
Reputation: 14011
Definitely do not count on this. Compilers in Debug mode (with the correct switch settings) will typically zero variables on the stack for you to track down bugs, but it is better to explicitly initialize all of your variables not just pointers.
EDIT: In response to your edit, the value of NULL is defined as zero. So, it would point to address 0, which in most modern operating systems would result in a segmentation fault, or memory access violation. But, on some embedded operating systems with little memory management it would let you change the contents of address 0! This one has bit me a few times ;)
Upvotes: 3
Reputation: 123468
A pointer with static storage duration will be automatically initialized to NULL. A pointer with auto duration will not be initialized, and will contain a random value that may or may not correspond to a valid memory address:
#include <stdio.h>
int *p0; // File scope, initialized to NULL
int main(void)
{
static int *p1; // Declared "static", initialized to NULL
int *p2; // Auto, not initialized
printf("p0 = %p, p1 = %p, p2 = %p\n", (void *) p0, (void *) p1, (void *) p2);
return 0;
}
There is the null pointer constant, which is 0 (the macro NULL expands to a 0-valued pointer expression). There is the null pointer value, which may or may not be 0-valued; it's the value used by the underlying system to indicate a well-defined "nowhere". When a null pointer constant appears in your code, it will be replaced with the null pointer value.
A null pointer value is guaranteed to compare unequal to any valid pointer value.
Upvotes: 1
Reputation: 4152
Initialise a pointer
Before you can use a pointer in for instance a printf statement, you have to initialize the pointer. The following example will not initialize the pointer:
#include<stdio.h>
void main()
{
int *ptr_p;
printf("%d\n",*ptr_p);
}
Note: We used void when we declared the function main(). So no return 0; is needed. (Some compilers will give a warning when void is used on main(). GNU compiler will give the warning: return type of ‘main’ is not ‘int’).
In this example we print the value that ptr_p points to. However, we did not initialize the pointer. In this case the pointer contains a random address or 0.
The result of this program is a segmentation fault, some other run-time error or the random address is printed. The meaning of a segmentation fault is that you have used a pointer that points to an invalid address.
The most straightforward way to ``get'' a null pointer in your program is by using the predefined constant NULL, which is defined for you by several standard header files, including , , and . To initialize a pointer to a null pointer, you might use code like
#include
int *ip = NULL; < and to test it for a null pointer before inspecting the value pointed to you might use code like
if(ip != NULL) printf("%d\n", *ip);
It is also possible to refer to the null pointer by using a constant 0, and you will see some code that sets null pointers by simply doing
int *ip = 0;
(In fact, NULL is a preprocessor macro which typically has the value, or replacement text, 0.) Furthermore, since the definition of ``true'' in C is a value that is not equal to 0, you will see code that tests for non-null pointers with abbreviated code like
if(ip) printf("%d\n", *ip);
This has the same meaning as our previous example; if(ip) is equivalent to if(ip != 0) and to if(ip != NULL). All of these uses are legal, and although I recommend that you use the constant NULL for clarity, you will come across the other forms, so you should be able to recognize them.
Upvotes: 1
Reputation: 503913
(global or local)
This is actually what makes the difference.
In general, if you didn't initialize a variable, it's not initialized and reading from it is undefined behavior. But if the variable has static storage duration, then it's automatically initialized to zero (null for pointers).
// foo.cpp
void* this_is_null;
void whatever(void)
{
static int* and_so_is_this;
}
void not_whatever(void)
{
float* but_this_local_is_uninitialized;
}
Upvotes: 7