Cant free memory.

Posted by atch on Stack Overflow See other posts from Stack Overflow or by atch
Published on 2010-04-02T09:47:24Z Indexed on 2010/04/02 9:53 UTC
Read the original article Hit count: 194

Filed under:
|

In code:

int a[3][4] = {1,2,3,4,
               5,6,7,8,
               9,10,11,12};


template<class T, int row, int col>
void invert(T a[row][col])
{
T* columns = new T[col];
T* const free_me = columns;
for (int i = 0; i < col; ++i)
{
    for (int j = 0; j < row; ++j)
    {
        *columns = a[j][i];
        ++columns;//SOMETIMES VALUE IS 0
    }
}
delete[] free_me;//I'M GETTING ERROR OF HEAP ABUSE IN THIS LINE
}
int main(int argc, char* argv[])
{
    invert<int,3,4>(a);
}

I've observed that while iterating, value of variable columns equals zero and I think thats the problem.
Thanks for your help.

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointer