deleting pointer to pointer is run-time error? - c++

i have a simple question about pointers , why when i tried to delete a pointer that is pointing into a pointer (the small pointer is pointing into a new variable (new memory allocation )) i always receive a run-time error ?? (as shown below :)) )
#include <iostream>
using namespace std;
int main() {
int *a=new int;
*a=10;
int **aa=&a;
cout<<*a<<endl;
cout<<**aa<<endl;
delete aa;// when i comment this line the program work as well as expectied
return 0;
}
and how can i delete a pointer that is pointing into another pointer ??
i receive this error when i try to run the previous code
10
10
*** Error in `/home/abdullah/CLionProjects/tstt1/cmake-build-debug/tstt1': munmap_chunk(): invalid pointer: 0x00007ffef8e9dbb0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bfb)[0x7fa3a9c5ebfb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76fc6)[0x7fa3a9c64fc6]
/home/abdullah/CLionProjects/tstt1/cmake-build-debug/tstt1(+0xa4e)[0x5590e44c0a4e]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7fa3a9c0e2e1]
/home/abdullah/CLionProjects/tstt1/cmake-build-debug/tstt1(+0x8ba)[0x5590e44c08ba]
======= Memory map: ========
5590e44c0000-5590e44c1000 r-xp 00000000 08:07 1836009 /home/abdullah/CLionProjects/tstt1/cmake-build-debug/tstt1
5590e46c0000-5590e46c1000 r--p 00000000 08:07 1836009 /home/abdullah/CLionProjects/tstt1/cmake-build-debug/tstt1
5590e46c1000-5590e46c2000 rw-p 00001000 08:07 1836009 /home/abdullah/CLionProjects/tstt1/cmake-build-debug/tstt1
5590e50f2000-5590e5124000 rw-p 00000000 00:00 0 [heap]
7fa3a9bee000-7fa3a9d83000 r-xp 00000000 08:07 2761474 /usr/lib/x86_64-linux-gnu/libc-2.24.so
7fa3a9d83000-7fa3a9f83000 ---p 00195000 08:07 2761474 /usr/lib/x86_64-linux-gnu/libc-2.24.so
7fa3a9f83000-7fa3a9f87000 r--p 00195000 08:07 2761474 /usr/lib/x86_64-linux-gnu/libc-2.24.so
7fa3a9f87000-7fa3a9f89000 rw-p 00199000 08:07 2761474 /usr/lib/x86_64-linux-gnu/libc-2.24.so
7fa3a9f89000-7fa3a9f8d000 rw-p 00000000 00:00 0
7fa3a9f8d000-7fa3a9fa3000 r-xp 00000000 08:07 2761798 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7fa3a9fa3000-7fa3aa1a2000 ---p 00016000 08:07 2761798 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7fa3aa1a2000-7fa3aa1a3000 r--p 00015000 08:07 2761798 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7fa3aa1a3000-7fa3aa1a4000 rw-p 00016000 08:07 2761798 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7fa3aa1a4000-7fa3aa2a7000 r-xp 00000000 08:07 2762304 /usr/lib/x86_64-linux-gnu/libm-2.24.so
7fa3aa2a7000-7fa3aa4a6000 ---p 00103000 08:07 2762304 /usr/lib/x86_64-linux-gnu/libm-2.24.so
7fa3aa4a6000-7fa3aa4a7000 r--p 00102000 08:07 2762304 /usr/lib/x86_64-linux-gnu/libm-2.24.so
7fa3aa4a7000-7fa3aa4a8000 rw-p 00103000 08:07 2762304 /usr/lib/x86_64-linux-gnu/libm-2.24.so
7fa3aa4a8000-7fa3aa61a000 r-xp 00000000 08:07 2762793 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7fa3aa61a000-7fa3aa81a000 ---p 00172000 08:07 2762793 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7fa3aa81a000-7fa3aa824000 r--p 00172000 08:07 2762793 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7fa3aa824000-7fa3aa826000 rw-p 0017c000 08:07 2762793 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7fa3aa826000-7fa3aa82a000 rw-p 00000000 00:00 0
7fa3aa82a000-7fa3aa84d000 r-xp 00000000 08:07 2761022 /usr/lib/x86_64-linux-gnu/ld-2.24.so
7fa3aaa2e000-7fa3aaa32000 rw-p 00000000 00:00 0
7fa3aaa49000-7fa3aaa4d000 rw-p 00000000 00:00 0
7fa3aaa4d000-7fa3aaa4e000 r--p 00023000 08:07 2761022 /usr/lib/x86_64-linux-gnu/ld-2.24.so
7fa3aaa4e000-7fa3aaa4f000 rw-p 00024000 08:07 2761022 /usr/lib/x86_64-linux-gnu/ld-2.24.so
7fa3aaa4f000-7fa3aaa50000 rw-p 00000000 00:00 0
7ffef8e7e000-7ffef8e9f000 rw-p 00000000 00:00 0 [stack]
7ffef8ed9000-7ffef8edc000 r--p 00000000 00:00 0 [vvar]
7ffef8edc000-7ffef8ede000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
any ideas ? :(

aa is pointing to a which is a stack variable. You can only delete objects that were created using new.
Are you trying to delete the int that you created using new? In that case you would want to try delete *aa.

Related

Runtime error while using vectors in c++ with large number of inputs

I was trying to solve a hackerearth problems to understand c++ better.
The Task is as follows :
The first line will consists of one integer T denoting the number of test cases.
For each test case:
The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.
The next line consists of N space separated integers , denoting the elements of the array A.
So,If the input is like
1
5 2
1 2 3 4 5
The output should be
4 5 1 2 3
The code that i have written is as follows :
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void ans(int r,vector<int> &a)
{
reverse(a.begin(),a.end());
reverse(a.begin(),a.begin()+r);
reverse(a.begin()+r,a.end());
}
int main() {
int testno;
cin>>testno;
int size,rotation,temp;
vector<vector<int>> arr(testno);
for(int i=0;i<testno;i++)
{
cin>>size;
cin>>rotation;
for(int j=0;j<size;j++)
{
cin>>temp;
arr[i].push_back(temp);
};
ans(rotation,arr[i]);
};
for(int i=0;i<arr.size();i++)
{
for(int j=0;j<arr[i].size();j++)
cout<<arr[i][j]<<" ";
cout<<"\n";
};
}
This code works fine for small test-cases or numbers.
But when the input is huge,I get the following error :
Execution failed.
Stack Trace:
*** Error in `/hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out': malloc(): memory corruption (fast): 0x0000000001b0ad50 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777f5)[0x7f10c1df57f5]
/lib/x86_64-linux-gnu/libc.so.6(+0x82679)[0x7f10c1e00679]
/lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x54)[0x7f10c1e021d4]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_Znwm+0x15)[0x7f10c2405c75]
/hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out[0x400f62]
/hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out[0x400bbb]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f10c1d9e840]
/hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out[0x400d49]
======= Memory map: ========
00400000-00402000 r-xp 00000000 07:00 28 /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out
00601000-00602000 r--p 00001000 07:00 28 /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out
00602000-00603000 rw-p 00002000 07:00 28 /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out
01af8000-01b2a000 rw-p 00000000 00:00 0 [heap]
7f10bc000000-7f10bc021000 rw-p 00000000 00:00 0
7f10bc021000-7f10c0000000 ---p 00000000 00:00 0
7f10c1a75000-7f10c1b7d000 r-xp 00000000 ca:01 48154 /lib/x86_64-linux-gnu/libm-2.23.so
7f10c1b7d000-7f10c1d7c000 ---p 00108000 ca:01 48154 /lib/x86_64-linux-gnu/libm-2.23.so
7f10c1d7c000-7f10c1d7d000 r--p 00107000 ca:01 48154 /lib/x86_64-linux-gnu/libm-2.23.so
7f10c1d7d000-7f10c1d7e000 rw-p 00108000 ca:01 48154 /lib/x86_64-linux-gnu/libm-2.23.so
7f10c1d7e000-7f10c1f3e000 r-xp 00000000 ca:01 48130 /lib/x86_64-linux-gnu/libc-2.23.so
7f10c1f3e000-7f10c213e000 ---p 001c0000 ca:01 48130 /lib/x86_64-linux-gnu/libc-2.23.so
7f10c213e000-7f10c2142000 r--p 001c0000 ca:01 48130 /lib/x86_64-linux-gnu/libc-2.23.so
7f10c2142000-7f10c2144000 rw-p 001c4000 ca:01 48130 /lib/x86_64-linux-gnu/libc-2.23.so
7f10c2144000-7f10c2148000 rw-p 00000000 00:00 0
7f10c2148000-7f10c215f000 r-xp 00000000 ca:01 18214 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f10c215f000-7f10c235e000 ---p 00017000 ca:01 18214 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f10c235e000-7f10c235f000 r--p 00016000 ca:01 18214 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f10c235f000-7f10c2360000 rw-p 00017000 ca:01 18214 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f10c2360000-7f10c2533000 r-xp 00000000 ca:01 16581 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f10c2533000-7f10c2732000 ---p 001d3000 ca:01 16581 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f10c2732000-7f10c273d000 r--p 001d2000 ca:01 16581 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f10c273d000-7f10c2740000 rw-p 001dd000 ca:01 16581 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f10c2740000-7f10c2743000 rw-p 00000000 00:00 0
7f10c2743000-7f10c2769000 r-xp 00000000 ca:01 48198 /lib/x86_64-linux-gnu/ld-2.23.so
7f10c2948000-7f10c294e000 rw-p 00000000 00:00 0
7f10c2967000-7f10c2968000 rw-p 00000000 00:00 0
7f10c2968000-7f10c2969000 r--p 00025000 ca:01 48198 /lib/x86_64-linux-gnu/ld-2.23.so
7f10c2969000-7f10c296a000 rw-p 00026000 ca:01 48198 /lib/x86_64-linux-gnu/ld-2.23.so
7f10c296a000-7f10c296b000 rw-p 00000000 00:00 0
7fff76e54000-7fff76e75000 rw-p 00000000 00:00 0 [stack]
7fff76f09000-7fff76f0b000 r--p 00000000 00:00 0 [vvar]
7fff76f0b000-7fff76f0d000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
I don't understand why there is a runtime malloc error? What exactly is happening that is leading to this error for large inputs? Why is there memory corruption? Am I trying to access memory not allocated to me during the runtime? If so,Where? The link to the problem,if needed is : https://www.hackerearth.com/practice/codemonk/
Turns out some test cases had rotation higher than the size of vector.So just updated rotation to rotation%size. Working fine now.

add pointer to structure array to std::map

I want to keep an array of data index by a string and I thought I'd best use std::map for this purpose. I have below example code:
typedef struct MyType_s {
long long timestamp;
int cnt;
bool parked;
}MyType;
static MyType *list = {0};
static int listcnt = 0;
//-------------------------------------------------------------------------------------------------
int map_add_item(std::map<std::string, MyType*> *pmap, std::string str, long long tmestmp)
{
if (listcnt == 0){
list =(MyType*)malloc(sizeof(MyType));
if (list)
listcnt++;
else
return ENOMEM;
}
if (realloc(list,sizeof(MyType)*(++listcnt))==0)
return ENOMEM;
list->timestamp = tmestmp;
if (!(str.length()&&tmestmp&&pmap))
return EINVAL*-1;
if (pmap->insert(std::make_pair(str, &list[listcnt-1])).second == false){
pmap->find(str)->second->timestamp = tmestmp;
return EEXIST*-1;
}
return OK;
}
which compiles fine but I get a mem dump like this when I run it:
*** Error in `./std_map': double free or corruption (fasttop): 0x000000000226ec20 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f19fb6267e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f19fb62f37a]
/lib/x86_64-linux-gnu/libc.so.6(+0x83350)[0x7f19fb632350]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x179)[0x7f19fb633839]
./std_map[0x4013c8]
./std_map[0x40198d]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f19fb5cf830]
./std_map[0x401259]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:02 6054199 /path/to/src/tmp/std_map
00604000-00605000 r--p 00004000 08:02 6054199 /path/to/src/tmp/std_map
00605000-00606000 rw-p 00005000 08:02 6054199 /path/to/src/tmp/std_map
0225d000-0228f000 rw-p 00000000 00:00 0 [heap]
7f19f4000000-7f19f4021000 rw-p 00000000 00:00 0
7f19f4021000-7f19f8000000 ---p 00000000 00:00 0
7f19fb2a6000-7f19fb3ae000 r-xp 00000000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so
7f19fb3ae000-7f19fb5ad000 ---p 00108000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so
7f19fb5ad000-7f19fb5ae000 r--p 00107000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so
7f19fb5ae000-7f19fb5af000 rw-p 00108000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so
7f19fb5af000-7f19fb76f000 r-xp 00000000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so
7f19fb76f000-7f19fb96f000 ---p 001c0000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so
7f19fb96f000-7f19fb973000 r--p 001c0000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so
7f19fb973000-7f19fb975000 rw-p 001c4000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so
7f19fb975000-7f19fb979000 rw-p 00000000 00:00 0
7f19fb979000-7f19fb98f000 r-xp 00000000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f19fb98f000-7f19fbb8e000 ---p 00016000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f19fbb8e000-7f19fbb8f000 rw-p 00015000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f19fbb8f000-7f19fbd01000 r-xp 00000000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f19fbd01000-7f19fbf01000 ---p 00172000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f19fbf01000-7f19fbf0b000 r--p 00172000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f19fbf0b000-7f19fbf0d000 rw-p 0017c000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f19fbf0d000-7f19fbf11000 rw-p 00000000 00:00 0
7f19fbf11000-7f19fbf37000 r-xp 00000000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so
7f19fc0fc000-7f19fc102000 rw-p 00000000 00:00 0
7f19fc135000-7f19fc136000 rw-p 00000000 00:00 0
7f19fc136000-7f19fc137000 r--p 00025000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so
7f19fc137000-7f19fc138000 rw-p 00026000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so
7f19fc138000-7f19fc139000 rw-p 00000000 00:00 0
7ffd68bf4000-7ffd68c16000 rw-p 00000000 00:00 0 [stack]
7ffd68d88000-7ffd68d8b000 r--p 00000000 00:00 0 [vvar]
7ffd68d8b000-7ffd68d8d000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Command terminated
gdb doesn't give me much more information too.
This is after I call the function like this from main():
int main()
{
int ret = 0;
std::map<std::string, MyType*> mapOfWords;
std::cout << map_add_item(&mapOfWords,"earth",time(NULL)+1) << std::endl;
return 0;
}
I'm wondering what I'm doing wrong, double free or corruption obviously is a hint but I'm not sure how to interpret this and I can't pinpoint the problem....
For a working solution, I replaced the global static variable and the pointers in the map with data that is located directly in the map. Plus I removed the typedef from the struct and declared it the C++ way instead, as in:
std::map<std::string, MyType> *pmap
struct MyType {
long long timestamp;
int cnt;
bool parked;
};

how to trace buffer overflow detected-ns terminated

I was working on aqua-sim that is based on ns2 for my ubuntu and i'm getting a buffer overflow error and i can't figure out how to trace this kind of error.
i tried to debug using gdb but with no luck as i don't know how to trace the error, can someone please let me know where should i look based on the following error segment:
num_nodes is set 10
INITIALIZE THE LIST xListHead
Starting Simulation...
GOD: the old file name istest.data
GOD: the new file name isunderwater.data
*** buffer overflow detected ***: ns terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f57d8e347e5]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7f57d8ed611c]
/lib/x86_64-linux-gnu/libc.so.6(+0x117120)[0x7f57d8ed4120]
/lib/x86_64-linux-gnu/libc.so.6(+0x116689)[0x7f57d8ed3689]
/lib/x86_64-linux-gnu/libc.so.6(_IO_default_xsputn+0x80)[0x7f57d8e386b0]
/lib/x86_64-linux-gnu/libc.so.6(_IO_vfprintf+0xc90)[0x7f57d8e0ae00]
/lib/x86_64-linux-gnu/libc.so.6(__vsprintf_chk+0x84)[0x7f57d8ed3714]
/lib/x86_64-linux-gnu/libc.so.6(__sprintf_chk+0x7d)[0x7f57d8ed366d]
ns[0x57ea03]
ns[0x57eb44]
ns[0x415367]
ns[0x4162e6]
ns[0x416d8c]
ns[0x5b3894]
ns[0x5b85d5]
ns[0x5bdf12]
ns[0x5c0372]
ns[0x5e7de9]
ns[0x5ec6c0]
ns[0x5e7e99]
ns[0x5ec6c0]
ns[0x613615]
ns[0x613803]
ns[0x5b8705]
ns[0x5bdf12]
ns[0x5c0372]
ns[0x5e7de9]
ns[0x5ec6c0]
ns[0x613615]
ns[0x613803]
ns[0x5b85d5]
ns[0x5bdf12]
ns[0x5c0372]
ns[0x5c1c37]
ns[0x603321]
ns[0x606936]
ns[0x405abe]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f57d8ddd830]
ns[0x410f59]
======= Memory map: ========
00400000-00766000 r-xp 00000000 08:06 2491295 /home/me/Aqua-Sim/Aqua-Sim-1.0/ns-2.30/ns
00965000-00967000 r--p 00365000 08:06 2491295 /home/me/Aqua-Sim/Aqua-Sim-1.0/ns-2.30/ns
00967000-009f2000 rw-p 00367000 08:06 2491295 /home/me/Aqua-Sim/Aqua-Sim-1.0/ns-2.30/ns
009f2000-00a00000 rw-p 00000000 00:00 0
00c2e000-01085000 rw-p 00000000 00:00 0 [heap]
7f57d8ad0000-7f57d8dbd000 r--p 00000000 08:06 2627421 /usr/lib/locale/locale-archive
7f57d8dbd000-7f57d8f7d000 r-xp 00000000 08:06 1179887 /lib/x86_64-linux-gnu/libc-2.23.so
7f57d8f7d000-7f57d917d000 ---p 001c0000 08:06 1179887 /lib/x86_64-linux-gnu/libc-2.23.so
7f57d917d000-7f57d9181000 r--p 001c0000 08:06 1179887 /lib/x86_64-linux-gnu/libc-2.23.so
7f57d9181000-7f57d9183000 rw-p 001c4000 08:06 1179887 /lib/x86_64-linux-gnu/libc-2.23.so
7f57d9183000-7f57d9187000 rw-p 00000000 00:00 0
7f57d9187000-7f57d919d000 r-xp 00000000 08:06 1184430 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f57d919d000-7f57d939c000 ---p 00016000 08:06 1184430 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f57d939c000-7f57d939d000 rw-p 00015000 08:06 1184430 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f57d939d000-7f57d9517000 r-xp 00000000 08:06 2621627 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.24
7f57d9517000-7f57d9716000 ---p 0017a000 08:06 2621627 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.24
7f57d9716000-7f57d9720000 r--p 00179000 08:06 2621627 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.24
7f57d9720000-7f57d9722000 rw-p 00183000 08:06 2621627 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.24
7f57d9722000-7f57d9725000 rw-p 00000000 00:00 0
7f57d9725000-7f57d982d000 r-xp 00000000 08:06 1179880 /lib/x86_64-linux-gnu/libm-2.23.so
7f57d982d000-7f57d9a2c000 ---p 00108000 08:06 1179880 /lib/x86_64-linux-gnu/libm-2.23.so
7f57d9a2c000-7f57d9a2d000 r--p 00107000 08:06 1179880 /lib/x86_64-linux-gnu/libm-2.23.so
7f57d9a2d000-7f57d9a2e000 rw-p 00108000 08:06 1179880 /lib/x86_64-linux-gnu/libm-2.23.so
7f57d9a2e000-7f57d9a31000 r-xp 00000000 08:06 1179868 /lib/x86_64-linux-gnu/libdl-2.23.so
7f57d9a31000-7f57d9c30000 ---p 00003000 08:06 1179868 /lib/x86_64-linux-gnu/libdl-2.23.so
7f57d9c30000-7f57d9c31000 r--p 00002000 08:06 1179868 /lib/x86_64-linux-gnu/libdl-2.23.so
7f57d9c31000-7f57d9c32000 rw-p 00003000 08:06 1179868 /lib/x86_64-linux-gnu/libdl-2.23.so
7f57d9c32000-7f57d9c58000 r-xp 00000000 08:06 1179846 /lib/x86_64-linux-gnu/ld-2.23.so
7f57d9e37000-7f57d9e3d000 rw-p 00000000 00:00 0
7f57d9e54000-7f57d9e57000 rw-p 00000000 00:00 0
7f57d9e57000-7f57d9e58000 r--p 00025000 08:06 1179846 /lib/x86_64-linux-gnu/ld-2.23.so
7f57d9e58000-7f57d9e59000 rw-p 00026000 08:06 1179846 /lib/x86_64-linux-gnu/ld-2.23.so
7f57d9e59000-7f57d9e5a000 rw-p 00000000 00:00 0
7ffeddbfa000-7ffeddc1c000 rw-p 00000000 00:00 0 [stack]
7ffeddc4b000-7ffeddc4d000 r--p 00000000 00:00 0 [vvar]
7ffeddc4d000-7ffeddc4f000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
my gcc is 4.9,on ns-2.30, and ubuntu 16.04

Error message glibc detected Malloc(): memory corruption (fast)

I recently got this error. The weird thing is, that I am not always getting this error message...
*** glibc detected *** ./a.out: malloc(): memory corruption (fast): 0x0000000002134dc0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x76a16)[0x7fa62b164a16]
/lib/x86_64-linux-gnu/libc.so.6(+0x7a2f8)[0x7fa62b1682f8]
/lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x70)[0x7fa62b1698a0]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_Znwm+0x1d)[0x7fa62b97607d]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNSs4_Rep9_S_createEmmRKSaIcE+0x59)[0x7fa62b9d1999]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNSs4_Rep8_M_cloneERKSaIcEm+0x28)[0x7fa62b9d2708]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNSs7reserveEm+0x30)[0x7fa62b9d27f0]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNSs6appendEPKcm+0xb5)[0x7fa62b9d2ab5]
./a.out[0x40758f]
./a.out[0x403279]
./a.out[0x405202]
./a.out[0x406332]
./a.out[0x406c90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd)[0x7fa62b10cead]
./a.out[0x402189]
======= Memory map: ========
00400000-0040e000 r-xp 00000000 08:05 6166978 /root/tradingbot/bot2/a.out
0060d000-0060e000 rw-p 0000d000 08:05 6166978 /root/tradingbot/bot2/a.out
02132000-02153000 rw-p 00000000 00:00 0 [heap]
7fa624000000-7fa624021000 rw-p 00000000 00:00 0
7fa624021000-7fa628000000 ---p 00000000 00:00 0
7fa62b0ee000-7fa62b270000 r-xp 00000000 08:05 12715768 /lib/x86_64-linux-gnu/libc-2.13.so
7fa62b270000-7fa62b470000 ---p 00182000 08:05 12715768 /lib/x86_64-linux-gnu/libc-2.13.so
7fa62b470000-7fa62b474000 r--p 00182000 08:05 12715768 /lib/x86_64-linux-gnu/libc-2.13.so
7fa62b474000-7fa62b475000 rw-p 00186000 08:05 12715768 /lib/x86_64-linux-gnu/libc-2.13.so
7fa62b475000-7fa62b47a000 rw-p 00000000 00:00 0
7fa62b47a000-7fa62b48f000 r-xp 00000000 08:05 12715492 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa62b48f000-7fa62b68f000 ---p 00015000 08:05 12715492 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa62b68f000-7fa62b690000 rw-p 00015000 08:05 12715492 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa62b690000-7fa62b711000 r-xp 00000000 08:05 12715766 /lib/x86_64-linux-gnu/libm-2.13.so
7fa62b711000-7fa62b910000 ---p 00081000 08:05 12715766 /lib/x86_64-linux-gnu/libm-2.13.so
7fa62b910000-7fa62b911000 r--p 00080000 08:05 12715766 /lib/x86_64-linux-gnu/libm-2.13.so
7fa62b911000-7fa62b912000 rw-p 00081000 08:05 12715766 /lib/x86_64-linux-gnu/libm-2.13.so
7fa62b912000-7fa62b9fa000 r-xp 00000000 08:05 7345862 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa62b9fa000-7fa62bbfa000 ---p 000e8000 08:05 7345862 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa62bbfa000-7fa62bc02000 r--p 000e8000 08:05 7345862 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa62bc02000-7fa62bc04000 rw-p 000f0000 08:05 7345862 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa62bc04000-7fa62bc19000 rw-p 00000000 00:00 0
7fa62bc19000-7fa62bc39000 r-xp 00000000 08:05 12715773 /lib/x86_64-linux-gnu/ld-2.13.so
7fa62be10000-7fa62be15000 rw-p 00000000 00:00 0
7fa62be35000-7fa62be38000 rw-p 00000000 00:00 0
7fa62be38000-7fa62be39000 r--p 0001f000 08:05 12715773 /lib/x86_64-linux-gnu/ld-2.13.so
7fa62be39000-7fa62be3a000 rw-p 00020000 08:05 12715773 /lib/x86_64-linux-gnu/ld-2.13.so
7fa62be3a000-7fa62be3b000 rw-p 00000000 00:00 0
7fffb0149000-7fffb016a000 rw-p 00000000 00:00 0 [stack]
7fffb019f000-7fffb01a1000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
Here is what I was doing...
I am creating n objects of a class dynamically and storing them in a vector (C++). Then with an iterator I access each object, and deference it to access the object's members. I am also reading some text files, so edited them by hand during runtime. I don't know which part of the code I should place here, but this is where it crashed exactly:
if( last_op_number == 1 ){
it = n_bots.end();// Grabs iterator and go to the end of the vector; This is where the last OP1 bot is located
bot *botop1 = &(*it); // Dereference iterator to have a pointer to that specific bot
/* 3) Set the real buy and sell prices */
botop1->real_bpsp(price_ex1,price_ex2);
}
// Clear text files
OKC->clear_pos();
OKC2->clear_pos();
updates_balance(); // Updates balances
green_light = 1; // Tell bot manager that we are good to go!
Any ideas of what's going on?
Thank you!
You should use n_bots.back() instead of n_bots.end().

Deletion of Pointers Issue

I am having trouble deleting my pointers that I have created. The program creates a double pointer to point to the threads. Then it creates threads dynamically. At the end it deletes them but I am getting a glibc error. It uses boost to create the threads. What is really puzzling is that I delete a similar double pointer the same exact way and that executes fine. The issue is at the end of the code block under the heading of /*clean up*/:
boost :: thread** thrds;
//create threads and bind to p_variantforloop_t
thrds = new boost::thread*[numThreads];
for (int i = 1; i <= numThreads; i++)
thrds[i] = new boost::thread(boost::bind(&p_variantforloop_t,
E, A, D, (i*n-n)/i ,(i*n)/n, numThreads, n));
/* join threads */
for (int i = 0; i < numThreads; i++)
thrds[i]->join();
/* cleanup */
for (int i = 0; i < numThreads; i++)
delete thrds[i];
delete[] thrds;
the error is:
*** glibc detected *** ./hw9: munmap_chunk(): invalid pointer: 0x0957d480 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x264591]
/lib/tls/i686/cmov/libc.so.6(+0x6c80e)[0x26580e]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x529741]
./hw9[0x804a0d1]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x20fbd6]
./hw9[0x8049871]
======= Memory map: ========
001f9000-0034c000 r-xp 00000000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
0034c000-0034d000 ---p 00153000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
0034d000-0034f000 r--p 00153000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
0034f000-00350000 rw-p 00155000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
00350000-00353000 rw-p 00000000 00:00 0
0046e000-00557000 r-xp 00000000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
00557000-00558000 ---p 000e9000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
00558000-0055c000 r--p 000e9000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
0055c000-0055d000 rw-p 000ed000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
0055d000-00564000 rw-p 00000000 00:00 0
005ad000-005d1000 r-xp 00000000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
005d1000-005d2000 r--p 00023000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
005d2000-005d3000 rw-p 00024000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
00950000-00965000 r-xp 00000000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
00965000-00966000 r--p 00014000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
00966000-00967000 rw-p 00015000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
00967000-00969000 rw-p 00000000 00:00 0
00a67000-00a7a000 r-xp 00000000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
00a7a000-00a7b000 r--p 00013000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
00a7b000-00a7c000 rw-p 00014000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
00bc7000-00be2000 r-xp 00000000 08:02 1128318 /lib/ld-2.11.1.so
00be2000-00be3000 r--p 0001a000 08:02 1128318 /lib/ld-2.11.1.so
00be3000-00be4000 rw-p 0001b000 08:02 1128318 /lib/ld-2.11.1.so
00c34000-00c3b000 r-xp 00000000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
00c3b000-00c3c000 r--p 00006000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
00c3c000-00c3d000 rw-p 00007000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
00ccf000-00cd0000 r-xp 00000000 00:00 0 [vdso]
00e85000-00ea2000 r-xp 00000000 08:02 1128359 /lib/libgcc_s.so.1
00ea2000-00ea3000 r--p 0001c000 08:02 1128359 /lib/libgcc_s.so.1
00ea3000-00ea4000 rw-p 0001d000 08:02 1128359 /lib/libgcc_s.so.1
08048000-0804e000 r-xp 00000000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
0804e000-0804f000 r--p 00005000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
0804f000-08050000 rw-p 00006000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
0957d000-0959e000 rw-p 00000000 00:00 0 [heap]
b68bf000-b68c0000 ---p 00000000 00:00 0
b68c0000-b70c0000 rw-p 00000000 00:00 0
b70c0000-b70c1000 ---p 00000000 00:00 0
b70c1000-b78c4000 rw-p 00000000 00:00 0
b78e8000-b78eb000 rw-p 00000000 00:00 0
bfc07000-bfc1c000 rw-p 00000000 00:00 0 [stack]
Aborted
You omit first element of the array when creating thread object. It stays with uninitialized pointer value, causing the invalid pointer error later.
There is also an issue of writing to memory immediately after the last element of array, which you also do in the problematic loop (i.e. the first for in your code).