Error: glibc detected *** ./a.out: malloc(): memory corruption (fast): 0x08c06058

I would appreciate help. I am recieving this error:
*** glibc detected *** ./a.out: malloc(): memory corruption (fast): 0x08c06058 ***
======= Backtrace: =========
/lib/libc.so.6[0x4b2249f2]
/lib/libc.so.6[0x4b2273c0]
/lib/libc.so.6(__libc_malloc+0x65)[0x4b228e75]
/usr/lib/libstdc++.so.6(_Znwj+0x28)[0x4b7e7378]
./a.out[0x804a140]
./a.out[0x8049fdc]
./a.out[0x8049d71]
./a.out[0x80499e3]
./a.out[0x8048d53]
./a.out[0x8048d88]
./a.out[0x8048d88]
./a.out[0x8048d88]
./a.out[0x8049618]
/lib/libc.so.6(__libc_start_main+0xf3)[0x4b1c56b3]
./a.out[0x8048a21]
======= Memory map: ========
08048000-0804c000 r-xp 00000000 00:23 7625066 /home/csu/ecx38/CS515/Assignment12/a.out
0804c000-0804d000 rw-p 00003000 00:23 7625066 /home/csu/ecx38/CS515/Assignment12/a.out
08c06000-08c27000 rw-p 00000000 00:00 0 [heap]
4b187000-4b1a8000 r-xp 00000000 08:03 2146357 /lib/ld-2.14.90.so
4b1a8000-4b1a9000 r--p 00020000 08:03 2146357 /lib/ld-2.14.90.so
4b1a9000-4b1aa000 rw-p 00021000 08:03 2146357 /lib/ld-2.14.90.so
4b1ac000-4b356000 r-xp 00000000 08:03 2146372 /lib/libc-2.14.90.so
4b356000-4b357000 ---p 001aa000 08:03 2146372 /lib/libc-2.14.90.so
4b357000-4b359000 r--p 001aa000 08:03 2146372 /lib/libc-2.14.90.so
4b359000-4b35a000 rw-p 001ac000 08:03 2146372 /lib/libc-2.14.90.so
4b35a000-4b35d000 rw-p 00000000 00:00 0
4b35f000-4b388000 r-xp 00000000 08:03 2146478 /lib/libm-2.14.90.so
4b388000-4b389000 r--p 00028000 08:03 2146478 /lib/libm-2.14.90.so
4b389000-4b38a000 rw-p 00029000 08:03 2146478 /lib/libm-2.14.90.so
4b3b0000-4b3cc000 r-xp 00000000 08:03 2146435 /lib/libgcc_s-4.6.3-20120306.so.1
4b3cc000-4b3cd000 rw-p 0001b000 08:03 2146435 /lib/libgcc_s-4.6.3-20120306.so.1
4b734000-4b816000 r-xp 00000000 08:03 232367 /usr/lib/libstdc++.so.6.0.16
4b816000-4b81a000 r--p 000e1000 08:03 232367 /usr/lib/libstdc++.so.6.0.16
4b81a000-4b81c000 rw-p 000e5000 08:03 232367 /usr/lib/libstdc++.so.6.0.16
4b81c000-4b822000 rw-p 00000000 00:00 0
b777b000-b777e000 rw-p 00000000 00:00 0
b77a6000-b77ab000 rw-p 00000000 00:00 0
b77ab000-b77ac000 r-xp 00000000 00:00 0 [vdso]
bff28000-bff49000 rw-p 00000000 00:00 0 [stack]
Aborted


when running this program:

#include <iostream>
#include <map>
#include <list>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <queue>
#include <math.h>
#include <vector>

using namespace std;

//Vertex Class Used for the nodes in the map
//////////////////////////////////////////////////////////////////////
class Post //
{ //
public: //
int x; //
int y; //
bool foodStation; //
bool monkey; //
//
Post() //
{ //
x = 0; //
y =0; //
foodStation = false; //
monkey = false; //
} //
//
Post(int xx, int yy) //
{ //
x = xx; //
y = yy; //
foodStation = false; //
monkey = false; //
} //
}; //
//////////////////////////////////////////////////////////////////////

double dist(Post start, Post end)
{
double xs = ((end.x - start.x)*(end.x - start.x));
double ys = ((end.y - start.y)*(end.y - start.y));

double sum = xs + ys;
double toReturn = sqrt(sum);

return toReturn;
}

void reset(vector<Post> & toReset)
{
while(!toReset.empty())
{
toReset.pop_back();
}
}
void findPath(vector<Post> toCheck, int sizeOfVector, Post starting, double &cyclePath, double &foodPathVal)
{
Post iter = starting;
double best = 1000;

//int debugging = 1;

//while(!toCheck.empty())
//{
if(toCheck.empty())
{

cerr << "Empty!" << endl;

}
else
{
Post tmpPost = toCheck[0];
Post returnPost = toCheck[0];
int location =0;

for(int a =1; a < sizeOfVector; a++)
{
double testDist = dist(iter,tmpPost);

if(best > testDist)
{
returnPost = tmpPost;
best = testDist;
location = a-1;
}

tmpPost = toCheck[a];
}

if(cyclePath < best)
{
cyclePath = best;
}

if(returnPost.foodStation)
{
foodPathVal = cyclePath;
}

int newSize = sizeOfVector -1;
vector<Post> newVector = toCheck;
newVector.erase(newVector.begin()+location);

findPath(newVector, newSize, returnPost, cyclePath, foodPathVal);

}
}


int main()
{
string input;

//vector<Post> graph;

while(getline(cin,input))
{

stringstream ss(input);

int postNumber =0;
int monkeyNumber =0;
int foodStationsNumber =0;
int emptyPostNumber =0;
int nonMonkeyNumber =0;
int maximumCapacity =0;
int subscript =0;

ss >> postNumber >> monkeyNumber >> foodStationsNumber >> maximumCapacity;
emptyPostNumber = (postNumber - monkeyNumber) - foodStationsNumber;
nonMonkeyNumber = postNumber - monkeyNumber;
vector<Post> graph(postNumber);
//cerr << "Segment 1" << endl;

for(int a=0; a<monkeyNumber; a++)
{
int xcoord =0;
int ycoord =0;
ss >> xcoord >> ycoord;
//cerr << "Segment 1.1" << endl;
Post monk;
//cerr << "Segment 1.2" << endl;
monk.x = xcoord;
monk.y = ycoord;
monk.monkey = true;
//cerr << "Segment 1.3" << endl;
graph[subscript] = monk;
//cerr << "Segment 1.4" << endl;
subscript += 1;
}

//cerr << "Segment 2" << endl;

for(int b=0; b < emptyPostNumber; b++)
{
int xcoord =0;
int ycoord =0;
ss >> xcoord >> ycoord;
Post * empt = new Post(xcoord, ycoord);
graph[subscript] = *empt;
subscript += 1;
}

//cerr << "Segment 3" << endl;

for(int c=0; c < foodStationsNumber; c++)
{
int xcoord =0;
int ycoord =0;
ss >> xcoord >> ycoord;
Post * foo = new Post(xcoord,ycoord);
foo->foodStation = true;
graph[subscript] = *foo;
subscript += 1;
}

//cerr << "Segment 4" << endl;

int counter =0;
vector<Post> partial(nonMonkeyNumber);
int d = monkeyNumber;

while(!graph[d].foodStation)
{
partial[counter] = graph[d];
counter++;
d++;
}

//cerr << "Segment 5" << endl;

int formattedSize = counter+1;
int foodCount =0;
vector<Post> ends(foodStationsNumber);
for(int e = formattedSize; e < postNumber; e++)
{
ends[foodCount] = graph[e];
foodCount+=1;
}

//cerr << "Segment 6" << endl;

for(int f = 0; f<monkeyNumber; f++)
{
double foodPathVal = 9999;
double cyclePath = 9999;
for(int g =0; g < foodStationsNumber; g++)
{
vector<Post> toUse(formattedSize+1);
for(int h =0; h < formattedSize+1; h++)
{
toUse[h] = partial[h];
}

toUse[formattedSize] = ends[g];
int totalSize = formattedSize+1;

Post tmp = graph[f];

//cerr << "Segment 7" << endl;

findPath(toUse, totalSize, tmp, cyclePath, foodPathVal);

//reset(toUse);

//cerr << "Segment 8" << endl;

}

int carriedFood = (maximumCapacity - int(foodPathVal*foodPathVal));
if(carriedFood < 0)
{
carriedFood =0;
}
cout << carriedFood << " ";
//reset(graph);
}
cout << "\n";

}
}


And have no clue what it means >=I
The error is usually due to dereferencing items that do not exists. For example:
1
2
int a[3];
int b = a[7]; // out of range error 


You don't test whether the Numbers are sane.

Why do you use new Post in segments 2 and 3? You never call delete on them.

You could set x and y of monk in constructor.

I would not create vector<Post> with size postNumber. I would call graph.reserve(postNumber) and then add Post's with graph.push_back().


PS. Use the code tags, please.
Topic archived. No new replies allowed.