-
February 26th, 2014, 05:01 AM
#1
_BLOCK_TYPE_IS_VALID(pHead->(nBlockUse) debug assertion error
EDIT: NEVER MIND, I totally blanked out and didn't realize that I was actually trying to delete an array that WASN"T created on the heap! IGNORE -> I can't figure out why I'm getting a _BLOCK_TYPE_IS_VALID debug assertion error. It's associated with the last line of code that deletes memory created on the heap. The program adds 2 to the ptr pointer, subscripts it with ptr[0], displays the value, then subtracts 2 to get the pointer back to the beginning of the array, and attempts to free the memory. But I get the error.
Code:
// Subscripting a pointer to an array after pointer arithmetic.
#include <iostream>
using namespace std;
int main()
{
int intArray[5] {0, 1, 2, 3, 4};
int *ptr = new int[5];
ptr = intArray;
cout << "intArray: {";
for (int i = 0; i < 5; ++i)
cout << ptr[i] << " ";
cout << "}\n\n";
cout << "Subscripting a pointer to intArray: \nptr[0]: " << ptr[0] << endl << endl;
cout << "Adding 2 to ptr: (ptr + 2)\n";
ptr += 2;
cout << "ptr[0] is now " << ptr[0] << endl << endl;
cout << "Subtracting 2 from ptr: (ptr - 2)\n";
ptr -= 2;
cout << "ptr[0] is now " << ptr[0] << "\n\n";
delete [] ptr; // Gets a _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) debug assertion failure error
return 0;
}
Last edited by spiritualfields; February 26th, 2014 at 05:08 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|