lvalue required as unary ‘&’ operand以及改动后的Segmentation fault

本文探讨了在C++编程中遇到的'lvalue required as unary ‘&’ operand'错误和后续的Segmentation fault问题。通过示例代码,解释了错误产生的原因,特别是当使用reinterpret_cast和指针操作时的注意事项。文章指出,错误通常源于尝试对空指针取地址或者对临时对象取地址。解决方案是确保在取地址操作前,指针已经被正确初始化并指向有效的内存区域。
lvalue required as unary ‘&’ operand
#include <iostream>

struct A {
  int data_;
};

void CreateA(A** a_addr) {
  *a_addr = new A;
}

void DestoryA(A* a) {
  if (a) {
    delete a;
    a = nullptr;  
  }
}

int main(int argc, char** argv) {
  A* a;
  CreateA(&a);
  a->data_ = 10;
  std::cout << a->data_ << std::endl;
  DestoryA(a);  
  return 0;
}

这时候我不想固定a的具体的类型
我就想将A*指针转成void*

void* ptr = nullptr;
  A* a;
  CreateA(&a);
  ptr = reinterpret_cast<void *>(a);
  a->data_ = 10;
  std::cout << reinterpret_cast<A*>(ptr)->data_ << std::endl;
  DestoryA(a);

那么代码就变成这个鬼样子了。
那么我想进一步懒一下

void* ptr = nullptr;
  CreateA(&reinterpret_cast<A*>(ptr));
  std::cout << reinterpret_cast<A*>(ptr)->data_ << std::endl;
  DestoryA(reinterpret_cast<A*>(ptr));

这时候就报这个错误

lvalue required as unary ‘&’ operand

当然下面这种情况也是不行的

void* ptr = nullptr;
  CreateA(&reinterpret_cast<A*>(ptr));
  DestoryA(reinterpret_cast<A*>(ptr));


这个操作说什么左值,其实就是&reinterpret_cast,相当于对寄存器里面的数据取地址,相当于&void。那么我们应该先将reinterpret_cast的结果move出来然后取地址就好了。

void* ptr = nullptr;
  A* a = reinterpret_cast<A*>(ptr);
  CreateA(&a);
  std::cout << reinterpret_cast<A*>(ptr)->data_ << std::endl;
  DestoryA(reinterpret_cast<A*>(ptr));
Segmentation fault

但是这样写还会报一个错Segmentation fault (core dumped)
我们发现 CreateA DestoyA都可以正常使用,但是具体使用ptr->data_就会出错。这里其实ptr从始至终都是null。

void* ptr = nullptr;
  A* a = nullptr;
  CreateA(&a);
  a->data_ = 10;
  ptr = reinterpret_cast<void*>(a);
  printf("ptr:%p, a:%p\n", ptr, a);
  std::cout << reinterpret_cast<A*>(ptr)->data_ << std::endl;
  DestoryA(reinterpret_cast<A*>(ptr));

为啥出错了呢

这里有点不同
我开始void* ptr = nullptr
当我使用 A* a = reinterpret_cast<A*>(ptr);, 然后用a的地址去申请空间,实际上这样操作了,虽然 a做了 a = new A();操作,但是ptr一直为空。
主要是要区别直接申请一块地址然后使用reinterpret_cast情况,这中情况实际上是有地址的,reinterpret_cast只是给这个地址换了个别名。
而如果void* ptr = nullptr然后使用A* a = reinterpret_cast<A*>(ptr); 其实就相当于A* a = nullptr,其实和ptr没有关系,ptr始终== nullptr.
其实只需要再使用了ptr = a,对ptr进行赋值就好了。
讲了这么多,这种情况就是要区别实际有地址的reinterpret_cast和没有地址的进行操作。有地址是,那么这个只是别名,没地址,那不就是``骗我,69岁的老同志嘛```。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值