Alright so I have this code that compiles nicely but when it outputs I get these messages that I have no idea wtf is going on. Could someone help me with whats going on?
Output:
*** glibc detected *** ./a.out: free(): invalid pointer: 0xb76617b4 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x73e42)[0xb7533e42]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb772d51f]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1b)[0xb771499b]
/usr/lib/i386-linux-gnu/libstdc++.so.6(+0x909dc)[0xb77149dc]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSs6assignERKSs+0x98)[0xb7716478]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSsaSERKSs+0x23)[0xb77164c3]
./a.out[0x8048e51]
./a.out[0x8049594]
./a.out[0x8048fb8]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb74d94d3]
./a.out[0x8048d21]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 07:00 785817 /home/brianjustice/CS/a.out
0804a000-0804b000 r--p 00001000 07:00 785817 /home/brianjustice/CS/a.out
0804b000-0804c000 rw-p 00002000 07:00 785817 /home/brianjustice/CS/a.out
0817b000-0819c000 rw-p 00000000 00:00 0 [heap]
b7492000-b7494000 rw-p 00000000 00:00 0
b7494000-b74be000 r-xp 00000000 07:00 267853 /lib/i386-linux-gnu/libm-2.15.so
b74be000-b74bf000 r--p 00029000 07:00 267853 /lib/i386-linux-gnu/libm-2.15.so
b74bf000-b74c0000 rw-p 0002a000 07:00 267853 /lib/i386-linux-gnu/libm-2.15.so
b74c0000-b765f000 r-xp 00000000 07:00 267780 /lib/i386-linux-gnu/libc-2.15.so
b765f000-b7661000 r--p 0019f000 07:00 267780 /lib/i386-linux-gnu/libc-2.15.so
b7661000-b7662000 rw-p 001a1000 07:00 267780 /lib/i386-linux-gnu/libc-2.15.so
b7662000-b7666000 rw-p 00000000 00:00 0
b7666000-b7682000 r-xp 00000000 07:00 263235 /lib/i386-linux-gnu/libgcc_s.so.1
b7682000-b7683000 r--p 0001b000 07:00 263235 /lib/i386-linux-gnu/libgcc_s.so.1
b7683000-b7684000 rw-p 0001c000 07:00 263235 /lib/i386-linux-gnu/libgcc_s.so.1
b7684000-b775c000 r-xp 00000000 07:00 916057 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b775c000-b775d000 ---p 000d8000 07:00 916057 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b775d000-b7761000 r--p 000d8000 07:00 916057 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7761000-b7762000 rw-p 000dc000 07:00 916057 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7762000-b7769000 rw-p 00000000 00:00 0
b7778000-b777b000 rw-p 00000000 00:00 0
b777b000-b777c000 r-xp 00000000 00:00 0 [vdso]
b777c000-b779c000 r-xp 00000000 07:00 267748 /lib/i386-linux-gnu/ld-2.15.so
b779c000-b779d000 r--p 0001f000 07:00 267748 /lib/i386-linux-gnu/ld-2.15.so
b779d000-b779e000 rw-p 00020000 07:00 267748 /lib/i386-linux-gnu/ld-2.15.so
bf968000-bf989000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
My code is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
class Queue;
class City {
friend class Queue;
friend class DataOrganizer;
Queue *q; // List of cities this one is directly connected to
City *from; // To trace a path back to origin
string name; // Name of the city
bool mark; // false=unmarked, true=visiting or done
City *nextOne;
public:
City ();
void setName (string theName);
void neighbor (City *c);
City *next ();
void setMark();
void setFrom(City* g);
void displayBestRoute ();
bool checkMark();
//~City () { delete q; if (name != NULL) delete name; }
};
//################## Queue class ######################
class Queue {
friend class City;
City *tail;
public:
Queue() { tail = NULL; }
void enqueue(City *t) {
if (t == NULL) return;
if (tail == NULL) {
tail = new City();
tail->setName(t->name);
tail->nextOne = tail; }
else {
City *h = new City();
h->setName(t->name);
h->nextOne = tail->nextOne;
tail->nextOne = h;
tail = h;
}
}
City *dequeue() {
if (tail == NULL) return NULL;
City *ptr = tail->nextOne;
if (ptr != tail) tail->nextOne = ptr->nextOne;
else tail = NULL;
City *t = ptr;
delete ptr;
return t;
}
int isEmpty() { return tail == NULL; }
};
City:: City () { q = new Queue(); from = NULL; mark = false; name = ""; nextOne = NULL; }
void City::setName (string theName) { name = theName; }
void City::neighbor (City *c) { q->enqueue(c); }
City *City::next () { return (City *)q->dequeue(); }
void City::setMark() { mark = true; }
void City::setFrom(City* g) { from = g; }
bool City::checkMark() {
if (mark == true) return true;
if (mark == false) return false;
}
void City::displayBestRoute () { // displays route back to city of origin
cout << name << " ";
while (from != NULL) {
cout << from->name << " ";
from = from;
}
}
///// Organizes Data
class DataOrganizer {
int count; // Counts the number of cities
string name; // accepts input from the file
City **cities;
public:
DataOrganizer () { count = 0; }
void readInput (char *filename) {
fstream *fin = new fstream(filename, ios::in);
while (true) {
string buffer;
*fin >> buffer;
if (buffer.compare("-") == 0) break;
count ++; }
fin->close();
fin->clear();
string tok;
cities = new City*[count];
fin->open(filename, ios::in);
size_t mark = fin->tellg();
for (int i=0; *fin>>tok && tok != "-"; i++) cities[i]->setName(tok);
for (int i=0; i < count; i++) {
for (int j=0; *fin>>tok && tok != "-"; j++) {
int n;
istringstream(tok) >> n;
cities[i]->neighbor(cities[n]);
}
}
fin->close();
fin->clear();
}
City *getOrigin() { return cities[0]; }
City *getDestination() { return cities[count]; }
};
int main (int argc, char **argv) {
if (argc != 2) {
cout << "Usage: " << argv[0] << " \n";
exit(0);
}
DataOrganizer dao;
dao.readInput(argv[1]);
// Set origin and destination cities
City *origin = dao.getOrigin();
City *destin = dao.getDestination();
City *city;
Queue *q = new Queue (); // Simulate simultaneity
origin->setMark();
q->enqueue(origin);
while (!q->isEmpty()) {
City *current = (City*) q->dequeue();
while((city = current->next()) != NULL) {
if (city == destin) {
city->setFrom(current);
cout << "Origin: " << origin << endl;
cout << "Destin: " << destin << endl;
city->displayBestRoute();
return 0;
}
if (!(city->checkMark() == true)) {
city->setMark();
city->setFrom(current);
q->enqueue(city);
}
}
}
cout << "No Route Between Selected Cities\n";
}
|