C++11 将指针内容复制到堆栈变量
我想到了这个疑问。这可能吗? 考虑这个代码:C++11 将指针内容复制到堆栈变量,c++11,pointers,memory,C++11,Pointers,Memory,我想到了这个疑问。这可能吗? 考虑这个代码: int* p; int j = 9; p = &j; // is it possible to declare int x and store inside 9? // so if I do delete p; that value is stored in x 提前感谢并向您致意您完全可以读取p引用的值,并将其复制到另一个int中,如下所示: int x = *p; // The value has been copied, j and
int* p;
int j = 9;
p = &j;
// is it possible to declare int x and store inside 9?
// so if I do delete p; that value is stored in x
提前感谢并向您致意您完全可以读取
p
引用的值,并将其复制到另一个int
中,如下所示:
int x = *p;
// The value has been copied, j and hence *p aren't affected by changes to x:
++x;
assert(x > *p);
但是,您肯定不想
删除p
,因为它不是用new
堆分配的。相反,p
指向j
,并且j
的生存期由周围范围自动管理。您所说的“声明int x并在9内存储”是什么意思?int x=*j;像这样的