-1

I'm having an interesting problem when I'm accessing a double vector. The idea is that I have deleted all information prior to accessing the vector. A for loop tries to access the vector and successful says that the vector is empty, but when I access the vector point directly it shows that there are variables still in the vector.

Also, the vector was set up like so:

vector<vector<string>> proTable;

Here is the loop attempting to access the vector.

    for(int a = 0; a < proTable.size(); a++)
    {
            for(int b = 0; b < proTable[a].size(); b++)
            {
                    cout << proTable[a][b] << "\t";
            }
    }

But if I edit the for loop this way it returns the variable inside.

        for(int a = 0; a < proTable.size(); a++)
    {
            for(int b = 0; b < proTable[a].size(); b++)
            {
                    cout << proTable[a][b] << "\t";
            }
            cout << proTable[0][0];
    }

The first prints nothing out. The second prints X which was in the vector before. Also, the vector does not show that it is empty.

This is how I was deleting it if it matters.

void MRelation::RemoveColumn(vector<int> rem)
{
    while(!rem.empty())
    {
            int z = rem[rem.size() - 1];
            for(int a = 0; a < proTable.size(); a++)
            {
                    for(int b = z; b < proTable[a].size() - 1; b++)
                    {
                            proTable[a][b] = proTable[a][b+1];
                    }
                    proTable[a].pop_back();
            }
            rem.pop_back();
    }
}

The vector rem holds the columns that need to be deleted from the table.

JKdub
  • 9
  • 2
  • 1
    Instead of having random snippets of code, please instead include a **[mcve]** that demonstrates your problem. You should be able to achieve this in about 10-20 lines of code – Tas Oct 31 '17 at 21:28
  • At the bare minimum, some judiciously sprinkled `cerr << something` in your program will probably tell you more about where your assumptions and your actual code diverge. Ideally, this should be run in a debugger, where you can step through your code and find the precise point where things go south. Also, I suspect your `rem` list of "things to delete" is concretely based on the *before* state of your matrix; not *interactive* state. To accommodate that, ensure `rem` is sorted *before* attempting your removal loops. – WhozCraig Oct 31 '17 at 21:30
  • 2
    If `proTable` is indeed empty, `proTable[0][0]` may crash, or return some value that might happen to be "right", purely by luck of the draw. That means absolutely nothing, whatsoever. – Sam Varshavchik Oct 31 '17 at 21:30
  • You may want to read [this question](https://stackoverflow.com/q/6441218/1782465) and its top-voted answer. It talks about function-scope local variables instead of dynamic allocation like in your case, but the principle is the same. Accessing memory to which you still have a pointer but no "legal rights" is UB. – Angew is no longer proud of SO Oct 31 '17 at 22:08
  • My guess is that your code (which we don't see all of) is engaging in Undefined Behaviour by reading variables which were already deleted (or similar). If my crystal ball is correct, then the answer is simply: your program is *meaningless* and any observed behaviour is irrelevant. In any case, please post a [mcve]. – Jesper Juhl Oct 31 '17 at 22:21

2 Answers2

1

I have deleted all information prior to accessing the vector.

Accessing an vector out of bounds has undefined behaviour. Since your vector is empty, proTable[0] is out of bounds. In the line cout << proTable[0][0];, you access proTable[0]. Therefore the behaviour of your program is undefined.

it shows that there are variables still in the vector.

You cannot jump to such conclusion from observing undefined behaviour.

"There are variables still in the vector" was not necessarily the reason why you saw output. You saw output because the behaviour was undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

I found out what it was. I you delete the contents of the inner vector but don't delete the vectors themselves then the vector will think that it contains something still and will pull out information that doesn't exist anymore. here is the code I was have problems with. It has been edited with an if statement to correct it.

void MRelation::FinalPrint()
{
    if(curTable.size() < 2)
    {
            ss << "? No\n";
    }       
    else
    {
            ss << "? Yes(" << curTable.size() - 1 << ")\n";
    }       
    if(!proTable[0].empty()) //This was added in after to correct the problem
    {
            for(int c = 1; c < proTable.size(); c++)
            {
                    ss << "  " << proTable[0][0] << "=" << proTable[c][0];
                    for(int d = 1; d < proTable[0].size(); d++)
                    {
                            ss << ", ";
                            ss << proTable[0][d] << "=" << proTable[c][d];
                    }
                    ss << "\n";
            }
    }
}

Sorry about not putting everything in context before. I was trying to put in as much relavant information without putting in 300 lines of code.

JKdub
  • 9
  • 2