Search code examples
c++oopheap-corruption

Application has triggered a breakpoint due to heap corruption


My application throws an exception most probably due to heap corruption as the error code given is c0000374.

It does this when creating the object at line :

Library library2("2");

My code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Book
{
private:
    char author[50];
    char bookid[10];
    char title[80];
public:
    Book(char* nid, char* ntitle, char* nauthor) {
        strcpy(author, nauthor);
        strcpy(title, ntitle);
        strcpy(bookid, nid);
    }
    Book() {
        strcpy(author, "\0");
        strcpy(title, "\0");
        strcpy(bookid, "\0");
    }
    
    void operator= (Book b) {
        strcpy(author, b.getAuthor());
        strcpy(title, b.getTitle());
        strcpy(bookid, b.getBookID());
    }
    
    friend ostream& operator<< (ostream& out, Book b) {
        out << b.title << "\t" << b.author << "\t" << b.bookid << endl;
        return out;
    }
    
    friend bool operator== (Book b1, Book b2) {
        return (strcmp(b1.bookid, b2.bookid));
    }
    
    char* getAuthor() {     return author;  }
    char* getBookID() {     return bookid;  }
    char* getTitle() {      return title;   }
};

class Library {
private:
    //libId: holds the library ID
    string libId;
    //Pointer to the array that holds the book objects
    Book* bookArr;
    //holds the number of books added in the array.
    int bookscount;
    
    void resizeBookArray(Book book) {
        bookscount++;
        Book* newBookArr = new Book[bookscount];
        for (int i = 0; i < bookscount-1; i++)
        {
            newBookArr[i] = bookArr[i];
        }
        delete[] bookArr;
        bookArr = new Book[bookscount];
        newBookArr[bookscount] = book;
        bookArr = newBookArr;
        newBookArr = nullptr;
    }
public:
    
    Library(string alibId) {
        libId = alibId;
        bookscount = 0;
        bookArr = new Book[bookscount];
    }
    void operator= (Library lib) {
        bookscount = lib.getBookNum();
        copyBookArray(lib.getBookArr());
    }
    
    void addBook(Book book) {
        if (!search(book)) {
            resizeBookArray(book);
        }
    }
    
    bool search(Book book)
    {
        for (int i = 0; i < bookscount; i++)
        {
            if (bookArr[i] == book)
            {
                return true;
            }
        }
        return false;
    }
    
    friend ostream& operator<< (ostream& out, Library lib) {
        for (int i = 0; i < lib.bookscount; i++)
        {
            out << lib.bookArr[i];
        }
        return out;
    }
    string getID() { return libId; }
    int getBookNum() { return bookscount; }
    Book* getBookArr() { return bookArr; }

    void copyBookArray(Book* bArr) {
        delete[] bookArr;
        Book* bookArr = new Book[bookscount];
        for (int i = 0; i < bookscount; i++)
        {
            bookArr[i] = bArr[i];
        }
    }
};

void readBooksItems(Library& lib) {
    ifstream fin("book.txt");
    if (fin.fail()) cout << "Input file error!";
    char author[50];
    char bookid[10];
    char title[80];
    while (!fin.eof())
    {
        fin >> bookid;
        fin.ignore();
        fin.getline(title, 80);
        fin.getline(author, 50);
        Book btemp(bookid, title, author);
        lib.addBook(btemp);
    }
    fin.close();
}
//getMoreBooks function, gets additional book from the user, and adds the book into the array
Library getMoreBooks(Library lib)
{
    char bId[10], btitle[80], bauthor[50];
    cout << "Enter book id:";
    cin.getline(bId, 10);
    cout << "Enter title: ";
    cin.getline(btitle, 80);
    cout << "Enter author: ";
    cin.getline(bauthor, 50);
    Book b(bId, btitle, bauthor);
    lib.addBook(b);
    return lib;
}

int main()
{
    int numvideos, numbooks;
    Library library1("1");
    readBooksItems(library1);
    Library library2("2");
    library2 = library1;
    int nbooks;
    cout << "How many new books to add in library 1: ";
    cin >> nbooks;
    cin.ignore();
    for (int i = 0; i < nbooks; i++)
    {
        library1 = getMoreBooks(library1);
    }
    cout << "How many new books to add in library 2: ";
    cin >> nbooks;
    cin.ignore();
    for (int i = 0; i < nbooks; i++)
    {
        library2 = getMoreBooks(library2);
    }
    cout << "=======Printing all books in library 1========= " << endl;
    cout << library1;
    cout << "=======Printing all books in library 2========= " << endl;
    cout << library2;
    return 0;
}

Input file book.txt:

111
Cloud Computing
John Rapheal
222
Programming 1
Sam Smiths
333
UML 2 for Dummies
Michael Jesse Chonoles

Solution

  • This line of code writes past the end of the array, which triggers undefined behavior.

            newBookArr[bookscount] = book;