【发布时间】:2021-09-13 15:48:21
【问题描述】:
代码编辑:https://gcc.godbolt.org/z/x3hnj46WY
场景-1 试图通过引用(或指针)传递原始指针 无法使用 & 将 get() 的值传递给 setBuffer() // 编译错误:需要左值作为一元 '&' 操作数
场景-2 通过获取原始字符指针分配值返回 使用 &
将 char 指针传递给 setBuffer()注意:setBuffer() 存在于 C 库中 我正在使用这个 API https://www.libssh2.org/libssh2_session_last_error.html 来获取 errmsg 和 我想使用 char 数组的智能指针,而不是使用 char 数组缓冲区。
#include <string.h>
#include <iostream>
#include <memory>
int setBuffer(char ** ptr) {
// ASSUMPTION: *ptr has sufficient memory to store copied data
strcpy(*ptr, "sample string");
return 0;
}
int main()
{
std::unique_ptr<char []> lup = std::make_unique<char []>(1024);
memset(lup.get(), 0 , 1024);
strcpy(lup.get(), "sample string - 1");
std::cout << lup << '\n'; // c++20
// SCENARIO - 1 | Compilation error
setBuffer(&lup.get()); // CE: lvalue required as unary '&' operand
// SCENARIO - 2 | Works fine
char * lp = lup.get();
setBuffer(&lp);
std::cout << lup << '\n'; // c++20
return 0;
}
【问题讨论】:
-
我很好奇你为什么选择
std::unique_ptr<char []>而不是std::string? -
在
libssh2_session_last_error的上下文中,该函数将自行设置指针。里面会有一个像*errmsg = some_internal_string_buffer;这样的赋值,这意味着你对智能指针的使用无论如何都不会起作用(实际上会导致问题,因为lup对象不会收到关于指针更改的通知)。如果要将返回的字符串作为 C++ 对象,则需要使用普通的原始非拥有指针开头,然后使用它来初始化std::string(例如)。 -
在实际程序中,我传递了一个 char [] 缓冲区,但出于学习目的,我想检查在从 C++ 调用此类 C 函数(如 setBuffer())时如何使用智能指针
标签: c++ unique-ptr raw-pointer c-libraries