473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

_CrtIsValidHeapPointer Debug Assertion

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
  1. Matrix* Matrix::multiply(const Matrix &b) {
  2.   assert(c == b.r);
  3.   Matrix *result = new Matrix(r, b.c);
  4.   for (int i = 0; i < b.c; i++)
  5.     for (int j = 0; j < r; j++)
  6.       for (int k = 0; k < c; k++)
  7.         result->matrix[i][j] += matrix[i][k] * b.matrix[k][j];
  8.   return result;
  9. }
The Constructor... Allocates a two dimensional array of type double, which can be referenced using array[i][j] notation.

Expand|Select|Wrap|Line Numbers
  1. Matrix::Matrix(int _r, int _c) {
  2.   r = _r;
  3.   c = _c;
  4.   matrix = (double**)malloc(r*sizeof(double*));
  5.   for (int i = 0; i < r; i++) {
  6.     matrix[i] = (double*)malloc(c*sizeof(double));
  7.     memset(matrix[i], 0x00, c*sizeof(double));
  8.   }
  9. }
The Destructor - frees allocated memory

Expand|Select|Wrap|Line Numbers
  1. Matrix::~Matrix() {
  2.   for (int i = 0; i < r; i++)
  3.     free(matrix[i]);
  4.   free(matrix);
  5. }
And the code that calls it...

Expand|Select|Wrap|Line Numbers
  1. Matrix a(3,1);
  2. Matrix b(3,3);
  3. Matrix *r = NULL;
  4.  
  5. //values fo the matrix are initialized here...
  6.  
  7. r = b.multiply(a);
  8. r->print();
  9. delete r;       //source of error...
  10.  
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
2 4496
Banfa
9,065 Expert Mod 8TB
I can see nothing obviously wrong, however I would not use the memory allocation technique you have.

Just because the data represents a matrix which is a 2 dimensional array does not mean you have to allocate it like that. For a 2 by 3 matrix all you actually need is space to store 6 doubles. This makes the constructor and destructor (obviously with a redefinition of matrix to type double *)

Expand|Select|Wrap|Line Numbers
  1. Matrix::Matrix(int _r, int _c) {
  2.   r = _r;
  3.   c = _c;
  4.   matrix = (double*)malloc(r*c*sizeof(double));
  5.   memset(matrix, 0x00, r*c*sizeof(double));
  6. }
  7.  
  8. Matrix::~Matrix() {
  9.   free(matrix);
  10. }
  11.  
As you can see the memory allocation is a lot simpler and has the added benafit of causing less memory fragmentation.

Next you add an inline member function in the header file a bit like

Expand|Select|Wrap|Line Numbers
  1.     inline int index(int i, int j) {
  2.         return i*c+j;
  3.     }
  4.  
then

Expand|Select|Wrap|Line Numbers
  1. /* instead of */
  2.  
  3. matrix[i][j] = ...;
  4.  
  5. /* use */
  6.  
  7. matrix[index(i,j)] = ...;
  8.  
This is a common technique for handling comples indexes, you can build various shape and dimension arrays this way.

On to you problem I believe it lies at least partly in this code

Expand|Select|Wrap|Line Numbers
  1. Matrix* Matrix::multiply(const Matrix &b) {
  2.   assert(c == b.r);
  3.   Matrix *result = new Matrix(r, b.c);
  4.   for (int i = 0; i < b.c; i++)
  5.     for (int j = 0; j < r; j++)
  6.       for (int k = 0; k < c; k++)
  7.         result->matrix[i][j] += matrix[i][k] * b.matrix[k][j];
  8.   return result;
  9. }
  10.  
In matrix[i][k] i and k have the wrong range for matrix, k 0 to c-1 may be ok but i 0 to b.c-1 does not necessarily have the same range as 0 to r-1.
Jun 27 '06 #2
Thanks Banfra...I'll have to try that.

What baffles me about the problem is that it compiles and runs under linux...and produces proper output for larger matrices (i.e. 3x2 multiplied by a 2x3). It's the visual studio compiler that causes it to crash, and not even all the time....

For example, I just ran it now, and it did the multiplication and returned a correct result... Running a second time caused the debug assertion (on the same dataset)...

I'll try your fixes though and see if that helps.
Jun 27 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Eyal | last post by:
Hi, We have an issue with Debug Assertion showing in Release builds in Managed C++! I have created a small managed C++ project that looks like this: #include "stdafx.h" #using <mscorlib.dll>...
2
by: Arti Potnis | last post by:
Hi, I have an application with a function "myfunction" that opens a file and writes to it using fprintf. This application runs on a unix (sun solaris 5.8) system. I connect to this application...
4
by: Adriano Coser | last post by:
I'm having a _CrtIsValidHeapPointer assertion at the _free_dbg_lk function. I'm calling a function from a DLL witch allocates (malloc) a char array and the exception occours freeing the array. ...
0
by: jiing | last post by:
When I execute my program (it's multithread and has COM and dll), there is an error message as follows: Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe File: dbgheap.c Line: 1132 Expression:...
0
by: jiing | last post by:
When I execute my program (it's multithread and has COM and dll), there is an error message as follows: Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe File: dbgheap.c Line: 1132 Expression:...
4
by: Mullai | last post by:
Hi , My program gives an error message like this Debug Assertion Failed! program:................ File: wincore.cpp Line: 958 Please can anyone help me out in this issue. I have to solve...
8
by: Zytan | last post by:
I am getting a Debug.Assert() firing which shows a mesasge so large that it is cropped. But it crops off the beginning. So, I have no idea what assertion is. My code is not complex. It's just...
2
by: Pushpa | last post by:
Hi All, This my part of the c++ program using threads in windows : //modified by pushpa struct structExrdDoc { CExrdDoc* spDoc; LPCTSTR sstrFileName;
6
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have a native DLL which is called from a managed application. One of the calls to the DLL returns a std::vector<Cell(by value) where Cell is a POD type, everything works fine until the function...
3
by: =?Utf-8?B?SnNDaGFybHk=?= | last post by:
Hello, My application is using a Dll loaded with the function "LoadLibrary(<NameOfTheDll>)" In the dll there is the following fonction : char * GetMessageError(int Error) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.