pkill
pkill

Reputation: 421

understanding pointer initialization behavior in C++

Hello stackoverflow community, I was studying pointers in C++. I have a little bit of background in golang and python but not in C or C++, so forgive me because of my flawed understanding. Let me post the piece of code that is working fine and is giving segfault.

This one prints 5

int var = 5;
int *p = &var;
cout << *p << endl;

This one also prints 5

int *p;
p = &var;
cout << *p << endl;

prints 5 (because p is a pointer to int and address of var is a pointer to int. ).

However,

This one segfaults.

int *p;
*p = var;
cout << *p << endl;

which I assume happens because I am assigning an integer variable to a variable which is supposed to be a pointer to the integer. Why was it not showing incompatible type error at compile time rather than giving a segmentation fault? What is the program trying to do that is giving a segmentation fault, and how?

and

This one also segfaults

int *p;
*p = 5;
cout << *p << endl;

throws a segfault as well. My understanding for the above code is, I declared a pointer to int, and I am assigning an integer value to a de-referenced pointer to integer, but since the variable is not initialized, the compiler haven't assigned the memory on the stack for storing the value (something like a dangling pointer behaviour?) If that is the reason, why is it throwing a segmentation fault, the reason for that error being accessing memory outside the program's reach?

Thank you.

Upvotes: 0

Views: 92

Answers (3)

TaQuangTu
TaQuangTu

Reputation: 2343

In the last one, you had written:

int *p;
*p = 5;
cout << *p << endl;

But in this case you have not assigned a value to p. You must assign a value for pointer before you use it. you can try as follow:

int *p;
p = &var; //or another address of integer variable
*p = 5;
cout << *p << endl;

in short, I want to emphasize that you must assign value to a pointer before using it.

Upvotes: 1

cadaniluk
cadaniluk

Reputation: 15229

Why was it not showing incompatible type error at compile time rather than giving a segmentation fault?

Because the types are compatible. *p is of type int, var is of type int too. What you are dealing with here is a problem at runtime, not at compile time.

What is the program trying to do that is giving a segmentation fault, and how?

The * operator dereferences a pointer. That means, the address of that pointer is taken and the content at that address is retrieved.

In your faulting cases, you left the pointer uninitialized. Therefore, the dereferencing operation fails because the address that is referred to is indeterminate.

If a variable is declared only and not initialized, does the compiler ignores the variable name totally and treats it as a non-existant variable name?

Not at all. A declaration introduces a name into a certain scope. In your examples, you had definitions: they told the compiler to allocate a certain amount of space for them.

Upvotes: 3

StereoBucket
StereoBucket

Reputation: 423

When you create a pointer it contains some garbage address. When you tried to dereference that and write some value you've caused an undefined behaviour. You have to assign an address of some existing variable on stack or allocate memory on heap and then you can dereference it.
Just like you did in the first 2 examples. You can also allocate some memory on the heap and assign the address for that memory to your pointer.
int *ptr = new int;
Now you can dereference this pointer (assuming bad_alloc exception wasn't thrown), and assign a value to the memory pointed by the pointer.

Upvotes: 1

Related Questions