CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2005

    _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.

    // 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
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012

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

     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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.4)

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.