gpt4 book ai didi

c++ - 双自由或腐败(fasttop):

转载 作者:行者123 更新时间:2023-11-28 07:34:57 25 4
gpt4 key购买 nike

我写了下面的一段代码,得到了上面提到的错误

谁能告诉我哪里错了

谢谢。

#include "iostream"
#include "sstream"
#include <string.h>

int main()
{
std::string temp1 = "454552354772";
char arr[16];
memcpy(arr , &temp1 , temp1.size());
std::string temp2;
memcpy(&temp2 , arr , temp1.size());
std::cout<<temp2;

}

我得到的错误是

*** Error in `./a.out': double free or corruption (fasttop): 0x00000000016c5010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7fa91d359a46]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNSsD1Ev+0x20)[0x7fa91dc78290]
./a.out[0x400bc9]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fa91d2faea5]
./a.out[0x400a09]

最佳答案

您不能以这种方式处理 string 类的对象。

memcpy(arr , &temp1 , temp1.size());

应该是

temp1.copy(arr, temp1.size());

memcpy(&temp2 , arr , temp1.size());

应该是

std::string temp2(arr, temp1.size());

关于c++ - 双自由或腐败(fasttop):,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com