why cout statement is not working after pointer initialization? - c++

cout statement is not working after pointer initialization. And this code gives me a segmentation fault.
#include <iostream>
using namespace std;
int main()
{
int *p;
*p=12;
cout<<"NOW THIS STATEMENT WILL NOT WORK";//BUT WHY?
cout<<*p;
return 0;
}

You didnt initialize the pointer:
int *p;
p points "nowhere", it is not initialized, its value is indeterminate. When you dereference it in the next line:
*p=12;
You cause undefined behavior, because p does not point to an int. There is no int where you could store 12.
If you want to store an int somewhere, you need an int not just a pointer to an int:
int x = 0;
int *p = &x;
*p=12; // same as x = 12;

Related

Can you tell me how the code given reachs to that output?

the output of the code is 30.But I am not sure how it is getting to it.
#include <iostream>
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
std::cout << fun();
return 0;
}
I am expecting that the output will be 10 but its showing 30. how?
Your fun gives a location of an int (since it returns a reference). That location is the static variable x which is initialized (once, conceptually before the program runs) to 10.
Then fun() = 30; is assigning that location. So x gets assigned to 30.
At last cout << fun() displays the content of that location.
If x was some automatic variable your code would have undefined behavior.
PS. A crude way of thinking about & unary reference like int &r = x; is that it sort-of "transforms" your code as: introduce a phantom pointer int *p = &x; (where p is some fresh variable not appearing elsewhere) and replace r with *p, so &r with p, everywhere in the scope of that r.

How to define an alias to a pointer to an integer?

I am trying to run the following code, but I am getting the following error.
error: cannot declare pointer to 'int&'
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int *ptr = &x;
int &(*y) = ptr;
*y = 5;
cout << *ptr << endl;
return 0;
}
You declare references to pointers the same way you declare references to basic types.
Consider:
int main()
{
int i = 0; // int
int& ir = i; // int reference (reference to int)
int* ip = &i; // int pointer (pointer to int)
int*& ipr = ip; // int pointer reference (reference to pointer to int)
*ip = 5;
cout << *ipr << endl;
return 0;
}
If you just want a new pointer to the same region of memory, use:
int *y = ptr;
This not so much an "alias" in that if you change *ptr or *y, both will change, but if you change the pointers themselves, the other will not be updated.
If you actually do want a reference to a pointer, use:
int *&y = ptr;
int *ptr = &x;
pointer value has an address and a type of x.
when you typed code above, the value of ptr is an address of x, and ptr know the type of x.
int * (&y) = ptr;
the code above is declaring variable 'y' (type:int*, define:ptr's reference)
reference variables should be declared and defined simultaneously.
anyway, as a result, ptr and y are pointing same memory address.
you can easily think y is a nickname of ptr.
so you can access the variable 'x' by using y, instead of ptr.
v - a variable name.
&v - a variable that will be a reference of something.
*&v - a variable that will be a reference of a pointer to something
int *&v - a variable that will be a reference of a pointer to int
Or for a more interesting example,
(*&v)[5] - a variable that will be a reference of a pointer to an array of 5 something
int (*&v)[5] - a variable that will be a reference of a pointer to an array of 5 int

C++ pointers: dereferencing, assigning values, etc

The questions I'm asking are very basic, but I'm trying to understand the behaviour of pointers in C++ by doing exercises in the compiler. So, for example, I start by declaring an int pointer *p and trying to ascribe it some values and print it:
#include <iostream>
using namespace std;
int *p, i;
int main(){
i=5;
p=&i;
cout<<*p<<endl;
return 0;
}
This is very clear and easy, right? But while I'm wondering why the C++ syntax works this way, I test another way of assigning a value to a pointer:
#include <iostream>
using namespace std;
int *p, i;
int main(){
i=5;
p=&i;
*p=3;
cout<<*p<<endl;
return 0;
}
Obviously, the result is different now. The question is, why this wouldn't work:
#include <iostream>
using namespace std;
int *p, i;
int main(){
*p=3;
i=5;
p=&i;
cout<<*p<<endl;
return 0;
}
Why do I have to dereference the pointer first before assigning it a value, but direct assignment would not work without dereferencing?
Also, if in the second example I wrote I added another assignment:
int main(){
i=5;
p=&i;
*p=3;
*p=6;
cout<<*p<<endl;
return 0;
}
This would not change the value stored at *p (it would still be 3). I don't simply want to learn by memorising this pointer behaviour, I'm interested in understanding why it works this way. Thanks.
#include <iostream>
using namespace std;
int *p, i;
int main(){
*p=3;
i=5;
p=&i;
cout<<*p<<endl;
return 0;
}
This doesn't work because you are trying to assign a value to the integer that p points to. That's what this line does:
*p = 3;
That means, "store the value 3 at the location which p points at". But p doesn't point at anything, because you didn't assign it to point to anything until two lines later.
p = &i;
That means, "assign the address of i to be the value of p". Or, in other words, "store the address of i in p". p needs to point to something, before you can assign to the thing p points to. Otherwise you have undefined behavior.
On the last section:
int main(){
i=5;
p=&i;
*p=3;
*p=6;
cout<<*p<<endl;
return 0;
}
You say this: "This would not change the value stored at *p (it would still be 3)." -- I'm not sure why you say that. Yes, it would change the value stored at *p (which is the value of i). It changes it to 6.
The question is, why this wouldn't work:
int *p, i;
int main(){
*p=3;
here you attempt to dereference p and write something, but since p is uninitialized here (it can be 0, for example), you are trying to write to memory that you didn't allocate, thus this is a segmentation fault.
Also, if in the second example I wrote I added another assignment: [...] This would not change the value stored at *p (it would still be 3).
Actually, it would. Why would you think otherwise?
I'm interested in understanding why it works this way.
You're on the right track, just continue reading (e.g. this thread) and experimenting!

Why does my code produce a segmentation fault?

#include <iostream>
using namespace std;
int main ()
{
int **a;
int b[5] = {3,4,5,6,1};
*a=b;
cout << *((*a)+0) << endl;
return 0;
}
According to my understanding *((*a)+0) is equivalent to (*a)[0]. Am I wrong? How can I make the above code print the first element of the array?
And why does this code work?
#include <iostream>
using namespace std;
int main ()
{
int *a;
int b[5] = {3,4,5,6,1};
a=b;
cout << *(a+0) << endl;
return 0;
}
When I replace a with *a everywhere, why is it wrong?
You access an uninitialized pointer in
*a=b;
At this point a points to a random location, and as is the rule with undefined behavior you can't predict what will happen. For you it seems to be a location that you can't write to, and so you get a crash.
The second variant works because then you make a point to b, you don't write to an uninitialized pointer, you actually initialize the pointer with the location of the first item in b.

C++ pointers declaration order output segmentation fault or bus error

This would be two questions in one
I have two pieces of codes, the only difference being the order between the declaration of int* a; and int cpt = 0; on lines 6 and 7.
Case 1:
#include <iostream>
using namespace std;
int main()
{
cout<<"begin"<<endl;
int* a;
int cpt = 0;
cout<<"after init "<<a<<endl;
*a = 2;
cout<<"after assign"<<endl;
cout<<a<<" "<<*a<<endl;
cout<<"after cout"<<endl;
int* b;
*b = 2;
cout<<b<<" "<<*b<<endl;
}
Output:
begin
after init 0x7fff6c97f05e
Bus error: 10
Case 2:
#include <iostream>
using namespace std;
int main()
{
cout<<"begin"<<endl;
int cpt = 0;
int* a;
cout<<"after init "<<a<<endl;
*a = 2;
cout<<"after assign"<<endl;
cout<<a<<" "<<*a<<endl;
cout<<"after cout"<<endl;
int* b;
*b = 2;
cout<<b<<" "<<*b<<endl;
}
Output:
begin
after init 0x7fff50e4ac00
after assign
0x7fff50e4ac00 2
after cout
Segmentation fault: 11
I'm wondering why the declaration order affects the error. The cpt variable isn't used anywhere, so why would it's declaration affect the error?
I'm also wondering why does the pointer "a" in the second case doesn't produce a segfault when referencing it when the "b" pointer does produce a segfault. They have the same declaration and same usage, why the difference?
Thanks!
The key is what you're doing (dereferencing an uninitialized pointer) results in undefined behavior, so you really can't expect anything in particular to happen, nor is there a reasonable/"standard-conformant" explanation for what the program does. It can be, however, the case, that the stack is set up in a way that in the second case, a points to a valid memory location by accident, but that's just a guess.