vector< vector<int> > resizeVector(vector< vector<int> > m)
{
vector< vector<int> > newMatrix;
int i,j;
for (i = 0; i < m[i].size(); i++)
{
for(j = 0; j < m[j].size(); j++)
{
newMatrix[i][j] = m[i][j];
}
}
return (newMatrix);
}
I am making a program that will do a whole lot of matrix manipulation, and this section is crashing and I don't exactly know why. I have narrowed it down to the line:
newMatrix[i][j] = m[i][j];
It crashes right here, and I am not sure why.
You're assigning into your new
newMatrix
without setting its size first. It will default to empty, and any attempt to assign to it will result in undefined behavior.Since you don't pass in a new size, it's hard to know exactly what you're trying to accomplish. That's why I don't have more explicit advice on how to fix it.
If you want to allocate vector of vector, the you need to allocate memory for the matrix before you index on it. So you will have to use something like
Or you could use .push_back() vector method to add values to the vector without allocating memory beforehand.
vector
soperator[]
returns a reference to the specified element without bounds checking.That means it does not magically resize the vector, or perform any other operations to ensure the element exists. If the element does not exist, the result is undefined behaviour - which means means anything can happen. Practically, it often causes the program to access an invalid memory location, which causes a crash.
The above is true even for a simple
vector<int>
. You've compounded your problems by working with avector<vector<int> >
(Each element of avector<vector<int> >
is avector<int>
).Your function is actually a little breathtaking in the number of times it potentially invokes undefined behaviour. You happen to be getting a crash on the statement
newMatrix[i][j] = m[i][j]
, but the potential for undefined behaviour actually occurs before then.In the outer loop,
m[i]
will not exist ifm.size()
(which your code doesn't check) has a value zero. Ifm.size()
is zero, that causes the evaluation ofm[i]
(asm.operator[](i)
) to have undefined behaviour.Essentially, you need to ensure any index
i
is valid before evaluatingm[i]
, and then ALSO ensure thatj
is a valid index ofm[i]
before evaluatingm[i][j]
. And then do the same fornewMatrix
. Your code does none of this at all. A more correct rendering of your function (assuming the intent is to create a copy ofm
) isNow, the thing is, your code is actually recreating functionality that vector provides already. So, we could replace the body of the function quite simply with
or even with
This means your function (as I've modified it) is misnamed - it doesn't resize anything. In fact, it is rather pointless since, if the caller does this
it could achieve the same effect without your function at all, simply as
which is also more efficient (no function call, no creating copies to pass by value, etc).
Your checks are wrong. Instead it should be
In addition to what @Saurav posted,
newMatrix
is empty so you cannot assign values tonewMatrix[i][j]
. You can fix this by initializing the vectors with a given size:Before the for-loops we initialize
newMatrix
to havem.size()
many empty vectors inside of it (the vectors are empty due to their default constructor). During each iteration of the outer for-loop we ensure that each vector withinnewMatrix
has the correct size using theresize
member function.Note that if you want a copy of a vector you can simply just write: