0

So i have been working on this code for a while, and I am not very skilled. I have created two classes one defines an item for a shopping list, and the other creates an array of the objects and outputs them as a list. The program works in my windows command prompt, but segfaults on GNU command line when I am outputting the array using cout statements.

void List::createList(Item ** itemPtr, int size)
{
    string** list1 = new string*[size];
    for(int i = 0; i < 5; i++)
    {
        list1[i] = new string[5];
    }
    for (int i = 0; i < size; i++)
    {
        list1[i][0] = itemPtr[i]->getName();
        list1[i][1] = itemPtr[i]->getUnit();
        list1[i][2] = itemPtr[i]->getSTRnumToBuy();
        list1[i][3] = itemPtr[i]->getSTRcost();
        list1[i][4] = itemPtr[i]->getSTRextCost();
    }
    cout << endl << left << fixed << setw(15) << setprecision(2) << "Name";
    cout << fixed << left << setw(15) << setprecision(2) << "Unit Type";
    cout << fixed << left << setw(15) << setprecision(2) << "# of units";
    cout << fixed << left << setw(15) << setprecision(2) << "Cost/Unit";
    cout << fixed << left << setw(15) << setprecision(2) << "Total" << endl;

    for (int i = 0; i < size; i++)
    {
        cout << fixed << left << setw(15) <<setprecision(2)<<endl<< list1[i][0];
        cout << fixed << left << setw(15) << setprecision(2) << list1[i][1];
        cout << fixed << left << setw(15) << setprecision(2) << list1[i][2];
        cout << fixed << left << setw(15) << setprecision(2) << list1[i][3];
        cout << fixed << left << setw(15) << setprecision(2) << list1[i][4];
    }
}
1
  • newing a standard container usually means you are doing something wrong. Why not use vectors of strings and avoid the headaches? Commented Oct 24, 2015 at 22:26

3 Answers 3

1

Your 1st loop should be :

for(int i = 0; i < size; i++)
{
  list1[i] = new string[5];
}

That is, loop till size, not till 5.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I feel like a bone head.
0
string** list1 = new string*[size];
for(int i = 0; i < 5; i++)

Looks like a bug to me: if size < 5 then you will be reading out of bounds of the array you have created.

list1[i][0] = itemPtr[i]->getName();
list1[i][1] = itemPtr[i]->getUnit();
list1[i][2] = itemPtr[i]->getSTRnumToBuy();
list1[i][3] = itemPtr[i]->getSTRcost();
list1[i][4] = itemPtr[i]->getSTRextCost();

This could fail if the size of the itemPtr array is less than size.

Another point, though not related to the crash you're seeing, you are leaking memory here. list1 is a local variable that's not stored somewhere in your class. You are allocating memory for it on the heap, but do not free that memory (delete) it anywhere.

1 Comment

Thanks Jan, I actually do have a line below the shared code to delete the array.
0

Your using C++ not C so use the power of the vector

void List::createList(Item ** itemPtr, int size) {
    vector<vector<string>> list1;
    list1.resize(size);
    for (int i = 0; i < size; i++) {
        list1[i].resize(5);
        list1[i][0] = itemPtr[i]->getName();
        list1[i][1] = itemPtr[i]->getUnit();
        list1[i][2] = itemPtr[i]->getSTRnumToBuy();
        list1[i][3] = itemPtr[i]->getSTRcost();
        list1[i][4] = itemPtr[i]->getSTRextCost();
    }
    cout << endl << left << fixed << setw(15) << setprecision(2) << "Name";
    cout << fixed << left << setw(15) << setprecision(2) << "Unit Type";
    cout << fixed << left << setw(15) << setprecision(2) << "# of units";
    cout << fixed << left << setw(15) << setprecision(2) << "Cost/Unit";
    cout << fixed << left << setw(15) << setprecision(2) << "Total" << endl;

    for (int i = 0; i < size; i++)
    {
        cout << fixed << left << setw(15) <<setprecision(2)<<endl<< list1[i][0];
        cout << fixed << left << setw(15) << setprecision(2) << list1[i][1];
        cout << fixed << left << setw(15) << setprecision(2) << list1[i][2];
        cout << fixed << left << setw(15) << setprecision(2) << list1[i][3];
        cout << fixed << left << setw(15) << setprecision(2) << list1[i][4];
    }
}

Look Ma, no news nor deletes.

1 Comment

Thanks Surt. I would have used vectors for this, however it was an assignment for a c++ class in which we were required to use arrays.

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.