@SouLWorkeR

Как исправить ошибку доступа к памяти?

C++ выдает такую ошибку:
"CRT detected that the application wrote to memory after end of heap buffer"
когда код доходит к деконструктору на освобождения памяти в масиве, никак не могу понять в чем дело. Подскажите как исправить мою ошибку?
Код:
#include <iostream>
using namespace std;

class shop
{
public:
	shop() {
		cout << "Start - shop" << endl;
	}

	virtual ~shop() {
		delete[]price_of_goods;
		cout << "End - shop" << endl;
	}
	/* Set */
	void set_number_of_goods() {
		int temp;
		cout << "Pls, write number of goods" << endl;
		cin >> temp;
		number_of_goods = temp;
	}
	
	void set_price_of_goods() {
		cout << "If you want to stop, pls, write 1001" << endl;
		for (int i = 0, price_of_goods_stop; i < number_of_goods; i++) {
			cin >> price_of_goods_stop;
			if (price_of_goods_stop == 1001) {
				break;
			}
			price_of_goods[i] = price_of_goods_stop;
		}
	}

	void set_top_good() {
		string temp;
		cout << "Pls, write your top good" << endl;
		cin >> temp;
		top_good = temp;
	}
	/*     */
	/* Get */
	void get_number_of_goods() {
		cout << number_of_goods << endl;
	}

	void get_top_good() {
		cout << top_good << endl;
	}

	void get_price_of_goods() {
		for (int i = 0; i < number_of_goods; i++) {
			cout << price_of_goods[i] << "-";
		}
		cout << endl;
	}
	/*     */
	virtual void read_information_about_goods() {
		get_top_good();
		get_number_of_goods();
		get_price_of_goods();
	}
	
	virtual void check_price() {
		int temp;
		int z = 0;
		cout << "Pls, write price of good which you need" << endl;
		cin >> temp;
		for (int i = 0; i < sizeof(price_of_goods)/sizeof(int); i++) {
			if (temp == price_of_goods[i]) {
				cout << "Yes, it has this price" << endl;
				z++;
				break;
			}
		}
		if (z == 0) {
			cout << "It doesn't have this price" << endl;
		}
	}
protected:
	string top_good;
	int number_of_goods;
	int* price_of_goods = new int[number_of_goods];
};

class supermarket : public shop
{
public:
	supermarket() {
		cout << "Start - supermarket" << endl;
	}

	~supermarket() {
		cout << "End - supermarket" << endl;
		delete[] works_hours;
	}
	/* Set */
	void set_name_of_supermarket() {
		cout << "Pls, write name of supermarket" << endl;
		string temp;
		cin >> temp;
		name_of_supermarket = temp;
	}

	void set_count_of_days() {
		int temp;
		cout << "Pls, write count of days" << endl;
		cin >> temp;
		count_of_days = temp;
	}

	void set_works_hours() {
		int temp;
		cout << "Pls, write hours, if you want to stop write 1001" << endl;
		for (int y = 0; y < count_of_days; y++) {
			cin >> temp;
			if (temp == 1001) {
				break;
			}
			works_hours[y] = temp;
		}
	}
	/*     */
	/* Get */
	void get_name_of_supermarket() {
		cout << "Name of supermarket - " << name_of_supermarket << endl;
	}

	void get_count_of_days() {
		cout << "Count of days - " << count_of_days << endl;
	}

	void get_works_hours() {
		cout << "Works hours" << endl;
		for (int i = 0; i < count_of_days; i++) {
			cout << works_hours[i] << endl;
		}
	}
	/*     */
	void operator = (const supermarket& other) {
		this->name_of_supermarket = other.name_of_supermarket;
	}

	void read_information_about_supermarket() {
		cout << name_of_supermarket << endl;
		cout << count_of_days << endl;
		get_works_hours();
	}

	void check_hours() {
		int temp;
		int z = 0;
		cout << "Pls, write hours which you need" << endl;
		cin >> temp;
		for (int i = 0; i < count_of_days; i++) {
			if (temp == works_hours[i]) {
				cout << "Yes, it has these hours" << endl;
				z++;
				break;
			}
		}
		if (z == 0) {
			cout << "It doesn't have these hours" << endl;
		}
	}
protected:
	string name_of_supermarket;
	int count_of_days;
	int *works_hours = new int[count_of_days];
};

int main() {
	supermarket soul;
	soul.set_number_of_goods();
	soul.set_top_good();
	soul.set_price_of_goods();
	soul.set_name_of_supermarket();
	soul.set_count_of_days();
	soul.set_works_hours();
	soul.get_count_of_days();
	soul.get_works_hours();
	soul.get_name_of_supermarket();
	soul.get_number_of_goods();
	soul.get_price_of_goods();
	soul.get_top_good();
	soul.read_information_about_goods();
	soul.check_price();
	soul.check_hours();
	soul.read_information_about_supermarket();
}
  • Вопрос задан
  • 929 просмотров
Решения вопроса 1
myjcom
@myjcom Куратор тега C++
SouLWorkeR,
инициализация идет у функциях выше

Извини, но ты пишешь немного не про то.
У тебя используются неиницилизированные переменные
int number_of_goods; // Вот тут рандомное значение
int* price_of_goods = new int[number_of_goods]; // ???


int count_of_days; // Вот тут тоже
int *works_hours = new int[count_of_days]; // ???


а здесь, ещё дополнительно

for (int i = 0; i < sizeof(price_of_goods)/sizeof(int); i++) {
      if (temp == price_of_goods[i]) {
        cout << "Yes, it has this price" << endl;
        z++;
        break;
      }

вот это
sizeof(price_of_goods) == sizeof(int*) / sizeof(int)
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы