0

I am rusty with C++ and have been using python/php too much recently which makes everything too easy haha. I'm having a memory corruption when I was trying to do this:

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

Ideally I want to actually do this in the end, but was just doing the std::string stuff piece by piece since I seem to be running into errors:

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

How can i concatenate chars, ints, and std::strings together again? Thanks

P.S. This was the gdb stack i got when trying to just do "\n" above:

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]
MasterGberry
  • 2,800
  • 6
  • 37
  • 56
  • This provides some possibilities: http://stackoverflow.com/questions/10516196/append-an-int-to-a-stdstring/10516313#10516313 – hmjd Jan 20 '13 at 19:19
  • What is the **type** of `data[]`. Depending on what the type of data[] is looks OK though potentially inefficient. Normally we use stringstream for concatenating multiple items into a string (though += and + should work (assuming the types are OK) they should definitely not give you exceptions). – Martin York Jan 20 '13 at 19:19
  • 1
    Note: It is more likely that you have another error. It would be useful if you gave a simplified compiling example of your code that generates the error. Note 2: simplifying the code for inclusion here will usually find you the error. – Martin York Jan 20 '13 at 19:23

2 Answers2

2

You can use std::stringstream to combine string, int, and chars as follows:

#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;
}
sgarizvi
  • 16,623
  • 9
  • 64
  • 98
  • Silly me, forgot all about stringstream, like I said it's been too long haha. Thanks, will mark as best answer in a few minutes when I can :) – MasterGberry Jan 20 '13 at 19:22
  • 2
    This _is_ the best answer. The others are rubbish. – Lightness Races in Orbit Jan 20 '13 at 19:26
  • So I am suffering from a seg fault, and i'm completely lost how it could be occurring. I modified my program to pass a reference to a `stringstream` instead of a string now, and have it adding this: `values << "\n" << data[l] << " " << data[j-1];` For some reason at the end of the program when it does `values.str();` it seg faults on the last pair of numbers that this is recording....I checked that `data[j-1]` and `data[l]` are valid entries, and they are, should be printing `5 4` to the cmd line, any ideas? – MasterGberry Jan 20 '13 at 19:38
  • 1
    @MasterGberry: please post the complete source code. – Andreas Florath Jan 20 '13 at 19:55
  • I'm pretty sure that for the code in the question, stringstream is not needed. – Mooing Duck Jan 20 '13 at 20:02
  • @AndreasFlorath http://pastebin.com/5ki8f1Qf – MasterGberry Jan 20 '13 at 20:14
  • 1
    @MasterGberry: There are some problems in your code - it is only one effect that you get the segmation fault. Using valgrind shows up: line 25: `Conditional jump or move depends on uninitialised value(s)`, line 47: `Invalid write of size 4` and so on... – Andreas Florath Jan 20 '13 at 20:47
  • @AndreasFlorath i'm a bit confused, i recognize what you posted as assembly language but I have no idea how you got it, and whether the line #'s are of the compiled code or of the regular source code. Anyways looking over it I don't see anything that isn't initialized to begin with...in main `test` is intiailized, `values` i assume is defaultly constructed, and the rest seem to be intialized.... – MasterGberry Jan 20 '13 at 21:01
  • 1
    @MasterGberry: I was using a tool which checks memory. It is called `valgrind`. I'm not sure which OS you are using and if valgrind is available for this. The stacktrace looks like Linux. If so, you can give a try: install valgrind, and run it with `valgrind yourProgram`. It will print out a list of problems it finds. – Andreas Florath Jan 20 '13 at 21:09
  • @AndreasFlorath i installed it but seem to be having problems to get it to run.... `[root@tudev prog1]# valgrind --leak-check main valgrind: main: command not found` `main` is the name of my program – MasterGberry Jan 20 '13 at 21:25
  • 1
    @MasterGberry: Maybe try `valgrind ./main` then. – Andreas Florath Jan 20 '13 at 21:34
  • @AndreasFlorath well i managed to get it to work, and then realized i was passing one too many length on my program which was breaking everything lol. Ty – MasterGberry Jan 20 '13 at 22:01
1

You can use the C++11 std::to_string() function:

#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;
}
Andreas Florath
  • 4,418
  • 22
  • 32