446,406 Members | 888 Online Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

# _CrtIsValidHeapPointer Debug Assertion

 P: 2 Hi... So I'm writing some code that does some matrix operations (namely multiplications) I've created a separate class for manipulating the matrices. The matrix itself has memory for it allocated within the class. Here's a sample of what I'm talking about... Expand|Select|Wrap|Line Numbers Matrix* Matrix::multiply(const Matrix &b) {   assert(c == b.r);   Matrix *result = new Matrix(r, b.c);   for (int i = 0; i < b.c; i++)     for (int j = 0; j < r; j++)       for (int k = 0; k < c; k++)         result->matrix[i][j] += matrix[i][k] * b.matrix[k][j];   return result; } The Constructor... Allocates a two dimensional array of type double, which can be referenced using array[i][j] notation. Expand|Select|Wrap|Line Numbers Matrix::Matrix(int _r, int _c) {   r = _r;   c = _c;   matrix = (double**)malloc(r*sizeof(double*));   for (int i = 0; i < r; i++) {     matrix[i] = (double*)malloc(c*sizeof(double));     memset(matrix[i], 0x00, c*sizeof(double));   } } The Destructor - frees allocated memory Expand|Select|Wrap|Line Numbers Matrix::~Matrix() {   for (int i = 0; i < r; i++)     free(matrix[i]);   free(matrix); } And the code that calls it... Expand|Select|Wrap|Line Numbers Matrix a(3,1); Matrix b(3,3); Matrix *r = NULL;   //values fo the matrix are initialized here...   r = b.multiply(a); r->print(); delete r;       //source of error...   So it compiles and runs under linux, and GDB doesn't catch an error, and there is no error when multipling a 3x2 by a 2x3 matrix...also, sometimes the program will exit normally (the error doesn't happen all the time, more like about 90% of the time... I'm flustered, thanks in advance to anyone that has suggestions! Jun 27 '06 #1 