CavemanAGz
CavemanAGz

Reputation: 43

Seg fault while outputting 2-d array of strings

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];
    }
}

Upvotes: 0

Views: 38

Answers (3)

Surt
Surt

Reputation: 16099

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.

Upvotes: 0

nikolas
nikolas

Reputation: 8975

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.

Upvotes: 0

Anonymous Coward
Anonymous Coward

Reputation: 3200

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.

Upvotes: 1

Related Questions