内存损坏字符串

时间:2013-01-20 19:16:02

标签: c++ string

我对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")

理想情况下,我最终想要实际做到这一点,但是因为我似乎遇到了错误,所以只是逐步完成std :: string的事情:

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

如何再次将chars,int和std :: strings连接在一起?感谢

P.S。这是我在尝试上面的“\ 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]

2 个答案:

答案 0 :(得分:2)

您可以使用std::stringstream合并stringintchars,如下所示:

#include<iostream>
#include<sstream>

void main()
{

    std::stringstream ss;

    int x = 5;
    std::string y = "Hello";
    char z = 'z';

    ss<<x<<y<<z;

    std::string result = ss.str();

    std::cout<<result;
}

答案 1 :(得分:1)

您可以使用C ++ 11 std::to_string()函数:

#include <string>
#include <iostream>

int main() {

   int const i { 77 };

   std::string const s {
      std::string("Hello") + ',' + std::to_string(i) };

   std::cout << s << std::endl;

   return 0;
}