There is a function to enter a new book (author, title, year, price) NewBook . After entering the second book, the program crashes. I don’t know what the reason is (probably something somewhere with memory allocation, but I don’t know where I wrote something wrong.) Before that, I made two exactly the same tasks, and there everything was fine, and then it crashes. In debugger watched - everything seems to be OK. I throw the code all. The second book is entered, but after entering crashes.
#include <iostream> using namespace std; struct Books { char *author; char *nazva; int year; int price; }; void NewBook(Books*&, int&); void Deleting(Books*&, int&); void Del(Books*&, int); void Print(Books*&, int&); void main() { Books *boo = NULL; int size = 0; for (int i = 0; i < 3; i++) { NewBook(boo, size); } Print(boo, size); Deleting(boo, size); } void Deleting(Books*&boo, int &size) { if (boo = NULL) cout << "Its okay=) You have'nt any book \n;)" << endl; else { } } void Del(Books *&boo, int size) { for (int i = 0; i < size; i++) delete[]boo[i].author; for (int i = 0; i < size; i++) delete[]boo[i].nazva; delete[]boo; } void Print(Books *&boo, int &size) { for (int i = 0; i < size; i++) { cout << boo[i].author << "\t" << boo[i].nazva << "\t" << boo[i].year << "\t" << boo[i].price << " UAH" << endl; } } void NewBook(Books*&boo, int &size) { if (boo == NULL) { boo = new Books[size + 1]; cout << "Author? " << endl; char*aut = new char[40]; cin.getline(aut, 40); boo[size].author = new char[strlen(aut) + 1]; strcpy_s(boo[size].author, strlen(aut) + 1, aut); //delete[]aut; cout << "Nazva? " << endl; char*aut1 = new char[40]; cin.getline(aut1, 40); boo[size].nazva = new char[strlen(aut1) + 1]; strcpy_s(boo[size].nazva, strlen(aut1) + 1, aut1); cout << "Year? " << endl; cin >> boo[size].year; cout << "Price?" << endl; cin >> boo[size].price; } else { Books *tmp = new Books[size + 1]; for (int i = 0; i < size; i++) { tmp[i].author = new char[strlen(boo[i].author) + 1]; strcpy_s(tmp[i].author, strlen(boo[i].author) + 1, boo[i].author); tmp[i].nazva = new char[strlen(boo[i].nazva) + 1]; strcpy_s(tmp[i].nazva, strlen(boo[i].nazva) + 1, boo[i].nazva); tmp[i].price = boo[i].price; tmp[i].year = boo[i].year; } cout << "Author? " << endl; char*aut = new char[40]; cin.get(); cin.getline(aut, 40); tmp[size + 1].author = new char[strlen(aut) + 1]; strcpy_s(tmp[size + 1].author, strlen(aut) + 1, aut); //delete[]aut; cout << "Nazva? " << endl; char*aut2 = new char[40]; cin.getline(aut2, 40); tmp[size + 1].nazva = new char[strlen(aut2) + 1]; strcpy_s(tmp[size + 1].nazva, strlen(aut2) + 1, aut2); cout << "Year? " << endl; cin >> tmp[size + 1].year; cout << "Price?" << endl; cin >> tmp[size + 1].price; Del(boo, size); /*for (int i = 0; i < size; i++) delete[]boo[i].author; for (int i = 0; i < size; i++) delete[]boo[i].nazva; delete[]boo;*/ boo = tmp; } size++; }
if (boo = NULL). However, the problem is not in it, it does not reach it ... - Harryc+:) Is it really interesting that this is some kind of flash mob source, where do the functions have a lot of parameter references to pointers? Or some new video course out? - PinkTux