Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allocating and releasing memory...

Status
Not open for further replies.

MattWoberts

Programmer
Oct 12, 2001
156
GB
Hi, I have this code which allocates a 2 dimensional array in C++:

Code:
        int subElements = 10;
	int ** myArray;
	myArray = (int **) new int*[lNumIntervals]; // Array of array pointers
		for (lTmp=0; lTmp<lNumIntervals; lTmp++)
	{
		myArray[lTmp] = (int *) new int[subElements];
		memset (myArray[lTmp],0,sizeof(int)*subElements);
	}

This works fine, and does the job. But in my code to release the array, I get a bad memory error (DAMAGE: After normal block). This is my code that causes the error:

Code:
	for (lTmp=0; lTmp<lNumIntervals; lTmp++)
	{
		delete [] myArray[lTmp];
	}
	delete [] myArray;

Any ideas? I'm a bit rusty on c++ so its stumped me!

Thanks.
Matt.
 
Darn,

Sorry all. Just as I posted this I found the solution.

In between the creation of the array and its cleanup, I had used memset to wipe it, but had gone beyond the size of the array with memset, hence nasty errors!

 
Aprppos: no need to cast new pointers implicitly in your case...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top