lvalue required as unary ‘&’ operand以及改動後的Segmentation fault
阿新 • • 發佈:2020-11-27
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歲的老同志嘛```。