Suppose 0xfe2200 is the memory address of a variable var2 and I want to display the value stored in it, E.g
cout<< "Value stored in the given address is : " << 0xfe2200 << " ";
I tried following but all in vain
cout << "Value is : " << *0xfee2200 << " ;
cout << "Value is : " << &0xfee200 << " ;
Assuming the address points to int, you may do:
cout << "Value is : " << *reinterpret_cast<int*>(0xfee2200);
as literal 0xfee2200 is an interger type whereas you expected a pointer.
You have to decide as what type of data you want to interpret the memory content and cast it accordingly:
const char* tmp = "foofoo"; // Valid ptr for this example
const void* address = tmp; // Set to your address
const int* i = reinterpret_cast<const int*>(address);
const unsigned short* us = reinterpret_cast<const unsigned short*>(address);
const char* c = reinterpret_cast<const char*>(address);
std::cout << "i: " << (*i)
<< "\nus: " << (*us)
<< "\nc: " << (*c);
Output:
i: 1718579046
us: 28518
c: f
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;
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 3 years ago.
If char* is a pointer (I find its size is always 4 bytes), how do I find its value (the address in hexa or decimal)? I tried &(*p) for char *p. It simply returned the initial string. If it is always 4 bytes, how is it that it can be initialized to long strings but point to the first character? In other words where is the string stored?
Is char* a weird pointer used for purposes other than what a pointer is intended to be?
// an initial pointer (usually its size is 32bit or 64bit, depending on CPU/OS).
// it's value is currently NULL (not pointing anywhere),
// so we can't do very much with this right now.
char* p = nullptr;
// for the sake of sanity, check the value (should be zero)
// we have to convert to intptr_t, otherwise we'd get the string
// value being printed out.
std::cout << "p address = " << intptr_t(p) << std::endl << std::endl;
// lets allocate a few chars to play with
p = new char[10];
// copy in some text value
std::strcpy(p, "Hello");
// and now if we print the address, the text string,
// and the char we are pointing at
std::cout << "p address = " << intptr_t(p) << std::endl;
std::cout << "p string = " << p << std::endl;
std::cout << "p dereferenced = " << *p << std::endl << std::endl;
// for fun, lets increment the pointer by 1
++p;
// this should have made a couple of changes here
std::cout << "p address = " << intptr_t(p) << std::endl;
std::cout << "p string = " << p << std::endl;
std::cout << "p dereferenced = " << *p << std::endl << std::endl;
// decrement again (so we can delete the correct memory allocation!)
--p;
// now free the original allocation
delete [] p;
// if we print again, notice it still has the memory location?
std::cout << "p address = " << intptr_t(p) << std::endl;
// This would be bad to access (we've just deleted the memory)
// So as a precaution, set the pointer back to null
p = nullptr;
// should be back where we started
std::cout << "p address = " << intptr_t(p) << std::endl;
int var = 5;
int *intptr = &var;
int *intptr1 = intptr;
std::cout << "adress of var : " << intptr << std::endl; //0x00EFFB05
std::cout << "adress of var : " << &var << std::endl; //0x00EFFB05
std::cout << "value of var : " << var << std::endl; // 5
std::cout << "value of var : " << *intptr << std::endl; // 5
std::cout << "adress of intptr: " << &intptr << std::endl; //0x00EFFB44
std::cout << "adress of var : " << intptr1 << std::endl; //0x00EFFB05 /In my unterstanding, the adress of intptr should be here, instead we have the adress of var
In the line where i assign "var" to the "intptr" pointer, the adress of var becomes the value of the pointer.
In the line where i assign my pointer "intptr" to the pointer "intptr1", the adress of "intptr" doesnt become the value of "intptr1", but instead, the VALUE of "intptr" becomes the value of "intptr1", which is the adress of var.
1: Why this change of logic when assigning a pointer to a pointer?
2: And how do i store the address of "intptr" to "intptr1"?
There is no change of logic. If you want to assign an address, you need to use the address-of operator (&). You did this for var but not for intptr, so you get exactly what you asked for.
Try int **intptr1 = &intptr;. Note the difference in the type declaration. intptr1 is not a pointer-to-int; it's a pointer-to-pointer-to-int.
I have a structure and i am trying to print the address of their member variables.
When tried to print the address of char member variable through &(f.c) i am not getting their address.
Here is the code:
struct foo
{
char c;
short s;
void *p;
int i;
};
int main()
{
cout << "Size of foo: " << sizeof(foo) << endl;
foo f;
cout << "Address of c: " << reinterpret_cast<void*>(&f.c) << endl;
cout << "Address of c: " << &(f.c) << endl;
cout << "Address of s: " << reinterpret_cast<void*>(&f.s) << endl;
cout << "Address of s: " << &(f.s) << endl;
cout << "Address of p: " << reinterpret_cast<void*>(&f.p) << endl;
cout << "Address of p: " << &(f.p) << endl;
cout << "Address of i: " << reinterpret_cast<void*>(&f.i) << endl;
cout << "Address of i: " << &(f.i) << endl;
return 1;
}
Output
/pp/cplus/bas ]$ ./a.out
Size of foo: 12
Address of c: 0xffbfe680
Address of c: //----------- &(f.c). Why this is empty..
Address of s: 0xffbfe682
Address of s: 0xffbfe682
Address of p: 0xffbfe684
Address of p: 0xffbfe684
Address of i: 0xffbfe688
Address of i: 0xffbfe688
just want to know why it is not printing when i tried accessing it through &(f.c)
Compiled using gcc version 3.4.6
cout has an operator<< overload for char* which treats the argument like a pointer to a C-string and it tries to print all the characters in that C-string until it gets to a NUL (0) byte. To get around this behaviour, you have to cast the addresses to void* like you are doing every other line.
You have just experienced the reason that arrays are sometimes considered second-class data types because they are treated specially in some situations (i.e. arrays of char are treated differently by some things but not others).
The Address of c: is empty because that's what you get when you try to print a string pointed to by &f.c. As dark_charlie pointed out, using an uninitialised variable is undefined behaviour, so technically anything can happen, but the former is probably the explanation for what you're seeing (though we can only guess).
The reason is that without the reinterpret cast &(f.c) is a char* pointer which is treated as a string by cout. Because you haven't filled the char with anything, you invoke an undefined behavior (i.e. it can print anything).
Every time I try to compile my code I get error:
cannot convert parameter 1 from 'int *' to 'int *&'
The test code looks like this:
void set (int *&val){
*val = 10;
}
int main(){
int myVal;
int *pMyVal = new int;
set(&myVal); // <- this causes trouble
set(pMyVal); // <- however, this doesn't
}
I'd like to call that function in a single shot without creating a pointer somewhere only to pass it. And as pointers don't have constructors, something like this can't be done: set(int*(&myVal));
Is there any other way to pass a pointer by reference without needing to create a temporary variable?
Edit: By the way I know why the code fails to compile (I'm just passing the address which is possibly int and not an actual pointer). The question is how else can it be done.
A reference to non-const cannot bind to an rvalue. The result of the & operator is an rvalue. Take a look at the difference between lvalues and rvalues or read a good C++ book.
Also, in your context, you don't need to pass by reference. The following is OK as well:
void set (int *val){
*val = 10;
}
The reference would be needed if you were to do something like this;
void set (int*& val){
val = new int; //notice, you change the value of val, not *val
*val = 10;
}
&myval is an rvalue (of type int*), because it's a temporary. It's a pointer, but you cannot modify it, because it's just created on the fly. Your function set however requires a non-const reference, so you cannot pass it a temporary.
By contrast, pMyVal is a named variable, thus an lvalue, so it can be passed as a non-constant reference.
A very simple example can be found in this place.
http://markgodwin.blogspot.de/2009/08/c-reference-to-pointer.html
You can see the following sample code:
#include <iostream>
using namespace std;
void change(int*& ptr) {
cout << endl;
cout << "==================change(int*& ptr)====================" << endl;
cout << " &ptr = " << &ptr << endl;
cout << " ptr = " << ptr << endl;
cout << "=======================================================" << endl;
cout << endl;
*ptr *= *ptr;
}
int main(void) {
int* ptrNumber = new int(10);
cout << endl;
cout << "&ptrNumber = " << &ptrNumber << endl;
cout << "ptrNumber = " << ptrNumber << endl;
cout << ">>> *ptrNumber = " << *ptrNumber << endl;
change(ptrNumber);
cout << "<<< *ptrNumber = " << *ptrNumber << endl;
}
I installed Cygwin and used g++ to compile the above source code, binary file is out_pointer.exe.
Executing out_pointer.exe, output is as follows:
$ ./out_pointer.exe
&ptrNumber = 0x28ac3c
ptrNumber = 0x800102c0
>>> *ptrNumber = 10
==================change(int*& ptr)====================
&ptr = 0x28ac3c
ptr = 0x800102c0
=======================================================
<<< *ptrNumber = 100
From the above output, we see
&ptrNumber = &ptr
So, ptr is alias of ptrNumber. You can modify ptrNumber inside function void change(int*& ptr) by modifying ptr. For example, you can point ptr to another memory location as below:
#include <iostream>
using namespace std;
void change(int*& ptr) {
cout << endl;
cout << "==================change(int*& ptr)====================" << endl;
cout << " &ptr = " << &ptr << endl;
cout << " >>> ptr = " << ptr << endl;
ptr = new int(20);
cout << " <<< ptr = " << ptr << endl;
cout << "=======================================================" << endl;
cout << endl;
}
int main(void) {
int* ptrNumber = new int(10);
cout << endl;
cout << ">>> &ptrNumber = " << &ptrNumber << endl;
cout << ">>> ptrNumber = " << ptrNumber << endl;
cout << ">>> *ptrNumber = " << *ptrNumber << endl;
change(ptrNumber);
cout << "<<< &ptrNumber = " << &ptrNumber << endl;
cout << "<<< ptrNumber = " << ptrNumber << endl;
cout << "<<< *ptrNumber = " << *ptrNumber << endl;
}
New output:
$ ./out_pointer.exe
>>> &ptrNumber = 0x28ac3c
>>> ptrNumber = 0x800102c0
>>> *ptrNumber = 10
==================change(int*& ptr)====================
&ptr = 0x28ac3c
>>> ptr = 0x800102c0
<<< ptr = 0x80048328
=======================================================
<<< &ptrNumber = 0x28ac3c
<<< ptrNumber = 0x80048328
<<< *ptrNumber = 20
The problem is, int*&val can only be passed an lvalue, which the result of &myVal is not. By changing the signature to void set(int* const& val), it's telling the compiler you're not going to change the value of the pointer.
However, you normally wouldn't do that, only because if you're not going to change the value of the pointer, then passing the pointer by value is the most straightforward way to pass the value. And if you are going to change the value of the pointer, then you need to create a temporary to receive the result.