如何查找auto_ptr的地址
How to find the address of an auto_ptr
我有一个函数,它接受unsigned char**
作为参数。我想传递一个使用auto_ptr定义的参数内存。我正在尝试这样的代码:
std::auto_ptr<unsigned char> pImageBuffer;
func(&(pImageBuffer.get()));
但是,看起来我在这里做错了什么。我得到一个错误说:
Cannot take the address of an rvalue of type 'element_type *' (aka 'unsigned char *')
你知道我该怎么做吗?我不能使用unique_ptr
或更改函数的签名。
听起来,传递指针地址的函数需要修改所指向的地址(而不是底层对象)。您不能直接通过auto_ptr
执行此操作(您试图获取auto_ptr::get
的返回值的地址——这是非法的,因为它是rvalue
)。您需要先创建一个临时变量来保存原始指针。我推荐这个:
unsigned char* tmpPtr;
func(&tmpPtr);
std::auto_ptr<unsigned char> pImageBuffer(tmpPtr);
当然,这是假设函数希望您拥有它将tmpPtr
设置为指向的东西的所有权
相关文章:
- 将数组的地址分配给变量并删除
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 如何在c++程序中找到函数的地址
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 被解释为低级别const的const对象的地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 内联程序集printf将整数解释为地址
- 为什么指针不写入类的地址?
- 如何在C++中获取该对象的类声明中对象的地址?
- 通过按地址访问变量
- CLANG 编译器 说:变量"PTR"可能未初始化
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 函数名是c中该函数的第一条指令的地址吗
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- ReadProcessMemory() 不适用于像 0x2840C6C68D8 这样的长地址
- 如果您使一个char*ptr存储整数变量的地址,会发生什么
- 为什么int** ptr在访问ptr[i][j]时不指向与int arr[3][3]相同的地址?