C++ - 被释放的指针未分配错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7935603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:44:51  来源:igfitidea点击:

C++ - pointer being freed was not allocated error

c++inputcin

提问by jordaninternets

malloc: *** error for object 0x10ee008c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

Or I get this when I try and print everything

或者当我尝试打印所有内容时得到这个

Segmentation fault: 11

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:

我正在为 OOP 课程做一些家庭作业,现在我已经被困了一个小时。一旦我使用了足够的键盘输入,我就会收到此错误。我根本不是一个会感到沮丧的人,在这里我对此感到非常沮丧。以下是文件:

This is the book class. I'm pretty sure this is very solid. But just for reference:

这是书本课。我很确定这是非常可靠的。但仅供参考:

//--------------- BOOK.CPP ---------------
// The class definition for Book.
//

#include <iostream>
#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 << GetTitle();
  for (i = strlen(title) + 1; i < 32; i++)
    cout << (' ');

  cout << GetAuthor();
  for (i = strlen(author) + 1; i < 22; i++)
    cout << (' ');

  switch (GetGenre())
  {
  case FICTION:
    cout << "Fiction   ";
    break;
  case MYSTERY: 
    cout << "Mystery   ";
    break;
  case SCIFI:
    cout << "SciFi     ";
    break;
  case COMPUTER:
    cout << "Computer  ";
    break;
  }

  cout << "$";
  if (GetPrice() < 1000)
    cout << " ";
  if (GetPrice() < 100)
    cout << " ";
  if (GetPrice() < 10)
    cout << " ";

  /* printf("%.2f", GetPrice());*/

  cout << '\n';
}

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.

这是处理数组和动态分配的存储类。这在没有输入命令的情况下运行良好,但仅使用其功能它就像一个冠军。

//--------------- STORE.CPP ---------------
// The class definition for Store.
//
#include <iostream>
#include <cstring>  // 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 << *name << " 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 < currentSize; j++)
    bookList[j - 1] = bookList[j];

      currentSize--;// Decrement the current number of entries.
      cout << "Entry removed.\n";

      if (currentSize < 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) && (thisAuthor == -1))
    cout << name << " not found in current directory\n";
}

void Store::DisplayGenre(const Genre g) const
{
  double genrePrice = 0;
  int genreCount = 0;

  for (int i = 0; i < currentSize; i++)// Look at each entry.
  {  
    if (bookList[i].GetGenre() ==  g)
    {
      bookList[i].Display();
      genrePrice = genrePrice + bookList[i].GetPrice();
      genreCount++;
    }
  }
  cout << "Number of books in this genre: " << genreCount
       << "                    " << "Total:    $";
  if (genrePrice < 1000)
    cout << " ";
  if (genrePrice < 100)
    cout << " ";
  if (genrePrice < 10)
    cout << " ";

  printf("%.2f", genrePrice);
}

void Store::DisplayStore() const
{
  if (currentSize >= 1)
  {
    cout << "**Title**\t\t"
     << "**Author**\t"
     << "**Genre**\t"
     << "**Price**\n\n";

    for (int i = 0; i < currentSize; i++)
      bookList[i].Display();
  }
  else
    cout << "No books currently in inventory\n\n";

  cout << "Total Books = " << currentSize 
       << "\nMoney in the register = $";
  if (cashRegister < 1000)
    cout << " ";
  if (cashRegister < 100)
    cout << " ";
  if (cashRegister < 10)
    cout << " ";

  printf("%.2f", cashRegister);

  cout << '\n';
}

void Store::Sort(char type)
{
  Book temp;

  for(int i = 0; i <= currentSize; i++)
  {
    for (int j = i+1; j < currentSize; j++)
    {
      if (type == 'A')
      {
    if (strcmp(bookList[i].GetTitle(), bookList[j].GetTitle()) > 0)
    {
      temp = bookList[i];
      bookList[i] = bookList[j];
      bookList[j] = temp;
    }
      }
      if (type == 'T')
      {
    if (strcmp(bookList[i].GetAuthor(), bookList[j].GetAuthor()) > 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 << "** Array being resized to " << maxSize 
       << " allocated slots" << '\n';

  Book* newList = new Book[maxSize];// Allocate a new array.

  for (int i = 0; i < 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 << "** Array being resized to " << maxSize 
       << " allocated slots" << '\n';

  Book* newList = new Book[maxSize];// Allocate a new array.

  for (int i = 0; i < 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 < 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 < 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;
}

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.

现在这是需要一些工作的人。可能有一些空白所以忽略它。这个控制所有输入,这是我认为的问题。

#include <iostream>
#include "store.h"

using namespace std;

void ShowMenu()
// Display the main program menu.
{
  cout << "\n\t\t*** BOOKSTORE MENU ***";
  cout << "\n\tA \tAdd a Book to Inventory";
  cout << "\n\tF \tFind a book from Inventory";
  cout << "\n\tS \tSell a book";
  cout << "\n\tD \tDisplay the inventory list";
  cout << "\n\tG \tGenre summary";
  cout << "\n\tO \tSort inventory list";
  cout << "\n\tM \tShow this Menu";
  cout << "\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 << promptString;// Prompt the user
  cin >> 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>");// Get a command character.

  while (!Legal(cmd))// As long as it's not a legal command,
    {// display menu and try again.
      cout << "\nIllegal command, please try again . . .";
      ShowMenu();
      cmd = GetAChar("\n\n>");
    }
  return cmd;
}

void Add(Store s)
{
  char aTitle[31];
  char aAuthor[21];
  Genre aGenre = FICTION;
  double aPrice = 10.00;

  cout << "Enter title: ";
  cin.getline(aTitle, 30);

  cout << "Enter author: ";
  cin.getline(aAuthor, 20);
  /*
  cout << aTitle << "  " << aAuthor << "\n";
  cout << aGenre << "  " << aPrice << '\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') && (c != 'M') && (c != 'S') && (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') && (c != 'T'));

  s.Sort(c);
}

void Intro(Store s)
{
  double amount;

  cout << "*** Welcome to Bookstore Inventory Manager ***\n"
       << "Please input the starting money in the cash register: ";
  /*  cin >> amount;

      s.SetCashRegister(amount);*/
}

int main()
{
  Store mainStore;// Create and initialize a Store.

  Intro(mainStore);//Display intro & 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;
}

Please, any and all help you can offer is amazing. Thank you.

拜托,你能提供的任何帮助都是惊人的。谢谢你。

回答by cdleonard

Welcome to the exciting world of C++!

欢迎来到激动人心的 C++ 世界!

Short answer: you're passing Store as a value. All your menu functions should take a Store& or Store* instead.

简短回答:您将 Store 作为值传递。你所有的菜单功能都应该使用 Store& 或 Store* 来代替。

When you're passing Store as a value then an implicit copy is done (so the mainStore variable is never actually modified). When you return from the function the Store::~Store is called to clean up the copied data. This frees mainStore.bookList without changing the actual pointer value.

当您将 Store 作为值传递时,就会完成一个隐式副本(因此 mainStore 变量实际上从未被修改过)。当您从函数返回时,调用 Store::~Store 来清理复制的数据。这在不更改实际指针值的情况下释放了 mainStore.bookList。

Further menu manipulation will corrupt memory and do many double frees.

进一步的菜单操作会破坏内存并进行多次双释放。

HINT: If you're on linux you can run your program under valgrind and it will point out most simple memory errors.

提示:如果您使用的是 linux,您可以在 valgrind 下运行您的程序,它会指出最简单的内存错误。

回答by Rob?

Your Storecontains dynamically-allocated data, but does not have an assignment operator. You have violated the Rule of Three.

Store包含动态分配的数据,但没有赋值运算符。你违反了三定律

回答by Aman

I don't see the Store class being instantiated anywhere by a call to new Store()which means the booklist array has not been created but when the program exits and calls the destructor, it tries to remove the array that was never allocated and hence that's why i think you are getting this error. Either, modify the destructor to have a null check or instantiate the class by a call to the constructor. Your code shouldn't still be working anywhere you are trying to use a Storeobject.

我没有看到 Store 类通过调用在任何地方实例化,new Store()这意味着尚未创建 booklist 数组,但是当程序退出并调用析构函数时,它会尝试删除从未分配的数组,因此这就是我认为的原因你收到这个错误。要么修改析构函数以进行空检查,要么通过调用构造函数来实例化类。您的代码不应在您尝试使用Store对象的任何地方仍然有效。

Hope this helps

希望这可以帮助