title: C++ - pointer being freed was not allocated error
body: <pre><code>malloc: *** error for object 0x10ee008c0: pointer being freed was not allocated
  *** set a breakpoint in malloc_error_break to debug
  Abort trap: 6
  </code></pre>
  
  <p>Or I get this when I try and print everything</p>
  
  <pre><code>Segmentation fault: 11
  </code></pre>
  
  <p>I'm doing some homework for an OOP class and I've been stuck for a good hour now. I'm getting this error once I've used keyboard input enough. I am not someone who gets frustrated at all, and here I am getting very frustrated with this. Here are the files:</p>
  
  <p>This is the book class. I'm pretty sure this is very solid. But just for reference:</p>
  
  <pre><code>//--------------- BOOK.CPP ---------------
  // The class definition for Book.
  //
  
  #include &lt;iostream&gt;
  #include "book.h"
  
  using namespace std;
  
  Book::Book()
  // 
  {
    strcpy(title, " ");
    strcpy(author, " ");
    type = FICTION;
    price = 0;
  }
  
  void Book::Set(const char* t, const char* a, Genre g, double p)
  {
    strcpy(title, t);
    strcpy(author, a);
    type = g;
    price = p;
  }
  
  const char* Book::GetTitle() const
  {
    return title;
  } 
  
  const char* Book::GetAuthor() const
  {
    return author;
  }
  
  double Book::GetPrice() const
  {
    return price;
  }
  
  Genre Book::GetGenre() const
  {
    return type;
  }
  
  void Book::Display() const
  {
    int i;
  
    cout &lt;&lt; GetTitle();
    for (i = strlen(title) + 1; i &lt; 32; i++)
      cout &lt;&lt; (' ');
  
    cout &lt;&lt; GetAuthor();
    for (i = strlen(author) + 1; i &lt; 22; i++)
      cout &lt;&lt; (' ');
  
    switch (GetGenre())
    {
    case FICTION:
      cout &lt;&lt; "Fiction   ";
      break;
    case MYSTERY: 
      cout &lt;&lt; "Mystery   ";
      break;
    case SCIFI:
      cout &lt;&lt; "SciFi     ";
      break;
    case COMPUTER:
      cout &lt;&lt; "Computer  ";
      break;
    }
  
    cout &lt;&lt; "$";
    if (GetPrice() &lt; 1000)
      cout &lt;&lt; " ";
    if (GetPrice() &lt; 100)
      cout &lt;&lt; " ";
    if (GetPrice() &lt; 10)
      cout &lt;&lt; " ";
  
    /* printf("%.2f", GetPrice());*/
  
    cout &lt;&lt; '\n';
  }
  </code></pre>
  
  <p>This is the store class that deals with the array and dynamic allocation. This was working well without input commands, but just using its functions it was working like a champ.</p>
  
  <pre><code>//--------------- STORE.CPP ---------------
  // The class definition for Store.
  //
  #include &lt;iostream&gt;
  #include &lt;cstring&gt;  // for strcmp
  #include "store.h"
  
  using namespace std;
  
  Store::Store()
  {
    maxSize = 5; 
    currentSize = 0; 
    bookList = new Book[maxSize];
  }
  
  Store::~Store()
  // This destructor function for class Store
  // deallocates the Store's list of Books
  {
    delete [] bookList;
  }
  
  void Store::Insert(const char* t, const char* a, Genre g, double p)
  // Insert a new entry into the direrctory.
  {
    if (currentSize == maxSize)// If the directory is full, grow it.
      Grow();
  
    bookList[currentSize++].Set(t, a, g, p);
  }
  
  void Store::Sell(const char* t)
  // Sell a book from the store.
  { 
    char name[31];
    strcpy(name, t);
  
    int thisEntry = FindBook(name);// Locate the name in the directory.
  
    if (thisEntry == -1)
      cout &lt;&lt; *name &lt;&lt; " not found in directory";
    else
      {
        cashRegister = cashRegister + bookList[thisEntry].GetPrice();
        // Shift each succeding element "down" one position in the
        // Entry array, thereby deleting the desired entry.
        for (int j = thisEntry + 1; j &lt; currentSize; j++)
      bookList[j - 1] = bookList[j];
  
        currentSize--;// Decrement the current number of entries.
        cout &lt;&lt; "Entry removed.\n";
  
        if (currentSize &lt; maxSize - 5)// If the directory is too big, shrink it.
      Shrink();
      }
  }
  
  void Store::Find(const char* x) const
  //  Display the Store's matches for a title or author.
  {
    // Prompt the user for a name to be looked up
  
    char name[31];
    strcpy(name, x);
  
    int thisBook = FindBook(name);
    if (thisBook != -1)
      bookList[thisBook].Display();
  
    int thisAuthor = FindAuthor(name, true);
  
    if ((thisBook == -1) &amp;&amp; (thisAuthor == -1))
      cout &lt;&lt; name &lt;&lt; " not found in current directory\n";
  }
  
  void Store::DisplayGenre(const Genre g) const
  {
    double genrePrice = 0;
    int genreCount = 0;
  
    for (int i = 0; i &lt; currentSize; i++)// Look at each entry.
    {  
      if (bookList[i].GetGenre() ==  g)
      {
        bookList[i].Display();
        genrePrice = genrePrice + bookList[i].GetPrice();
        genreCount++;
      }
    }
    cout &lt;&lt; "Number of books in this genre: " &lt;&lt; genreCount
         &lt;&lt; "                    " &lt;&lt; "Total:    $";
    if (genrePrice &lt; 1000)
      cout &lt;&lt; " ";
    if (genrePrice &lt; 100)
      cout &lt;&lt; " ";
    if (genrePrice &lt; 10)
      cout &lt;&lt; " ";
  
    printf("%.2f", genrePrice);
  }
  
  void Store::DisplayStore() const
  {
    if (currentSize &gt;= 1)
    {
      cout &lt;&lt; "**Title**\t\t"
       &lt;&lt; "**Author**\t"
       &lt;&lt; "**Genre**\t"
       &lt;&lt; "**Price**\n\n";
  
      for (int i = 0; i &lt; currentSize; i++)
        bookList[i].Display();
    }
    else
      cout &lt;&lt; "No books currently in inventory\n\n";
  
    cout &lt;&lt; "Total Books = " &lt;&lt; currentSize 
         &lt;&lt; "\nMoney in the register = $";
    if (cashRegister &lt; 1000)
      cout &lt;&lt; " ";
    if (cashRegister &lt; 100)
      cout &lt;&lt; " ";
    if (cashRegister &lt; 10)
      cout &lt;&lt; " ";
  
    printf("%.2f", cashRegister);
  
    cout &lt;&lt; '\n';
  }
  
  void Store::Sort(char type)
  {
    Book temp;
  
    for(int i = 0; i &lt;= currentSize; i++)
    {
      for (int j = i+1; j &lt; currentSize; j++)
      {
        if (type == 'A')
        {
      if (strcmp(bookList[i].GetTitle(), bookList[j].GetTitle()) &gt; 0)
      {
        temp = bookList[i];
        bookList[i] = bookList[j];
        bookList[j] = temp;
      }
        }
        if (type == 'T')
        {
      if (strcmp(bookList[i].GetAuthor(), bookList[j].GetAuthor()) &gt; 0)
      {
        temp = bookList[i];
        bookList[i] = bookList[j];
        bookList[j] = temp;
      }
        }
      }
    }
  }
  
  void Store::SetCashRegister(double x)
  // Set value of cash register
  {
    cashRegister = x;
  }
  
  void Store::Grow()
  // Double the size of the Store's bookList
  // by creating a new, larger array of books
  // and changing the store's pointer to refer to
  // this new array.
  {
    maxSize = currentSize + 5;// Determine a new size.
  
    cout &lt;&lt; "** Array being resized to " &lt;&lt; maxSize 
         &lt;&lt; " allocated slots" &lt;&lt; '\n';
  
    Book* newList = new Book[maxSize];// Allocate a new array.
  
    for (int i = 0; i &lt; currentSize; i++)// Copy each entry into
      newList[i] = bookList[i];// the new array.
  
    delete [] bookList;// Remove the old array
    bookList = newList;// Point old name to new array.
  }
  
  void Store::Shrink()
  // Divide the size of the Store's bookList in
  // half by creating a new, smaller array of books
  // and changing the store's pointer to refer to
  // this new array.
  {
    maxSize = maxSize - 5;// Determine a new size.
  
    cout &lt;&lt; "** Array being resized to " &lt;&lt; maxSize 
         &lt;&lt; " allocated slots" &lt;&lt; '\n';
  
    Book* newList = new Book[maxSize];// Allocate a new array.
  
    for (int i = 0; i &lt; currentSize; i++)// Copy each entry into
      newList[i] = bookList[i];// the new array.
  
    delete [] bookList;// Remove the old array
    bookList = newList;// Point old name to new array.
  }
  
  int Store::FindBook(char* name) const
  // Locate a name in the directory.  Returns the
  // position of the entry list as an integer if found.
  // and returns -1 if the entry is not found in the directory.
  {
    for (int i = 0; i &lt; currentSize; i++)// Look at each entry.
      if (strcmp(bookList[i].GetTitle(), name) == 0)
        return i;// If found, return position and exit.
  
    return -1;// Return -1 if never found.
  }
  
  int Store::FindAuthor(char* name, const bool print) const
  // Locate a name in the directory.  Returns the
  // position of the entry list as an integer if found.
  // and returns -1 if the entry is not found in the directory.
  {
    int returnValue;
  
    for (int i = 0; i &lt; currentSize; i++)// Look at each entry.
      if (strcmp(bookList[i].GetAuthor(), name) == 0)
      {
        if (print == true)
      bookList[i].Display();
        returnValue = i;// If found, return position and exit.
      }
      else
        returnValue = -1;// Return -1 if never found.
  
    return returnValue;
  }
  </code></pre>
  
  <p>Now this is the guy who needs some work. There may be some stuff blank so ignore that. This one controls all the input, which is the problem I believe. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "store.h"
  
  using namespace std;
  
  void ShowMenu()
  // Display the main program menu.
  {
    cout &lt;&lt; "\n\t\t*** BOOKSTORE MENU ***";
    cout &lt;&lt; "\n\tA \tAdd a Book to Inventory";
    cout &lt;&lt; "\n\tF \tFind a book from Inventory";
    cout &lt;&lt; "\n\tS \tSell a book";
    cout &lt;&lt; "\n\tD \tDisplay the inventory list";
    cout &lt;&lt; "\n\tG \tGenre summary";
    cout &lt;&lt; "\n\tO \tSort inventory list";
    cout &lt;&lt; "\n\tM \tShow this Menu";
    cout &lt;&lt; "\n\tX \teXit Program";
  }
  
  char GetAChar(const char* promptString)
  // Prompt the user and get a single character,
  // discarding the Return character.
  // Used in GetCommand.
  {
    char response;// the char to be returned
  
    cout &lt;&lt; promptString;// Prompt the user
    cin &gt;&gt; response;// Get a char,
    response = toupper(response);// and convert it to uppercase
    cin.get();// Discard newline char from input.
    return response;
  }
  
  char Legal(char c)
  // Determine if a particular character, c, corresponds
  // to a legal menu command.  Returns 1 if legal, 0 if not.
  // Used in GetCommand.
  {
    return((c == 'A') || (c == 'F') || (c == 'S') || 
       (c == 'D') || (c == 'G') || (c == 'O') || 
       (c == 'M') || (c == 'X'));
  }
  
  char GetCommand()
  // Prompts the user for a menu command until a legal 
  // command character is entered.  Return the command character.
  // Calls GetAChar, Legal, ShowMenu.
  {
    char cmd = GetAChar("\n\n&gt;");// Get a command character.
  
    while (!Legal(cmd))// As long as it's not a legal command,
      {// display menu and try again.
        cout &lt;&lt; "\nIllegal command, please try again . . .";
        ShowMenu();
        cmd = GetAChar("\n\n&gt;");
      }
    return cmd;
  }
  
  void Add(Store s)
  {
    char aTitle[31];
    char aAuthor[21];
    Genre aGenre = FICTION;
    double aPrice = 10.00;
  
    cout &lt;&lt; "Enter title: ";
    cin.getline(aTitle, 30);
  
    cout &lt;&lt; "Enter author: ";
    cin.getline(aAuthor, 20);
    /*
    cout &lt;&lt; aTitle &lt;&lt; "  " &lt;&lt; aAuthor &lt;&lt; "\n";
    cout &lt;&lt; aGenre &lt;&lt; "  " &lt;&lt; aPrice &lt;&lt; '\n';
    */
    s.Insert(aTitle, aAuthor, aGenre, aPrice);
  
  }
  
  void Find()
  {
  }
  
  void Sell()
  {
  }
  
  void ViewGenre(Store s)
  {
    char c;
    Genre result;
  
    do
      c = GetAChar("Enter Genre - (F)iction, (M)ystery, (S)ci-Fi, or (C)omputer: ");
    while ((c != 'F') &amp;&amp; (c != 'M') &amp;&amp; (c != 'S') &amp;&amp; (c != 'C'));
  
    switch (result)
      {
      case 'F': s.DisplayGenre(FICTION);    break;
      case 'M': s.DisplayGenre(MYSTERY);    break;
      case 'S': s.DisplayGenre(SCIFI);      break;
      case 'C': s.DisplayGenre(COMPUTER);   break;
      }
  
  }
  
  void Sort(Store s)
  {
    char c;
    Genre result;
  
    do
      c = GetAChar("Enter Genre - (F)iction, (M)ystery, (S)ci-Fi, or (C)omputer: ");
    while ((c != 'A') &amp;&amp; (c != 'T'));
  
    s.Sort(c);
  }
  
  void Intro(Store s)
  {
    double amount;
  
    cout &lt;&lt; "*** Welcome to Bookstore Inventory Manager ***\n"
         &lt;&lt; "Please input the starting money in the cash register: ";
    /*  cin &gt;&gt; amount;
  
        s.SetCashRegister(amount);*/
  }
  
  int main()
  {
    Store mainStore;// Create and initialize a Store.
  
    Intro(mainStore);//Display intro &amp; set Cash Regsiter
  
    ShowMenu();// Display the menu.
  
    /*mainStore.Insert("A Clockwork Orange", "Anthony Burgess", SCIFI, 30.25);
      mainStore.Insert("X-Factor", "Anthony Burgess", SCIFI, 30.25);*/
  
    char command;// menu command entered by user
     do
       {
         command = GetCommand();// Retrieve a command.
         switch (command)
       {
       case 'A': Add(mainStore);             break;
       case 'F': Find();                     break;
       case 'S': Sell();                     break;
       case 'D': mainStore.DisplayStore();   break;
       case 'G': ViewGenre(mainStore);       break;
       case 'O': Sort(mainStore);            break;
       case 'M': ShowMenu();                 break;
       case 'X':                             break;
       }
       } while ((command != 'X'));
  
     return 0;
  }
  </code></pre>
  
  <p>Please, any and all help you can offer is amazing.
  Thank you.</p>