无法修改 const wchar * 的向量

unable to modify a vector of const wchar *

我有一个函数可以将字符串列表转换为 const wchar_t *.

的向量
void convertListtoVector(std::vector<const wchar_t *>& messageInserts)
{
    std::list<std::wstring> Inserts;
    Inserts.push_back(L"str1");
    Inserts.push_back(L"str2");
    Inserts.push_back(L"str3");

    std::list<std::wstring>::iterator itr;
    
    for (itr = Inserts.begin(); itr != Inserts.end(); itr++) {
        messageInserts.push_back((*itr).c_str());
    
    }
}

我是这样称呼它的:

std::vector<const wchar_t *> msg;
convertListtoVector(msg);

但是在函数执行后我得到了一个包含 3 个空字符串的向量。当我调试我的代码时,我注意到在函数内部,向量正在使用字符串进行更新,但是在函数 returns 之后,我看到大小从 0 变为 3,但所有字符串都是空的“”。我想要带有字符串的向量。请让我知道我哪里出错了。 提前致谢。

使用地址清理程序构建程序时,我们检测到内存错误:AddressSanitizer: heap-use-after-free

一般在现代c++中,我们不推荐使用原始指针:我们更喜欢使用容器。

你在函数returns之后存储了为什么归本地std::list所有的字符串地址,这些地址是不可访问的(打印时得到一个空字符串,但实际上它可能会崩溃),要修复它,我们可以有 3 个选择:

  1. 复制
void convertListtoVector(std::vector<std::wstring>& messageInserts)
{
    std::list<std::wstring> Inserts;
    Inserts.push_back(L"str1");
    Inserts.push_back(L"str2");
    Inserts.push_back(L"str3");

    std::list<std::wstring>::iterator itr;
    
    for (itr = Inserts.begin(); itr != Inserts.end(); itr++) {
        messageInserts.push_back(*itr);
    
    }
}

或使用STL算法

void convertListtoVector(std::vector<std::wstring>& messageInserts)
{
    std::list<std::wstring> Inserts;
    Inserts.push_back(L"str1");
    Inserts.push_back(L"str2");
    Inserts.push_back(L"str3");
    messageInserts.insert(messageInserts.end(), Inserts.begin(), Inserts.end());
}
  1. 从本地容器移动,因为不再需要本地列表,对于大字符串,移动它们将获得性能提升
void convertListtoVector(std::vector<std::wstring>& messageInserts)
{
    std::list<std::wstring> Inserts;
    Inserts.push_back(L"str1");
    Inserts.push_back(L"str2");
    Inserts.push_back(L"str3");
    
    for (itr = Inserts.begin(); itr != Inserts.end(); itr++) {
        messageInserts.push_back(std::move(*itr));
    }
}

或使用STL算法

void convertListtoVector(std::vector<std::wstring>& messageInserts)
{
    std::list<std::wstring> Inserts;
    Inserts.push_back(L"str1");
    Inserts.push_back(L"str2");
    Inserts.push_back(L"str3");
    messageInserts.insert(messageInserts.end(), std::make_move_iterator(Inserts.begin()), std::make_move_iterator(Inserts.end()));
}
  1. 使本地 list 静态化,但这可能容易出错:
void convertListtoVector(std::vector<const wchar_t*>& messageInserts)
{
    static std::list<std::wstring> Inserts;
    Inserts.push_back(L"str1");
    Inserts.push_back(L"str2");
    Inserts.push_back(L"str3");

    std::list<std::wstring>::iterator itr; 
    for (itr = Inserts.begin(); itr != Inserts.end(); itr++) {
        messageInserts.push_back(itr->c_str());
    }
}

您的向量存储了一个指向字符串的指针,该字符串的生命周期由 std::list 管理。当 list as local variable 被删除时,所有托管字符串也被删除,你在 vector 中留下了悬空指针。

您必须对列表的字符串内容进行深度复制:

void convertListtoVector(std::vector<const wchar_t *>& messageInserts)
{
    std::list<std::wstring> Inserts;
    Inserts.push_back(L"str1");
    Inserts.push_back(L"str2");
    Inserts.push_back(L"str3");

    std::list<std::wstring>::iterator itr;
    
    for (itr = Inserts.begin(); itr != Inserts.end(); itr++) {
        wchar_t *cloneStr = new wchar_t[wcslen(itr->c_str())+1];
        wcscpy(cloneStr,itr->c_str());
        messageInserts.push_back(cloneStr);
    }
}

int main(){
    std::vector<const wchar_t *> msg;
    convertListtoVector(msg);
    for (const wchar_t* str : msg) {
        std::wcout << str << std::endl;
        delete[] str;
    }
}