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.
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'm a python programmer new to C++. Currently getting a segfault when trying to play around with pointers. Can someone explain this behavior? I suspect there's something significant about cout that I'm not understanding.
#include<iostream>
using namespace std;
int main() {
int var; // declaration
cout << "var declared" << endl;
cout << "var : " << var << endl;
cout << "&var : " << &var << endl;
var = 10; // initialization
cout << "\nvar initialized" << endl;
cout << "var : " << var << endl;
cout << "&var : " << &var << endl;
int* ptr; // declaration
cout << "\nptr declared" << endl;
cout << "ptr : " << ptr << endl;
cout << "*ptr : " << *ptr << endl;
cout << "&ptr : " << &ptr << endl;
ptr = &var; // initialization
cout << "\nptr initialized : " << endl;
cout << "ptr : " << ptr << endl;
cout << "*ptr : " << *ptr << endl;
cout << "&ptr : " << &ptr << endl;
return 0;
}
using this compiler command
g++ --std=c++14 main.cpp -o main_exec;
This code produces the following output
var declared
var : 0
&var : 0x7fff55724478
var initialized
var : 10
&var : 0x7fff55724478
ptr declared
ptr : 0x0
[1] 82727 segmentation fault ./main_exec
This code obviously compiles but produces a segfault at runtime.
Things I've tried
I've tried combinations of commenting out the lines that include *ptr and the lines that include &ptr, short story is some combinations produce no segfault. It seems that I can use *ptr 1 time and not in combination with &ptr
You declared a pointer to an integer, but not initialized or assigned it with any pointer (i.e. to memory address of an integer variable).
int* ptr; // declaration
The line above says, ptr is a pointer (that would point to memory address of an integer variable). In your case it just says it is a pointer, what it does point is not defined.
When you hit this line,
cout << "*ptr : " << *ptr << endl;
Here *ptr means, get the value at address (the address that actually would store the value) and is being pointed by ptr. However ptr points to nothing. So you're trying to access memory in a way that is not permitted which leads to the segmentation fault.
Just remember that pointers are variable that as their values store memory address of another variable.
int *ptr; // would store memory address of an integer type variable.
int a = 5; // would store an integer variable.
ptr = &a; // Here &a gets the address of variable a and stores in *ptr
cout<<*ptr // Here *ptr gets the value of variable whose memory address is pointed to by *ptr.
Edit
To answer the question if int * ptr = new int; is valid.
Yes it will reserve you a memory location and allows you to later store values on that location later, as shown in code below,
int* ptr = new int; // gets memory address and assigns to *ptr
*ptr = 5; // assigns value 5 to the memory being pointed by *ptr
cout<<*ptr; // output 5
However you're supposed to not do this unless you've a particular need.
Variables reserve you memory, and give you a friendly variable name of your choice.
Anything you create with new is not deleted automatically, as suggested in comment section.
Variables, in contrast to this, are deleted automatically when they get out of scope (block, function, loop, etc.)
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;
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 7 years ago.
I have this maybe weird question. I was considering following example where I pass a pointer by reference so if I change the pointer address of the argument the initial pointer should also change the address. And indeed this happens.
However the value of a in the main method will have some random value and not 3 as the q variable inside changePointer whose address we use to change pointer a to.
Is there something stupid oversee or is the whole thing just undefined behaviour.
#include <iostream>
void changePointer(int* &p) {
int q = 3;
std::cout << "Address of q: " << &q << std::endl;
p = &q;
std::cout << "Value of p: " << *p << std::endl;
}
int main() {
int* a = new int(4);
changePointer(a);
std::cout << "Adress of a: " << a << std::endl;
std::cout << "Value of a: " << *a << std::endl;
}
This is undefined behavior because you access a local variable which is already vanished.
Try adding static before int q = 3; and making the variable static so that the variable won't vanish on returning from the function.
Also, please do not cause memory leak by allocating some buffer to a and throwing it away!
q is scoped variable, it gets destroyed when changePointer returns. a then points to freed memory, so it's invalid pointer, and dereferencing it (*a) is Undefined Behavior.
Corrected code:
void changePointer(int* &p) {
int* q = new int(3); // this way q lives until you delete it
std::cout << "Address of q: " << q << std::endl;
delete p; // we don't want unfreeable memory
p = q;
std::cout << "Value of p: " << *p << std::endl;
}
int main() {
int* a = new int(4);
changePointer(a);
std::cout << "Adress of a: " << a << std::endl;
std::cout << "Value of a: " << *a << std::endl;
delete a;
}
q is a local variable inside changePointer, so when changePointer exits the address of q contains garbage.
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