gpt4 book ai didi

c++ - 字符串的内存损坏

转载 作者:行者123 更新时间:2023-11-28 00:50:23 26 4
gpt4 key购买 nike

<分区>

我对 C++ 生疏了,最近一直在使用 python/php,这让一切变得太容易了哈哈。我在尝试执行此操作时出现内存损坏:

// values is passed to the function as std::string &values
// and values is declared earlier as std::string values = "";
values += std::string("\n")

理想情况下,我最终想真正做到这一点,但我只是一 block 一 block 地做 std::string 的东西,因为我似乎遇到了错误:

// data is an int array
values += std::string("\n") + data[l] + " " + data[j-1];

如何再次将字符、整数和 std::string 连接在一起?谢谢

附言这是我在尝试执行上面的“\n”时得到的 gdb 堆栈:

34                                      values += std::string("\n");// + data[l] + " " + data[j-1];
(gdb)
*** glibc detected *** /{HIDDEN}/main: malloc(): memory corruption (fast): 0x0000000000603030 ***
======= Backtrace: =========
/lib64/libc.so.6[0x38ec07b776]
/lib64/libc.so.6[0x38ec07e107]
/lib64/libc.so.6(__libc_malloc+0x63)[0x38ec07f443]
/lib64/libstdc++.so.6(_Znwm+0x1d)[0x38eec5f59d]
/lib64/libstdc++.so.6(_ZNSs4_Rep9_S_createEmmRKSaIcE+0x59)[0x38eecbc339]
/lib64/libstdc++.so.6(_ZNSs12_S_constructIPKcEEPcT_S3_RKSaIcESt20forward_iterator_tag+0x35)[0x38eecbdd25]
/lib64/libstdc++.so.6(_ZNSsC2EPKcRKSaIcE+0x43)[0x38eecbde03]
/{HIDDEN}/main[0x400cb6]
/{HIDDEN}/main[0x400ba7]
/{HIDDEN}/main[0x400eeb]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x38ec021a05]
/{HIDDEN}/main[0x400a29]
======= Memory map: ========
00400000-00402000 r-xp 00000000 00:23 549601814 /{HIDDEN}/main
00601000-00602000 r--p 00001000 00:23 549601814 /{HIDDEN}/main
00602000-00603000 rw-p 00002000 00:23 549601814 /{HIDDEN}/main
00603000-00624000 rw-p 00000000 00:00 0 [heap]
38ebc00000-38ebc20000 r-xp 00000000 08:07 7604569 /usr/lib64/ld-2.16.so
38ebe20000-38ebe21000 r--p 00020000 08:07 7604569 /usr/lib64/ld-2.16.so
38ebe21000-38ebe22000 rw-p 00021000 08:07 7604569 /usr/lib64/ld-2.16.so
38ebe22000-38ebe23000 rw-p 00000000 00:00 0
38ec000000-38ec1ad000 r-xp 00000000 08:07 7604570 /usr/lib64/libc-2.16.so
38ec1ad000-38ec3ad000 ---p 001ad000 08:07 7604570 /usr/lib64/libc-2.16.so
38ec3ad000-38ec3b1000 r--p 001ad000 08:07 7604570 /usr/lib64/libc-2.16.so
38ec3b1000-38ec3b3000 rw-p 001b1000 08:07 7604570 /usr/lib64/libc-2.16.so
38ec3b3000-38ec3b8000 rw-p 00000000 00:00 0
38ed000000-38ed100000 r-xp 00000000 08:07 7604585 /usr/lib64/libm-2.16.so
38ed100000-38ed2ff000 ---p 00100000 08:07 7604585 /usr/lib64/libm-2.16.so
38ed2ff000-38ed300000 r--p 000ff000 08:07 7604585 /usr/lib64/libm-2.16.so
38ed300000-38ed301000 rw-p 00100000 08:07 7604585 /usr/lib64/libm-2.16.so
38ed800000-38ed815000 r-xp 00000000 08:07 7604600 /usr/lib64/libgcc_s-4.7.2-20121109.so.1
38ed815000-38eda14000 ---p 00015000 08:07 7604600 /usr/lib64/libgcc_s-4.7.2-20121109.so.1
38eda14000-38eda15000 r--p 00014000 08:07 7604600 /usr/lib64/libgcc_s-4.7.2-20121109.so.1
38eda15000-38eda16000 rw-p 00015000 08:07 7604600 /usr/lib64/libgcc_s-4.7.2-20121109.so.1
38eec00000-38eece5000 r-xp 00000000 08:07 7604613 /usr/lib64/libstdc++.so.6.0.17
38eece5000-38eeee4000 ---p 000e5000 08:07 7604613 /usr/lib64/libstdc++.so.6.0.17
38eeee4000-38eeeec000 r--p 000e4000 08:07 7604613 /usr/lib64/libstdc++.so.6.0.17
38eeeec000-38eeeee000 rw-p 000ec000 08:07 7604613 /usr/lib64/libstdc++.so.6.0.17
38eeeee000-38eef03000 rw-p 00000000 00:00 0
7ffff7fd6000-7ffff7fdb000 rw-p 00000000 00:00 0
7ffff7ffc000-7ffff7ffe000 rw-p 00000000 00:00 0
7ffff7ffe000-7ffff7fff000 r-xp 00000000 00:00 0 [vdso]
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

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