_BLOCK_TYPE_IS_VALID(pHead->(nBlockUse) debug assertion error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: _BLOCK_TYPE_IS_VALID(pHead->(nBlockUse) debug assertion error

  1. #1
    Join Date
    Dec 2005
    Posts
    251

    _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 04:08 AM.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: _BLOCK_TYPE_IS_VALID(pHead->(nBlockUse) debug assertion error

    Code:
     int intArray[5] {0, 1, 2, 3, 4};
       int *ptr = new int[5];
       ptr = intArray;
    This gives a memory leak as the memory address initially stored in ptr from the new is overwritten to be the address of intArray.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center