2

I am trying to dynamically allocate an array of records. When I run my program with ./a.out it appears to work fine but when I try and run the program inputing the data from a .txt file (./myProg < ) it repeatedly records the first line of text in the whole array and crashes and does not print out the chosen book in the end. My guess is I am not creating the array properly but I cannot seem to figure out what my problem is.

struct Book {
    char *title; //entered with no spaces
    int date; // in the form ddmmyy
};

Book *createRecord(int);
void input(Book *, int);
void display(Book *, int, int);
void destroyRecord(Book *);

int main() {
    int arrN = 0;
    int n = 0;

    cout << "Enter size of array: ";
    cin >> arrN;

    Book *bookArr;  
    bookArr = new Book[arrN];

    bookArr = createRecord(arrN);

    input(bookArr, arrN);

    cin.ignore();
    cout << "Book: ";
    cin >> n;

    display(bookArr, arrN, n);  
    destroyRecord(bookArr);

    return EXIT_SUCCESS; 
}


Book *createRecord(int arrN){
    struct Book *bookArr;
    bookArr = new Book[arrN];
    return bookArr;
}

void input(Book *bookArr, int arrN) {
    for(int i = 0; i < arrN; i++){
        char arrFirst[20];
        cin.ignore();
        cout << "Name: ";
        cin.getline(arrFirst, 20);
        strcpy((bookArr[i]).title = new char, arrFirst);
        cout << "Score: ";
        cin >> (bookArr[i]).date;
    }
}


void display(Book *bookArr, int arrN, int n) {
    if (0 <= n && n <= arrN){
        cout << (bookArr[n-1]).title << " " << (bookArr[n-1]).date << endl;
    }
}

void destroyRecord(Book *bookArr) {
   delete [] (bookArr)->title;
   delete bookArr;
}
0

3 Answers 3

6

Well, first up, you're allocating two arrays:

bookArr = new Book[arrN]; // <-- leaked
bookArr = createRecord(arrN);

That's a memory leak.

Secondly:

(bookArr[i]).title = new char

That is allocating one single char that you're trying to copy a whole string into. That should probably be:

// two lines please!
bookArr[i].title = new char[20];
strcpy(bookArr[i].title, arrFirst);

Thirdly:

if (0 <= n && n <= arrN){

That bounds check is incorrect. The upper bound should be n < arrN, and then just index n. By indexing n - 1, you might print the -1st index.

And last, but not least, prefer:

struct Book {
    std::string title;
    int date;
}

std::vector<Book> bookArr;
Sign up to request clarification or add additional context in comments.

Comments

0

Problem1:

Fix the array indexing problem in the display function:

void display(Book *bookArr, int arrN, int n) {
    if (0 <= n && n <= arrN){
        cout << (bookArr[n - 1]).title << " " << (bookArr[n - 1]).date << endl;
    }
}

to

void display(Book *bookArr, int arrN, int n) {
    if (0 < n && n <= arrN){
        cout << (bookArr[n - 1]).title << " " << (bookArr[n - 1]).date << endl;
    }
}

when n = 0, bookArr[-1] is throwing the error in your code.

Problem 2:

Allocate character array instead of single character to title in line, change:

    strcpy((bookArr[i]).title = new char, arrFirst);

to

    strcpy((bookArr[i]).title = new char[20], arrFirst);

Comments

-1

Not sure about the input problem, but you are definitely not creating the array as you should. In your code you are creating an array of books, then assigning a new array of books in the createRecord function and replacing the original array with it. Unlike what you are expecting to happen, you end up with an array of uninitialized Book pointers.

This is what you should be doing...

// Allocate an array of books
Book *bookArr; 
bookArr = new Book[arrN];

// Preallocate the records
for ( int iBook = 0; iBool < arrN; iBook++ )
{
  bookArr[ iBook ] = createRecord();
}

...


Book *createRecord()
{
  // Allocate one new book
  struct Book *pbook = new book;
  return pbook;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.