Reputation: 43
this a simple program that display pointer address :
#include <stdio.h>
int main(void)
{
int *a;
int *b;
printf ("address of a : %p, the address of b : %p\n", a, b);
return (0);
}
so the output is something like this :
address of a : 0x7fff537b1ad8, the address of b : 0x0
my question is : why variable b
has an invalid address memory but variable a
not
Upvotes: 2
Views: 335
Reputation: 33601
a
[and b
] were never initialized so they can have any value. The values they get are just what happens to be in the corresponding memory locations of main
's stack frame prior to the call to main
. That is, a
and b
are function scope variables so they reside in the function's stack frame.
In your case, b
got 0x0 and a
got a non-zero value that seems to be valid, but it's not. It could point to anywhere in the program's physical address space. We don't know where that is. If we read from it, it might be outside of the assigned/valid/mapped address space of the program and we'd get a segfault on that, too.
Or, if we write to the location that a
points to, we could be writing to any location. So, we could corrupt the program data at a random location. This might not generate an exception immediately, but the effects are unknown. We could be overwriting value(s) in the data segment.
It might cause the program to generate incorrect results later because the value of a given value in the data segment is not what was statically assigned.
Or, we might be corrupting the value contained in another pointer variable in global memory space that has been statically assigned. Later on, dereferencing that pointer could [probably would] produce a segfault
It might even point to protected memory such as the code segment, which would [again] generate a segfault.
Upvotes: 1
Reputation: 213593
A local variable has automatic storage duration (goes out of scope, likely stack allocated).
To access an uninitialized variable with automatic storage duration (that does not have its address taken) invokes undefined behavior. Anything can happen when you do.
Furthermore, uninitialized variables with automatic storage duration have indeterminate values. Meaning it can contain any value, including garbage, zero or a valid address. It does not need to return the same value twice, should you execute the program twice or read the variable twice in a program. On some exotic systems, accessing a variable with indeterminate value can cause undefined behavior if that value is a trap representation.
Summary: There is no telling what this code will do, as there are no guarantees by the C language or anyone else. Reasoning about what such code does is pointless.
As a side note, the address 0
is not the same thing as NULL and not necessarily the representation of a null pointer. NULL is a null pointer constant not an address, with the integer value 0
or (void*)0
. When NULL is assigned to a pointer of any type, it creates a null pointer. The null pointer can contain any internal representation, such as the address 0 or some other value - it is not specified by C.
Upvotes: 1
Reputation: 223719
You're not actually printing the address of the pointers. You're printing the values of those pointers. And since the variables are uninitialized, they have indeterminate values.
You need to use the address-of operator to get their addresses:
printf("address of a : %p, the address of b : %p\n", (void *)&a, (void *)&b);
Note also that this is one of the few instances where a cast to void *
is required.
Upvotes: 1
Reputation: 134286
Both the pointers point to invalid memory. Just because a pointer points to a seemingly valid memory location, does not mean it is valid in the context of your program.
To elaborate, the memory location which a
points to, is not allocated for your program, so from the point of view of your application, it is invalid memory. Any attempt to read from or write to, would invoke undefined behavior.
In other words, there is absolutely no guarantee that b
will hold a NULL
value, it can also have some other seemingly proper but invalid memory address. They are invalid, irrespective of the memory location they point to.
Upvotes: 1