Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在什么情况下,两个堆栈分配的结构对象的this点指向同一地址? 我在阅读C++中如何分配内存时遇到了一些代码。但我不明白为什么两个ValueHolder指针(x,y)指向同一个地址。代码的真正含义是什么 struct ValueHolder{ int value; operator ValueHolder* () { return this; } // just typecast }; void objects_stored_by_pointers_with_autocleanup() { ValueHolder * x = ValueHolder(); ValueHolder * y = ValueHolder(); x->value = 7; y->value = 9; printf("%d\n", x->value); printf("%d\n", y->value); printf("%p\n", (ValueHolder*)x); printf("%p\n", (ValueHolder*)y); } int main(int argc, char* argv[]){ objects_stored_by_pointers_with_autocleanup(); }_C++ - Fatal编程技术网

在什么情况下,两个堆栈分配的结构对象的this点指向同一地址? 我在阅读C++中如何分配内存时遇到了一些代码。但我不明白为什么两个ValueHolder指针(x,y)指向同一个地址。代码的真正含义是什么 struct ValueHolder{ int value; operator ValueHolder* () { return this; } // just typecast }; void objects_stored_by_pointers_with_autocleanup() { ValueHolder * x = ValueHolder(); ValueHolder * y = ValueHolder(); x->value = 7; y->value = 9; printf("%d\n", x->value); printf("%d\n", y->value); printf("%p\n", (ValueHolder*)x); printf("%p\n", (ValueHolder*)y); } int main(int argc, char* argv[]){ objects_stored_by_pointers_with_autocleanup(); }

在什么情况下,两个堆栈分配的结构对象的this点指向同一地址? 我在阅读C++中如何分配内存时遇到了一些代码。但我不明白为什么两个ValueHolder指针(x,y)指向同一个地址。代码的真正含义是什么 struct ValueHolder{ int value; operator ValueHolder* () { return this; } // just typecast }; void objects_stored_by_pointers_with_autocleanup() { ValueHolder * x = ValueHolder(); ValueHolder * y = ValueHolder(); x->value = 7; y->value = 9; printf("%d\n", x->value); printf("%d\n", y->value); printf("%p\n", (ValueHolder*)x); printf("%p\n", (ValueHolder*)y); } int main(int argc, char* argv[]){ objects_stored_by_pointers_with_autocleanup(); },c++,C++,预期结果: 7 9 0x55afde2aae70 0x55afde2aae90 实际结果: 9 9 0x7ffeb6d8fcf4 0x7ffeb6d8fcf4 你所看到的一切都是由于 表达式ValueHolder()创建一个临时对象,该对象立即被销毁。初始化完成后,转换运算符返回的指针将立即无效 因为对象是临时的,所以它允许编译器在下次创建临时对象时重用相同的内存 代码的真正含义是什么 struct ValueHolder{ int value; operator ValueHold

预期结果:

7
9
0x55afde2aae70
0x55afde2aae90
实际结果:

9
9
0x7ffeb6d8fcf4
0x7ffeb6d8fcf4

你所看到的一切都是由于

表达式
ValueHolder()
创建一个临时对象,该对象立即被销毁。初始化完成后,转换运算符返回的指针将立即无效

因为对象是临时的,所以它允许编译器在下次创建临时对象时重用相同的内存

代码的真正含义是什么

struct ValueHolder{
  int value;
  operator ValueHolder* () { return this; } // just typecast 
};

void objects_stored_by_pointers_with_autocleanup() {
  ValueHolder * x = ValueHolder();
  ValueHolder * y = ValueHolder();
  x->value = 7;
  y->value = 9;
  printf("%d\n", x->value);
  printf("%d\n", y->value);
  printf("%p\n", (ValueHolder*)x);
  printf("%p\n", (ValueHolder*)y);
}   
int main(int argc, char* argv[]){
  objects_stored_by_pointers_with_autocleanup();
}
ValueHolder()
创建一个临时对象,在完整表达式被销毁后,占用的内存也被释放。在此之后,指针
x
y
立即悬挂起来,对它们的任何取消引用,如
x->value
y->value
都会导致UB

在您的情况下,第二个临时对象的地址似乎与第一个已被销毁的对象的地址相同,这很好,但请注意,这并不能保证