0

Need help.. Please tell me why " A.clear() " does not clear the first column too? ........................................................

#include <iostream>
#include <vector>
using namespace std;

int N, M;
vector< vector<int> > A;
int main()
{
    cin >> N >> M;
    A.resize(N + 2);
    for (int i = 0; i <= N; ++i)
    {
        A[i].resize(M + 2);
    }
    A.clear();
    for (int i = 0; i <= N; ++i)
    {
        for (int j = 0; j <= M; ++j)
        {
            cout << A[i][j] << ' ';
        }
        cout << '\n';
    }
    return 0;
}
user1948703
  • 3
  • 1
  • 3
  • 2
    1. What output do you expect? 2. What output do you get? Hint: you are accessing things that used to be in the `vector` before you `clear`ed it. What do you think is there now? Is it safe to access it? – BoBTFish Jan 04 '13 at 13:38
  • You posted working code, which is great; but as BoBTFish suggested you need to be more precise in your question. [Here](http://liveworkspace.org/code/1mislX$0) is your code rewritten with `at()` calls instead of `operator []` calls on the vector object. – Matthieu M. Jan 04 '13 at 13:43
  • I will, but this is my first post and I didn't know. Thank you! – user1948703 Jan 04 '13 at 14:31

3 Answers3

8

A.clear() does clear the array in the sense that as the result, A contains zero elements. This is not the same as setting every element to zero.

Your code has undefined behaviour since the post-A.clear() loop accesses elements past the end of the now empty vector. It just so happens that the memory is still accessible and still contains the old data. However, this is not guaranteed to be the case.

If you iterated using the correct dimensions, you'd see that A is empty:

for (int i = 0; i < A.size(); ++i)
{
    for (int j = 0; j < A[i].size(); ++j)
    {
        cout << A[i][j] << ' ';
    }
    cout << '\n';
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    By using the `at` function instead that overloaded `[]` you could catch such things. – Jack Jan 04 '13 at 13:38
5

You are invoking undefined behaviour. A.clear() is working fine, but you are reading memory that you shouldn't be. Try with "< A.size()" instead of "<= N"

janm
  • 17,976
  • 1
  • 43
  • 61
2

clear removes all elements of the vector as opposed to setting all of them to 0 as you seem to be expecting. After calling clear the size of your vector is 0. Thus when you try to read A[i][j] you are accessing an index out of bounds and anything may happen(your code causes undefined behavior).

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • I understand now, thank you! But, how should I allocate a dynamic matrix of N x M size ? – user1948703 Jan 04 '13 at 13:47
  • 2
    @user1948703 it is perfectly fine and I believe the best solution is to use a vector for that. Still to fill a vector with zeros, use `std::fill` defined in algorithm or simply do a cycle. `clear` is what you do wrong, not the rest of the code. Also please always check the sizes of the vector(i.e. don't use M and N, but `A.size()` and `A[i].size()`) – Ivaylo Strandjev Jan 04 '13 at 13:51
  • A.resize(N + 2); fill(A.begin(), A.end(), vector (M + 1)); That's working. Is it good? – user1948703 Jan 04 '13 at 13:57
  • @user1948703 It does, only thing I don't get is why first size is `N+2`. Also please note you may initialize the vector during resize like: `A.resize(N + 2, vector(M+1, 0));`. – Ivaylo Strandjev Jan 04 '13 at 14:00
  • That's it, thank you very much, I figured it out now :) – user1948703 Jan 04 '13 at 14:05