gpt4 book ai didi

c++ - 需要帮助解决内存泄漏

转载 作者:行者123 更新时间:2023-11-30 03:13:57 25 4
gpt4 key购买 nike

我正在编写一个使用对象的动态数组的库存管理系统。这样做的策略不是使用 vector ,而是分配一个动态数组,每次客户端需要添加到库存时,该数组都会递增 1。我收到“段错误”错误,所以我认为这是内存泄漏。

我曾尝试重写它以匹配地址,但没有成功。我认为 buildItem 函数会产生一个临时对象,该对象会在函数结束时被销毁。不过我不知道如何解决这个问题。

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

class InventoryObject {
private:
int itemNumber;
string description;
int qty;
float price;

public:
int getItemNum() { return itemNumber; }
string getDescription() { return description; }
int getQty() { return qty; }
float getPrice() { return price; }

void storeInfo(int p, string d, int q, float pr);
void showValues(InventoryObject &item);
};

// Function Implementation
void InventoryObject::storeInfo(int p, string d, int q, float pr) {
itemNumber = p;
description = d;
qty = q;
price = pr;
}

void InventoryObject::showValues(InventoryObject &item) {
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Part Number : " << item.getItemNum() << endl;
cout << "Description : " << item.getDescription() << endl;
cout << "Quantity: : " << item.getQty() << endl;
cout << "Price : " << item.getPrice() << endl << endl;
}

// Function Prototypes for Client Program
InventoryObject buildItem();
void drawMenu();
void showValues(InventoryObject &);
void printInventory(int size);

int main() {
int size = 1;
int choice;
bool quit = false;
InventoryObject part;
InventoryObject *iArray = new InventoryObject[size];
drawMenu();
cin >> choice;
while (quit == false) {
if (choice == 1) {
InventoryObject item;
item = buildItem();
iArray[size] = item;
}
if (choice == 2) {
iArray[size].showValues(iArray[size]);
}
if (choice == 3) {
quit = true;
}
}

return 0;
}

// This function accepts the data from the client and creates a new
// InventoryObject object. The object is then supposed to be added to the
// dynamic array.
InventoryObject buildItem() {
InventoryObject *tempObject = new InventoryObject;
int itemNum;
string description;
int qty;
float price;

cout << "Enter data for the item you want to enter:\n\n";
cout << "Item Number: \n";
cin >> itemNum;
cout << "Description: \n";
cin.get();
getline(cin, description);
cout << "Quantity: \n";
cin >> qty;
cout << "Unit price: \n";
cin >> price;

tempObject->storeInfo(itemNum, description, qty, price);
return *tempObject;
}

void drawMenu() {
cout << "1. Add Inventory\n";
cout << "2. Display Inventory\n";
cout << "3. Quit Program\n";
}

我希望创建对象并将其放入动态数组中。然后重新绘制菜单并从那里与客户端交互。

最佳答案

主要问题是您写入的内存超出了为数组 (iArray) 分配的内存区域。

具体来说,这行代码:

iArray[size] = item;

实际上应该是:

iArray[size - 1] = item;

上述不会导致内存泄漏,但您在程序中做的其他事情会:

您的函数 buildItem 在返回指针值而不先删除指针时执行此操作。

要解决此问题,请更改

InventoryObject *tempObject = new InventoryObject;

InventoryObject tempObject;

最后,记得在 main 中 return 0; 之前删除 iArray

delete[] iArray

关于c++ - 需要帮助解决内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58327584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com