【问题标题】:Attempt to print array element returns "Access violation reading location 0xCCCCCCCC. occurred"尝试打印数组元素返回“访问冲突读取位置 0xCCCCCCCC。发生”
【发布时间】:2020-07-15 01:26:38
【问题描述】:

此代码适用于一个项目,当给定这样的文本文件时:

14.99 24 Hat
29.99 31 Shirt
17.99 12 Shorts
5.50 18 Socks
-1 -1 endofdata

应该打印出各种各样的“收据”,但是当我尝试打印 array[i].name 时,我在第 73 行遇到了一个异常(在那里放了一个全部大写的注释)。 我尝试将其更改为 &array[i].name (以及我尝试打印的其他元素),它可以很好地打印地址。 我将衷心感谢您的帮助。代码如下所示。

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct prod {
    string name;
    float price;
    int inStock;
};

void swapName(string* name1, string* name2) {
    string temp = *name1;
    *name1 = *name2;
    *name2 = temp;
}

prod readInventory(prod array[], int max) {
    ifstream inventoryF("inventory.txt");
    if (inventoryF.fail()) {
        cout << "Unable to open input file.\n";
        for (int i = 0; i < 3; i++) {
            array[i].price = 0;
            array[i].inStock = 0;
            array[i].name = " ";
        }
    }
    else {
        int i = 0;
        while (array[i].price > 0) {

            inventoryF>> array[i].price;
            inventoryF >> array[i].inStock;
            inventoryF >>array[i].name;
            i += 1;
        } 
        cout << "Inventory read."<< endl;
    }
    return *array;
}

float totalValue(prod array[]) {
    int i = 0;
    float total = 0;
    while (array[i].price> 0) {
        total+=array[i].price* array[i].inStock;
        i++;
    }
    return total;
}

prod sortByName(prod array[]) {
    for (int i = 0; i < 5; i++) {
        if (array[i].name > array[i + 1].name) {
            swapName(&array[i].name, &array[i + 1].name);
        }
    }
    cout << "Poducts sorted by name.\n";
    return *array;
}

void writeReport(prod array[],int max) {
    cout <<setprecision(2)<< "+---------------------------+" << endl;
    cout << "|     Current Inventory     |" << endl;
    cout << "+---------------------------+" << endl;
    cout << left << setw(15) << "NAME" << setw(12) << "PRICE" << "#" << endl;
    cout << "------------  -------     ---" << endl;
    int j = 0;
    float total = totalValue(array);
    for (int i =0;i< max;i++){
        //PROBLEM IS ON THE LINE BELOW
        cout << left << setw(15) << array[i].name << setw(2) << "$" << array[i].price<< right << array[i].inStock<< endl;
        j++;
    }
    cout << "+---------------------------+" << endl;
    cout << left << setw(22) << "Number of products:" << j << endl;
    cout << setw(22) << "Inventory total value:" << total << endl;;
}


int main() {
    const int prodMax = 20;
    int current = 0;
    prod productArray[prodMax];
    prod temp = readInventory(productArray, prodMax);
    //temp = sortByName(&temp);
    writeReport(&temp, prodMax);
    system("pause");
    return 0;
}

【问题讨论】:

标签: c++ arrays exception memory-management


【解决方案1】:

您的readInventory() 函数本身就有缺陷。您正在返回一系列产品的初始产品。如果你想返回整个数组,你需要让 readInventory return prod* 并从 return *array 更改为 return array。意思是,通过将 &temp 传递给writeReport(),您传递了一个包含 1 个产品的数组,这当然会导致读取访问冲突。

【讨论】:

  • 另外你不能打印一个字符串而不包括
  • 并且不要使用普通数组,如果你需要 const 大小的数组,使用std::array,如果你需要可变大小的数组,使用std::vector
  • 一开始就返回一个输入参数是没有意义的。该函数可以简单地为void
  • 确实如此。真的,我只是想找到一个解决方案,一旦我找到它,我就在几秒钟内完成了修复。当然,一个人需要安定下来才能正确设计一切,但这不是 codereview 哈哈。
  • 谢谢。这是我在这里的第一个问题,您的解决方案有效。我们不允许使用向量,所以我不得不坚持使用数组。是否在所有情况下都将函数更改为指针允许您返回值而不是引用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
相关资源
最近更新 更多