c++ double free or corruption Error - c++

When I run a simple c++ program on my on Ubuntu system, I get double free or corruption error. This has been haunting me for a while now.
I have marked the line that gives me this error. When I comment that line, I get no error. How can I get rid of this error? Plz help!
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int t;
int count = 0;
string a, b;
vector<int> v(57, 0);
cin >> t;
while(t--) {
cin >> a >> b;
for (int i = 0; i < a.size(); i++) {
v[a[i] - 65]++;
cout << char(a[i]) << " " << v[a[i] - 65] << endl;
}
cout << endl;
for (int j = 0; j < b.size(); j++) {
int id = b[j] - 65;
if (v[id] > 0) {
int abc = v[id];
v[id] = abc - 1; /*****ERROR******/
cout << char(b[j]) << " ";
count++;
}
}
cout << count << endl;
cout << "----" << endl;
count = 0;
}
return 0;
}
After enter the number of test cases and 2 string as input, I get the following error:
1
abcd
xyz
a 1
b 1
c 1
d 1
z 1
----
*** Error in `./a.out': double free or corruption (!prev): 0x09db6008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767e2)[0xb74b97e2]
/lib/i386-linux-gnu/libc.so.6(+0x77530)[0xb74ba530]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb765db4f]
./a.out[0x8049147]
./a.out[0x8049091]
./a.out[0x8048f99]
./a.out[0x8048ef6]
./a.out[0x8048d79]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb745c935]
./a.out[0x80489a1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:05 4195506 /home/vijender/codechef/a.out
0804a000-0804b000 r--p 00001000 08:05 4195506 /home/vijender/codechef/a.out
0804b000-0804c000 rw-p 00002000 08:05 4195506 /home/vijender/codechef/a.out
09db6000-09dd7000 rw-p 00000000 00:00 0 [heap]
b73fe000-b7400000 rw-p 00000000 00:00 0
b7400000-b7441000 r-xp 00000000 08:05 3670121 /lib/i386-linux-gnu/libm-2.17.so
b7441000-b7442000 r--p 00040000 08:05 3670121 /lib/i386-linux-gnu/libm-2.17.so
b7442000-b7443000 rw-p 00041000 08:05 3670121 /lib/i386-linux-gnu/libm-2.17.so
b7443000-b75f1000 r-xp 00000000 08:05 3670122 /lib/i386-linux-gnu/libc-2.17.so
b75f1000-b75f3000 r--p 001ae000 08:05 3670122 /lib/i386-linux-gnu/libc-2.17.so
b75f3000-b75f4000 rw-p 001b0000 08:05 3670122 /lib/i386-linux-gnu/libc-2.17.so
b75f4000-b75f8000 rw-p 00000000 00:00 0
b75f8000-b7613000 r-xp 00000000 08:05 3670878 /lib/i386-linux-gnu/libgcc_s.so.1
b7613000-b7614000 r--p 0001a000 08:05 3670878 /lib/i386-linux-gnu/libgcc_s.so.1
b7614000-b7615000 rw-p 0001b000 08:05 3670878 /lib/i386-linux-gnu/libgcc_s.so.1
b7615000-b76f1000 r-xp 00000000 08:05 268046 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76f1000-b76f2000 ---p 000dc000 08:05 268046 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76f2000-b76f6000 r--p 000dc000 08:05 268046 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76f6000-b76f7000 rw-p 000e0000 08:05 268046 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76f7000-b76fe000 rw-p 00000000 00:00 0
b7717000-b771c000 rw-p 00000000 00:00 0
b771c000-b771d000 r-xp 00000000 00:00 0 [vdso]
b771d000-b773d000 r-xp 00000000 08:05 3670123 /lib/i386-linux-gnu/ld-2.17.so
b773d000-b773e000 r--p 0001f000 08:05 3670123 /lib/i386-linux-gnu/ld-2.17.so
b773e000-b773f000 rw-p 00020000 08:05 3670123 /lib/i386-linux-gnu/ld-2.17.so
bfa67000-bfa88000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)

I believe the size of v is off by one; the biggest index you can get is 56, but 'z'-65 = 57. I'd recommend using an unordered_map instead of a vector, it also extends better to Unicode characters :).

Related

C++: Error with MergeSort implementation with arrays: malloc(): memory corruption (fast): for

I'm working on an implementation of Merge Sort in C++, and I'm getting the following error when I run my MergeSort function:
*** glibc detected *** ./p1: malloc(): memory corruption (fast): 0x0000000001a250d0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x386ce75e5e]
/lib64/libc.so.6[0x386ce7a528]
/lib64/libc.so.6(__libc_malloc+0x5c)[0x386ce7ab1c]
/usr/lib64/libstdc++.so.6(_Znwm+0x1d)[0x343febd0cd]
/usr/lib64/libstdc++.so.6(_ZNSs4_Rep9_S_createEmmRKSaIcE+0x59)[0x343fe9c3c9]
/usr/lib64/libstdc++.so.6(_ZNSs4_Rep8_M_cloneERKSaIcEm+0x2b)[0x343fe9d19b]
/usr/lib64/libstdc++.so.6(_ZNSs7reserveEm+0x3c)[0x343fe9d5ec]
/usr/lib64/libstdc++.so.6(_ZNSs6appendERKSs+0x4f)[0x343fe9dabf]
./p1[0x403b35]
./p1[0x401ef9]
/lib64/libc.so.6(__libc_start_main+0x100)[0x386ce1ed20]
./p1[0x401879]
======= Memory map: ========
00400000-00406000 r-xp 00000000 fd:00 12322163 /home/hmp54/p1b/p1
00605000-00606000 rw-p 00005000 fd:00 12322163 /home/hmp54/p1b/p1
01a25000-01a46000 rw-p 00000000 00:00 0 [heap]
343fe00000-343fee8000 r-xp 00000000 fd:00 1062181 /usr/lib64/libstdc++.so.6.0.13
343fee8000-34400e8000 ---p 000e8000 fd:00 1062181 /usr/lib64/libstdc++.so.6.0.13
34400e8000-34400ef000 r--p 000e8000 fd:00 1062181 /usr/lib64/libstdc++.so.6.0.13
34400ef000-34400f1000 rw-p 000ef000 fd:00 1062181 /usr/lib64/libstdc++.so.6.0.13
34400f1000-3440106000 rw-p 00000000 00:00 0
386ca00000-386ca20000 r-xp 00000000 fd:00 6815797 /lib64/ld-2.12.so
386cc20000-386cc21000 r--p 00020000 fd:00 6815797 /lib64/ld-2.12.so
386cc21000-386cc22000 rw-p 00021000 fd:00 6815797 /lib64/ld-2.12.so
386cc22000-386cc23000 rw-p 00000000 00:00 0
386ce00000-386cf8b000 r-xp 00000000 fd:00 6815808 /lib64/libc-2.12.so
386cf8b000-386d18a000 ---p 0018b000 fd:00 6815808 /lib64/libc-2.12.so
386d18a000-386d18e000 r--p 0018a000 fd:00 6815808 /lib64/libc-2.12.so
386d18e000-386d190000 rw-p 0018e000 fd:00 6815808 /lib64/libc-2.12.so
386d190000-386d194000 rw-p 00000000 00:00 0
386d200000-386d283000 r-xp 00000000 fd:00 6816160 /lib64/libm-2.12.so
386d283000-386d482000 ---p 00083000 fd:00 6816160 /lib64/libm-2.12.so
386d482000-386d483000 r--p 00082000 fd:00 6816160 /lib64/libm-2.12.so
386d483000-386d484000 rw-p 00083000 fd:00 6816160 /lib64/libm-2.12.so
3995800000-3995816000 r-xp 00000000 fd:00 6815780 /lib64/libgcc_s-4.4.7-20120601.so.1
3995816000-3995a15000 ---p 00016000 fd:00 6815780 /lib64/libgcc_s-4.4.7-20120601.so.1
3995a15000-3995a16000 rw-p 00015000 fd:00 6815780 /lib64/libgcc_s-4.4.7-20120601.so.1
7f4654000000-7f4654021000 rw-p 00000000 00:00 0
7f4654021000-7f4658000000 ---p 00000000 00:00 0
7f465a4d0000-7f465a4d5000 rw-p 00000000 00:00 0
7f465a4e2000-7f465a4e5000 rw-p 00000000 00:00 0
7ffeca062000-7ffeca077000 rw-p 00000000 00:00 0 [stack]
7ffeca13c000-7ffeca13d000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
(OrderedList) After mergeSort: Aborted (core dumped)
I've declared the functions within an OrderedList object, and myArray from the following chunk of code is what I'm sorting:
(myArray is an int*)
OrderedList::OrderedList(int size){
theArraySize = size;
myArray = new int[size];
}
'MergeSort()':
void OrderedList::mergeSort(int first, int last){
int middle;
if(first < last){
int middle = ((first + last) / 2);
mergeSort(first, middle);
mergeSort(middle + 1, last);
merge(first, middle, last);
}
}
Here is my 'Merge()' Function:
void OrderedList::merge(int first, int middle, int last){
int tempArray[last];
int i = first, j = middle + 1;
int index = first;
while((i <= middle) && (j <=last)){
if(myArray[i] < myArray[j]){
tempArray[index] = myArray[i];
i++;
} else {
tempArray[index] = myArray[j];
j++;
}
index++;
theTraversalCount++;
}
if(i > middle){
while(j <= last){
tempArray[index] = myArray[j];
j++;
index++;
theTraversalCount++;
}
} else{
while(i <= middle){
tempArray[index] = myArray[i];
i++;
index++;
theTraversalCount++;
}
}
for(int k = first; k < index; k++){
myArray[k] = tempArray[k];
theTraversalCount++;
}
}
I'm still new to using pointers (I've really only worked with java before c++, so pointers still aren't that intuitive to me) so I'm wondering if I might have done something wrong with my array declarations/array pointer declarations?

Passing variable by reference to several threads corrupts heap

This is part of a test for thread safety. I'm running an anonymous lambda in different threads.
I use the variable i as thread id.
Originally I passed every variable from main scope by using [&], but this corrupts the heap.
Solved it now by passing i by value, but for the life of me I can't figure out why this would cause problems on the heap since the threads are only reading i.
Can anyone explain?
Minimal compilable example producing error:
#include <thread>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
vector<thread> threads;
vector<string> vec1;
vector<string> vec2;
for (int i = 0; i < 2; i++) {
threads.push_back(
thread([&vec1, &vec2, &i]() {
for (int j = 0; j < 10; j++) {
const string str = "foo";
if (i == 0) {
vec1.push_back(str);
} else {
vec2.push_back(str);
}
}
})
);
}
for (auto& thread : threads) {
thread.join();
}
return 0;
}
Output:
*** Error in `/vagrant/bin/TempFileTest': double free or corruption (fasttop): 0x00007f00240008c0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f002a0e97e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f002a0f237a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f002a0f653c]
/vagrant/bin/TempFileTest(_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE19_M_emplace_back_auxIJRKS5_EEEvDpOT_+0x1a3)[0x4021b3]
/vagrant/bin/TempFileTest[0x401e83]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0xb8c80)[0x7f002a70ac80]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7f002a9db6ba]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7f002a17941d]
======= Memory map: ========
00400000-00403000 r-xp 00000000 08:02 2622834 /vagrant/bin/TempFileTest
00602000-00603000 r--p 00002000 08:02 2622834 /vagrant/bin/TempFileTest
00603000-00604000 rw-p 00003000 08:02 2622834 /vagrant/bin/TempFileTest
02182000-021b4000 rw-p 00000000 00:00 0 [heap]
7f001c000000-7f001c021000 rw-p 00000000 00:00 0
7f001c021000-7f0020000000 ---p 00000000 00:00 0
7f0024000000-7f0024021000 rw-p 00000000 00:00 0
7f0024021000-7f0028000000 ---p 00000000 00:00 0
7f0028d67000-7f0028d68000 ---p 00000000 00:00 0
7f0028d68000-7f0029568000 rw-p 00000000 00:00 0
7f0029568000-7f0029569000 ---p 00000000 00:00 0
7f0029569000-7f0029d69000 rw-p 00000000 00:00 0
7f0029d69000-7f0029e71000 r-xp 00000000 00:32 313 /lib/x86_64-linux-gnu/libm-2.23.so
7f0029e71000-7f002a070000 ---p 00108000 00:32 313 /lib/x86_64-linux-gnu/libm-2.23.so
7f002a070000-7f002a071000 r--p 00107000 00:32 313 /lib/x86_64-linux-gnu/libm-2.23.so
7f002a071000-7f002a072000 rw-p 00108000 00:32 313 /lib/x86_64-linux-gnu/libm-2.23.so
7f002a072000-7f002a232000 r-xp 00000000 00:32 45 /lib/x86_64-linux-gnu/libc-2.23.so
7f002a232000-7f002a432000 ---p 001c0000 00:32 45 /lib/x86_64-linux-gnu/libc-2.23.so
7f002a432000-7f002a436000 r--p 001c0000 00:32 45 /lib/x86_64-linux-gnu/libc-2.23.so
7f002a436000-7f002a438000 rw-p 001c4000 00:32 45 /lib/x86_64-linux-gnu/libc-2.23.so
7f002a438000-7f002a43c000 rw-p 00000000 00:00 0
7f002a43c000-7f002a452000 r-xp 00000000 00:32 314 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f002a452000-7f002a651000 ---p 00016000 00:32 314 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f002a651000-7f002a652000 rw-p 00015000 00:32 314 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f002a652000-7f002a7c4000 r-xp 00000000 00:32 311 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f002a7c4000-7f002a9c4000 ---p 00172000 00:32 311 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f002a9c4000-7f002a9ce000 r--p 00172000 00:32 311 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f002a9ce000-7f002a9d0000 rw-p 0017c000 00:32 311 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f002a9d0000-7f002a9d4000 rw-p 00000000 00:00 0
7f002a9d4000-7f002a9ec000 r-xp 00000000 00:32 61 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f002a9ec000-7f002abeb000 ---p 00018000 00:32 61 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f002abeb000-7f002abec000 r--p 00017000 00:32 61 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f002abec000-7f002abed000 rw-p 00018000 00:32 61 /lib/x86_64-linux-gnu/libpthread-2.23.so
7f002abed000-7f002abf1000 rw-p 00000000 00:00 0
7f002abf1000-7f002ac17000 r-xp 00000000 00:32 42 /lib/x86_64-linux-gnu/ld-2.23.so
7f002adf6000-7f002adfc000 rw-p 00000000 00:00 0
7f002ae15000-7f002ae16000 rw-p 00000000 00:00 0
7f002ae16000-7f002ae17000 r--p 00025000 00:32 42 /lib/x86_64-linux-gnu/ld-2.23.so
7f002ae17000-7f002ae18000 rw-p 00026000 00:32 42 /lib/x86_64-linux-gnu/ld-2.23.so
7f002ae18000-7f002ae19000 rw-p 00000000 00:00 0
7fff30694000-7fff306b5000 rw-p 00000000 00:00 0 [stack]
7fff30761000-7fff30763000 r--p 00000000 00:00 0 [vvar]
7fff30763000-7fff30765000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
Minimal compilable example without error (notice no & on i):
#include <thread>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
vector<thread> threads;
vector<string> vec1;
vector<string> vec2;
for (int i = 0; i < 2; i++) {
threads.push_back(
thread([&vec1, &vec2, i]() {
for (int j = 0; j < 10; j++) {
const string str = "foo";
if (i == 0) {
vec1.push_back(str);
} else {
vec2.push_back(str);
}
}
})
);
}
for (auto& thread : threads) {
thread.join();
}
return 0;
}
I'm using:
Ubuntu 16.04
gcc 5.4.0
Value of i changes at each iteration loop (in main thread) whereas you read it in other thread (without synchronization) -> UB.
Moreover, once the primary loop ends, you have dangling reference to i.
As a side note, you can spare yourself a lot of grief and reduce code size if you just capture the vector itself conditionally:
for (int i = 0; i < 2; i++) {
auto& vec = (i == 0 ? vec1 : vec2);
threads.push_back(
thread([&vec]() {
for (int j = 0; j < 10; j++) {
const string str = "foo";
vec.push_back(str);
}
})
);
}
Both for and the thread uses the same memory address of i (because you pass it by reference). Correct way is to let the thread have its own copy of i, which would be the same for the thread lifetime and independant from the loop changes.

Getting a free(): invalid pointer error

I was trying to make a program that replaces characters e with * on a string that has the most e's,but I'm getting a random free(): invalid pointer error whenever I run this program:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
const char char1 = 'e', char2 = '*';
int N, mostchars = 0, its;
cout << "N = ";
cin >> N;
string strings[N];
for(int i = 1; i <= N; i++)
{
cin >> strings[i];
if(mostchars < count(strings[i].begin(), strings[i].end(), char1))
{
mostchars = count(strings[i].begin(), strings[i].end(), char1);
its = i;
}
}
replace(strings[its].begin(), strings[its].end(), char1, char2);
cout << strings[its] << endl;
return 0;
}
And here's the gdb output:
*** Error in `/home/hiddendirishidden/STRING/bin/Debug/STRING': free(): invalid pointer: 0x00007fffffffe6a0 ***
======= Backtrace: =========
/usr/lib/libc.so.6(+0x6f364)[0x7ffff720a364]
/usr/lib/libc.so.6(+0x74d96)[0x7ffff720fd96]
/usr/lib/libc.so.6(+0x7557e)[0x7ffff721057e]
/usr/lib/libstdc++.so.6(_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_mutateEmmPKcm+0xfd)[0x7ffff7b765dd]
/usr/lib/libstdc++.so.6(_ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE+0x314)[0x7ffff7afd624]
/home/hiddendirishidden/STRING/bin/Debug/STRING[0x400e13]
/usr/lib/libc.so.6(__libc_start_main+0xf0)[0x7ffff71bb710]
/home/hiddendirishidden/STRING/bin/Debug/STRING[0x400c39]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:21 1452363 /home/hiddendirishidden/STRING/bin/Debug/STRING
00601000-00602000 rw-p 00001000 08:21 1452363 /home/hiddendirishidden/STRING/bin/Debug/STRING
00602000-00634000 rw-p 00000000 00:00 0 [heap]
7ffff0000000-7ffff0021000 rw-p 00000000 00:00 0
7ffff0021000-7ffff4000000 ---p 00000000 00:00 0
7ffff719b000-7ffff7333000 r-xp 00000000 08:21 396432 /usr/lib/libc-2.23.so
7ffff7333000-7ffff7532000 ---p 00198000 08:21 396432 /usr/lib/libc-2.23.so
7ffff7532000-7ffff7536000 r--p 00197000 08:21 396432 /usr/lib/libc-2.23.so
7ffff7536000-7ffff7538000 rw-p 0019b000 08:21 396432 /usr/lib/libc-2.23.so
7ffff7538000-7ffff753c000 rw-p 00000000 00:00 0
7ffff753c000-7ffff7552000 r-xp 00000000 08:21 396763 /usr/lib/libgcc_s.so.1
7ffff7552000-7ffff7751000 ---p 00016000 08:21 396763 /usr/lib/libgcc_s.so.1
7ffff7751000-7ffff7752000 rw-p 00015000 08:21 396763 /usr/lib/libgcc_s.so.1
7ffff7752000-7ffff7855000 r-xp 00000000 08:21 396490 /usr/lib/libm-2.23.so
7ffff7855000-7ffff7a55000 ---p 00103000 08:21 396490 /usr/lib/libm-2.23.so
7ffff7a55000-7ffff7a56000 r--p 00103000 08:21 396490 /usr/lib/libm-2.23.so
7ffff7a56000-7ffff7a57000 rw-p 00104000 08:21 396490 /usr/lib/libm-2.23.so
7ffff7a57000-7ffff7bc9000 r-xp 00000000 08:21 396798 /usr/lib/libstdc++.so.6.0.21
7ffff7bc9000-7ffff7dc9000 ---p 00172000 08:21 396798 /usr/lib/libstdc++.so.6.0.21
7ffff7dc9000-7ffff7dd3000 r--p 00172000 08:21 396798 /usr/lib/libstdc++.so.6.0.21
7ffff7dd3000-7ffff7dd5000 rw-p 0017c000 08:21 396798 /usr/lib/libstdc++.so.6.0.21
7ffff7dd5000-7ffff7dd9000 rw-p 00000000 00:00 0
7ffff7dd9000-7ffff7dfc000 r-xp 00000000 08:21 396431 /usr/lib/ld-2.23.so
7ffff7fc5000-7ffff7fcb000 rw-p 00000000 00:00 0
7ffff7ff7000-7ffff7ff8000 rw-p 00000000 00:00 0
7ffff7ff8000-7ffff7ffa000 r--p 00000000 00:00 0 [vvar]
7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0 [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00023000 08:21 396431 /usr/lib/ld-2.23.so
7ffff7ffd000-7ffff7ffe000 rw-p 00024000 08:21 396431 /usr/lib/ld-2.23.so
7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Program received signal SIGABRT, Aborted.
0x00007ffff71ce2a8 in raise () from /usr/lib/libc.so.6
I'm not sure why that happens. Maybe I did something wrong, after all this is the first time I'm trying out the algorithm library.
In
for(int i = 1; i <= N; i++)
You are using 1-based index. strings only has elements indexed from 0 to N-1, strings[N] is an invalid access.
C++ convention is 0-based indexes. Do
for(int i = 0; i < N; ++i)
You also may like to initialize variables and check for errors on input.
You used a variable to determine the length of an array. You cannot do this safely in C++.
Use std::vector<std::string> instead of std::string[N].
Furthermore, your loop goes 1 → N, instead of the correct 0 → N-1. That means the std::string you're trying to access on the final iteration doesn't exist, and the various allocations that the std::string performs internally are therefore broken.

*** glibc detected *** ./main: double free or corruption (fasttop): 0x08857008 ***

Line of fail: Xplano.ubicacion(Eureca.getCoordenadaX(),Eureca.getCoordenadaY())
Location in main.cpp, down but most information in the source code.
Thanks.
Error: * glibc detected ./main: double free or corruption
(fasttop): 0x08857008 **
======= Backtrace: ========= /lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x70f01)[0xb7550f01]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x72768)[0xb7552768]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(cfree+0x6d)[0xb755581d]
/usr/lib/i386-linux-gnu/libstdc++.so.6(ZdlPv+0x1f)[0xb76d44bf]
./main[0x8048cec]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(_libc_start_main+0xe6)[0xb74f6e46]
./main[0x8048941]
======= Memory map: ======== 08048000-0804a000 r-xp 00000000 08:05 165215 /home/administrator/Taller/main 0804a000-0804b000 rw-p
00001000 08:05 165215 /home/administrator/Taller/main
08857000-08878000 rw-p 00000000 00:00 0 [heap]
b7300000-b7321000 rw-p 00000000 00:00 0 b7321000-b7400000 ---p
00000000 00:00 0 b74de000-b74e0000 rw-p 00000000 00:00 0
b74e0000-b763c000 r-xp 00000000 08:05 914371
/lib/i386-linux-gnu/i686/cmov/libc-2.13.so b763c000-b763d000 ---p
0015c000 08:05 914371 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b763d000-b763f000 r--p 0015c000 08:05 914371
/lib/i386-linux-gnu/i686/cmov/libc-2.13.so b763f000-b7640000 rw-p
0015e000 08:05 914371 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7640000-b7643000 rw-p 00000000 00:00 0 b7643000-b765f000 r-xp
00000000 08:05 914451 /lib/i386-linux-gnu/libgcc_s.so.1
b765f000-b7660000 rw-p 0001b000 08:05 914451
/lib/i386-linux-gnu/libgcc_s.so.1 b7660000-b7661000 rw-p 00000000
00:00 0 b7661000-b7685000 r-xp 00000000 08:05 914379
/lib/i386-linux-gnu/i686/cmov/libm-2.13.so b7685000-b7686000 r--p
00023000 08:05 914379 /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b7686000-b7687000 rw-p 00024000 08:05 914379
/lib/i386-linux-gnu/i686/cmov/libm-2.13.so b7687000-b7767000 r-xp
00000000 08:05 528279 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b7767000-b776b000 r--p 000e0000 08:05 528279
/usr/lib/i386-linux-gnu/libstdc++.so.6.0.17 b776b000-b776c000 rw-p
000e4000 08:05 528279 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b776c000-b7773000 rw-p 00000000 00:00 0 b7788000-b778c000 rw-p
00000000 00:00 0 b778c000-b778d000 r-xp 00000000 00:00 0
[vdso] b778d000-b77a9000 r-xp 00000000 08:05 914407
/lib/i386-linux-gnu/ld-2.13.so b77a9000-b77aa000 r--p 0001b000 08:05
914407 /lib/i386-linux-gnu/ld-2.13.so b77aa000-b77ab000 rw-p
0001c000 08:05 914407 /lib/i386-linux-gnu/ld-2.13.so
bfe46000-bfe67000 rw-p 00000000 00:00 0 [stack] Aborted
Main.CPP
#include <iostream>
#include "Coordenada.cpp"
#include "plano.cpp"
#include <string>
using namespace std;
int main (){
int x;
int y;
x = y = 0;
std::cout << "Digite la Coordenada X:" << std::endl;
std::cin >> x;
std::cout << "Digite la Coordenada Y:" << std::endl;
std::cin >> y;
Coordenada Eureca;
Plano Xplano;
Eureca.setCoordenadaX(x);
Eureca.setCoordenadaY(y);
std::cout << "X es: " << Eureca.getCoordenadaX() << std::endl;
std::cout << "Y es: " << Eureca.getCoordenadaY() << std::endl;
std::cout << "Ubicado en el " << Xplano.ubicacion(Eureca.getCoordenadaX(),Eureca.getCoordenadaY()) << std::endl;
Eureca.~Coordenada();
Xplano.~Plano();
return 0;
}
Plano.CPP
#include <iostream>
#include <string>
#include "plano.hpp"
using namespace std;
using std::string;
Plano::Plano(){ubicacionX = "desconocido";}
Plano::~Plano(){}
string Plano::ubicacion(int x, int y) { if ((x=0) && (y=0)){this->ubicacionX = "origen";}
return (this->ubicacionX);}
Plano.HPP
#ifndef _PLANO_HPP
#define _PLANO_HPP
#include <string>
using std::string;
class Plano{
private:
string ubicacionX;
public:
Plano();
~Plano();
string ubicacion(int x, int y);
};
#endif
You don't need to call object destructor explicitly. When objects go out of scope, their destructors will be called implicitly. You need to remove below two lines of code:
// Eureca.~Coordenada();
// Xplano.~Plano();

Converting integer to a character pointer in c++ and then appending it to another chacter pointer

I have the following code for converting the integer(a score) into the character and then appending it with the player's name (player1). It gets displayed after that. It is a part of a bigger project :
#include <iostream>
#include <string.h>
using namespace std;
char* convertIntTochar(int number)
{
char t[3];
t[0] = 0;
t[1] = 0;
t[2] = '\0';
int i = 0;
for(; number != 0; i++)
{
t[i] = ((number%10) + 48);
number/=10;
}
if(i == 2)
{
char temp = t[0];
t[0] = t[1];
t[1] = temp;
}
else
t[i] = '\0';
char *ans = t;
return ans;
}
int main()
{
char str11[] = "Player1: ";
char *str1 = str11;
char *str2 = convertIntTochar(11);
strcat(str1 , str2);
while(*str1)
{
cout<<*(str1++);
}
return 0;
}
It compiles correctly but when I run it , it shows the following error :
*** stack smashing detected ***: ./a.out terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x50)[0x9b3390]
/lib/tls/i686/cmov/libc.so.6(+0xe233a)[0x9b333a]
./a.out[0x80487ff]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x8e7bd6]
./a.out[0x8048621]
======= Memory map: ========
00110000-00134000 r-xp 00000000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so
00134000-00135000 r--p 00023000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so
00135000-00136000 rw-p 00024000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so
004b9000-004d4000 r-xp 00000000 08:06 2887597 /lib/ld-2.11.1.so
004d4000-004d5000 r--p 0001a000 08:06 2887597 /lib/ld-2.11.1.so
004d5000-004d6000 rw-p 0001b000 08:06 2887597 /lib/ld-2.11.1.so
0077d000-00866000 r-xp 00000000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13
00866000-00867000 ---p 000e9000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13
00867000-0086b000 r--p 000e9000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13
0086b000-0086c000 rw-p 000ed000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13
0086c000-00873000 rw-p 00000000 00:00 0
008d1000-00a24000 r-xp 00000000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so
00a24000-00a25000 ---p 00153000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so
00a25000-00a27000 r--p 00153000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so
00a27000-00a28000 rw-p 00155000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so
00a28000-00a2b000 rw-p 00000000 00:00 0
00a3b000-00a58000 r-xp 00000000 08:06 2883667 /lib/libgcc_s.so.1
00a58000-00a59000 r--p 0001c000 08:06 2883667 /lib/libgcc_s.so.1
00a59000-00a5a000 rw-p 0001d000 08:06 2883667 /lib/libgcc_s.so.1
00b74000-00b75000 r-xp 00000000 00:00 0 [vdso]
08048000-08049000 r-xp 00000000 08:06 4719693 /home/dhruv/Desktop/a.out
08049000-0804a000 r--p 00000000 08:06 4719693 /home/dhruv/Desktop/a.out
0804a000-0804b000 rw-p 00001000 08:06 4719693 /home/dhruv/Desktop/a.out
08b67000-08b88000 rw-p 00000000 00:00 0 [heap]
b77f7000-b77f9000 rw-p 00000000 00:00 0
b780d000-b7810000 rw-p 00000000 00:00 0
bfd2a000-bfd3f000 rw-p 00000000 00:00 0 [stack]
Player1: "�ӿ�XMAborted
What is the reason for this? How can it be rectified. I have already put the null termination character in the convertIntTochar function.
Many problems here...
convertIntTochar works for 2 digit numbers only. No checks are done.
char t[3] defined in convertIntTochar is a local variable, you can't return a pointer to it and use this pointer outside convertIntTochar.
strcat(str1 , str2); appends to an array that is already full (str11) so you overwrite the stack.
Just switch to std::strings, it will be simpler.
char str11[] = "Player1: ";
This is the problem. There is not enough room for string concatenation. Try this:
char str11[100] = "Player1: ";
Better yet, use std::string instead of C-like char*. Smallest possible changes that fix string problems are these (because there exist using namespace std then std:: part in std::string can be left out, but I just prefer to leave it in):
#include <iostream>
#include <string> // instead of <string.h>
using namespace std;
std::string convertIntTochar(int number)
{
...
}
int main()
{
std::string str1 = "Player1: ";
std::string str2 = convertIntTochar(11);
str1 += str2;
cout << str1;
// Or even more effective, just one line of code:
cout << "Player1: " << convertIntTochar(11);
return 0;
}
Use std::strings and std::ostringstreams, it's much simpler.
#include <sstream>
#include <iostream>
std::ostringstream player_score_stream;
player_score_stream << "Player1: " << score_as_an_integer;
std::string player_score(player_score_stream.str());
std::cout << player_score;
and if you want a read-only C string, use player_score.c_str(), that returns a const char *
To convert int to char * see if you can use itoa with your compiler.
If it is not supported you can find its implemenation to do what you want.
That is if you have to do it using C-strings