Considere the following code in c++:
#include <iostream>
using namespace std;
int main() {
int x=2, y;
int *p = &x;
int **q = &p;
std::cout << p << std::endl;
std::cout << q << std::endl;
std::cout << *p << std::endl;
std::cout << x << std::endl;
std::cout << *q << std::endl;
*p = 8;
*q = &y;
std::cout << "--------------" << std::endl;
std::cout << p << std::endl;
std::cout << q << std::endl;
std::cout << *p << std::endl;
std::cout << x << std::endl;
std::cout << *q << std::endl;
return 0;
}
The output of code is (of course the list numbers is not the part of output):
0x7fff568e52e0
0x7fff568e52e8
2
2
0x7fff568e52e0
'-----------'
0x7fff568e52e4
0x7fff568e52e8
0
8
0x7fff568e52e4
Except for 7 and 9, all outputs were expected for me. I appreciate someone explaining them to me.
The variable y was not initialized
int x=2, y;
So it has an indeterminate value.
As the pointer q points to the pointer p
int **q = &p;
then dereferencing the pointer q you get a reference to the pointer p.
So this assignment statement
*q = &y;
in fact is equivalent to
p = &y;
That is after the assignment the pointer p contains the address of the variable y.
So this call
std::cout << p << std::endl;
now outputs the address of the variable y.
0x7fff568e52e4
and this call
std::cout << *p << std::endl;
outputs the indeterminate value of y that happily is equal to 0.
9. 0
Related
Let's consider I have the following x pointer variable in my function:
int* x = new int { 666 };
I know I can print its value by using the * operator:
std::cout << *x << std::endl;
And that I can even print the address of where the 666 is being stored on the heap, like this:
std::cout << (uint64_t)x << std::endl;
But what I'd like to know is on whether it's also possible to grab the address of the x variable itself, that is, the address of the region of memory in the stack containing the pointer to the heap int containing 666?
Thanks
Just use another &
std::cout << (uint64_t)&x << std::endl;
e.g.:
int v = 666; // variable
int * x = &v; // address of variable
int ** p = &x; // address of pointer x
and so on
int *** pp = &p;
int **** ppp = &pp;
and how to access to it:
std::cout << ****ppp << " == " << ***pp << " == "
<< **p << " == " << *x << " == " << v << std::endl;
i have a code:
#include <iostream>
using namespace std;
int main()
{
int *a, y = 6 , *yPtr = &y;
cout << "y:" << y << "| &y:" << &y << "| yptr:" << yPtr << "| *yptr:" << *yPtr << " | &yptr:" << &yPtr << " |a:" << a << endl;
*a = y;
cout<< "a:"<<a<<endl;
return 0;
}
when i assign *a to y *a = y then *a value not printed for me
This is because you never initialize a itself. *a points to who-knows-where, some random location. So you set some random location to 6.
As it's probably pointed outside of legal space, your program is probably quitting before it gets to the cout statement.
This is what i have:
void g(int *&x) {
int a = 3;
x = &a;
}
void h(const int *&x) {
int b = 2;
x = &b;
}
int main() {
int *p = new int;
*p = 5;
g(p);
cout << p << " " << *p << endl; // Print #2
cout << p << " " << *p << endl; // Print #3
const int*p1 = p;
h(p1);
cout << p << " " << *p << endl; // Print #4
cout << p << " " << *p << endl; // Print #5
}
From what i understand, Print#2 and Print#3 should have the same result but it isn't when i compile it. It goes for Print#4andPrint#5 too. Can someone help me?
Updated : This is the output looks like when i compiled it on my computer:
00EFF9D4 3 //1
00EFF9D4 1552276352 //2
00EFF9D4 2 //3
00EFF9D4 1552276352 //4
shouldn't (1) and (2) be the same ? (3) and (4) either.
I assume you mean int a in g().
Your function make the pointer point to a local variable, which after the termination of the function, will go out of scope.
You then dereference the pointer, which invokes Undefined Behavior.
I have the simple program:
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 6;
int* p1 = &a;
int* p2 = &b;
std::cout << p1 << " " << p2 << " ,sizeof(int)=" << sizeof(int) << std::endl;
system("pause");
return 0;
}
It produces the following output:
00DBF9B8 00DBF9AC ,sizeof(int)=4
but, 00DBF9B8 - 00DBF9AC == ะก. I cannot understand this result.
If I modify the program like this:
#include <iostream>
using namespace std;
int main()
{
static int a = 5;
static int b = 6;
int* p1 = &a;
int* p2 = &b;
std::cout << p1 << " " << p2 << " ,sizeof(int)=" << sizeof(int) << std::endl;
system("pause");
return 0;
}
I got correct result:
00394000 00394004 ,sizeof(int)=4
There is no guarantee that local variables (or even static variables) are put on consecutive memory addresses. And actually it would be undefined behaviour if you substracted two pointer values that do not point into the same array.
But you could use pointer arithmetics as follows:
int main()
{
int a;
int* p1 = &a;
int* p2 = p1+1;
std::cout << p1 << " " << p2 << " ,sizeof(int)=" << sizeof(int) << std::endl;
return 0;
}
Note that a single integral value may be considered as an array of size 1, and that p1+1 therefore points to "one after the last element of an array", such that the operation p2 = p1+1 is actually valid (dereferencing p2 then would not be valid, of course).
This question already has answers here:
How to print function pointers with cout?
(7 answers)
Closed 9 years ago.
I'm not clear on what the values that are being returning from calling:
&next, fp, *fp, &return_func_ptr, fp_ptr, &fp_ptr, *fp_ptr
They all seem to give me the value 1. What does it mean?
Also, how would I declare
int (*return_f())(char)
to receive a parameter without using typedef?
#include <iostream>
int next(int n){
return n+99;
}
// returns pointer to a function
typedef int (*fptr)(int); // using typdef
fptr return_func_ptr(){
return next;
}
int f(char){
return 0;
}
int (*return_f())(char){ // how do you pass a parameter here?
// std::cout << "do something with " << param << std::endl;
return f;
}
int main()
{
int x = 5;
// p points to x
int *p = &x;
std::cout << "x=" << x << std::endl; // 5, value of x
std::cout << "&x=" << &x << std::endl; // 0x7fff6447a82c, address of x
std::cout << "p=" << p << std::endl; // 0x7fff6447a82c, value of p is address of x
std::cout << "*p=" << *p << std::endl; // 5, value of x (p dereferenced)
std::cout << "&p=" << &p << std::endl; // 0x7fff6447a820, address of p pointer
// change value of x thru p
// p = 6; // error, can't set int* to int
*p = 6;
std::cout << "x=" << x << std::endl; // 6
int y = 2;
// int *q = y; // error can't initiate with type int, needs int*
// pointer to a function
int (*fp)(int);
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp
std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp
fp = &next; // fp points to function next(int)
fp = next;
std::cout << "&next=" << &next << std::endl; // 1, address of function?
std::cout << "fp=" << fp << std::endl; // 1, value is address of function?
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp?
std::cout << "*fp=" << *fp << std::endl; // 1, address of function?
// calling function thru pointer
int i = 0;
i = (*fp)(i);
std::cout << "i=" << i << std::endl; // 99
i = fp(i);
std::cout << "i=" << i << std::endl; // 198
// function returns pointer to function
fptr fp_ptr = return_func_ptr();
std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl; // 1
std::cout << "fp_ptr=" << *fp_ptr << std::endl; // 1
std::cout << "&fp_ptr=" << *fp_ptr << std::endl; // 1
std::cout << "*fp_ptr=" << *fp_ptr << std::endl; // 1
int j = fp_ptr(1);
std::cout << "j=" << j << std::endl; // 100
}
There is some pointer here who seems not clear :
// pointer to a function
int (*fp)(int);
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp
std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp
Here fp is undefined. Those lines have an undefined behaviour.
After that :
// function returns pointer to function
fptr fp_ptr = return_func_ptr();
std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl; // 1
std::cout << "fp_ptr=" << *fp_ptr << std::endl; // 1
std::cout << "&fp_ptr=" << *fp_ptr << std::endl; // 1
// ^^^^^^^^^^ ^^^^^^^
std::cout << "*fp_ptr=" << *fp_ptr << std::endl; // 1
There are two things here :
On the line I pointed, I'm not sure it is what you wanted to test.
Also, cout doesn't have an overload to take a function pointer, it will take a bool instead. So it should be :
std::cout << "fn_ptr=" << reinterpret_cast<void*>( fn_ptr ) << std::endl;
I would suggest you to read this article about function pointer, it explains almost all you need to know : http://www.learncpp.com/cpp-tutorial/78-function-pointers/
std::cout << "fp_ptr=" << *fp_ptr << std::endl;
should be
std::cout << "fp_ptr=" << (void*)fp_ptr << std::endl;
The cout operator doesn't have an overload for a function pointer, so it uses bool instead. That's why you always get 1 as output. When I compile your code, I even get a warning for that, telling me that it will always evaluate to true. You should switch on all warnings and try to get rid of them.