Thread: Aborted (core dumped)? Error when running

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    Question Aborted (core dumped)? Error when running

    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
    Code:
    #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"; }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You might want to reformat the code in your post. Nobody is going to attempt to debug a complete program rendered as a single line (which is how the code in your post appears).

    I certainly will not look at your code in its current form. However, I will offer a general comment.....

    The first line of the errors you have reported is telling. Your code is passing a pointer to free() that should not be passed to free().

    Only pointers returned by malloc(), calloc(), or realloc() - or NULL pointers - should be passed to free(). At most once.

    So you can't free() a pointer twice. You can't pass a string literal to free(). You can't pass the address of some local variable to free(). You can't pass something created with operator new() to free(). If you have a pointer x returned by malloc(), don't pass x+1 to free().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    oh sorry im new to this website form and I dont dont know how to make the code bigger?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Brian Justice View Post
    oh sorry im new to this website form and I dont dont know how to make the code bigger?
    It was probably not your fault, the formatting problems seem to happen at random after posting code.
    Try pasting it again.. copy as plain text, if you didn't before.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try entering newlines (carriage returns) on each line. If copying and pasting from something else, it does depend on how you copy and paste (and what program you copy and paste from) and I can't help you there.

    In any event, have a look at the general comments I made. I've listed the likely causes (albeit generically) of your problem. You will learn more by trying to work out the specifics for your code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    4
    Code:
    //Brian Justice
    //Assignment 6
    // 5/17/12
    #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 ();
    };
    
    //################## 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;
        }
    }
    //City::~City() { delete q; if (name != NULL) delete name; }
    
    
    /////  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);
      City *current = NULL;
      while (!q->isEmpty()) {
        if (current != NULL) current->~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";
    }

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    C programmer here (learning C++); but does not dequeue() return a pointer to memory you just gave back to the Operating System?

    Tim S.

    Code:
      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;
      }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    4
    This my new code which should fix the dequeue problem but I am still getting that error message. Could you guys look at the void readInput() function but because i think it may have something to do with that.


    Code:
    //Brian Justice
    //Assignment 6
    // 5/17/12
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sstream>
    using namespace std;
    
    class Queue;
    class City;
    
    class Cell {
      friend class Queue;
      Cell *next; City *object;
    public:
      Cell (City *o, Cell *n) { next = n; object = o; }
    };
    
    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
    
    public:
      City ();
      void setName (string theName);
      void neighbor (City *c);
      City *next ();
      void setMark();
      void setFrom(City* g);
      void displayBestRoute ();
      bool checkMark();
      //~City ();
    };
    
    //################## Queue class ######################
    class Queue {
      friend class City; 
      Cell *tail;
    public:
      Queue() { tail = NULL;  }
    
      void enqueue(City *t) {
        if (t == NULL) return;
        if (tail == NULL) {
          tail = new Cell(t, NULL);
          tail->next = tail;
        }
        else {
          Cell *h = new Cell (t, tail->next);
          tail->next = h;
          tail = h;
        }
      }
    
      City *dequeue() {
        if (tail == NULL) return NULL;
        Cell *ptr = tail->next;
        City *t = ptr->object;
        if (ptr != tail) tail->next = ptr->next; 
        else tail = NULL;
        delete ptr;
        return t; 
      }
      int isEmpty() { return tail == NULL;  }
    }; 
    
    City:: City () { q = new Queue();  from = NULL;  mark = false;  name = ""; }
    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;
        }
    }
    //City::~City() { delete q; if (name != NULL) delete name; }
    
    
    /////  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 == "-") break;
          count ++; }
        fin->close();  
        fin->clear();
        
        string tok;
        cities = new City*[count];
        fin->open(filename, ios::in);
        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 buffer(tok);
        buffer >> 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";
    }

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    First thing I would do is stop allocation fin dynacmically. That is a very abnormal thing to do, and at the very least results in a memory leak here.
    Just use the constructor:
    Code:
    fstream fin(filename, ios::in);
    Next, you're creating a lot of city pointers, but you're not pointing any of them at anything and then just proceed to use them. You probably mean to allocate a City object for each pointer, to point to.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 02:29 PM
  2. Bus Error (Core Dumped) with sscanf
    By saadahmed42 in forum C Programming
    Replies: 10
    Last Post: 04-12-2010, 01:28 PM
  3. Bus error (Core Dumped)
    By RandomX in forum C Programming
    Replies: 10
    Last Post: 12-11-2006, 09:45 AM
  4. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:14 AM
  5. Segmantation Fault (core dumped) error
    By Vinnie66 in forum C Programming
    Replies: 6
    Last Post: 03-25-2002, 01:34 PM