Debug Assertion Failed _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

10,153

It's a memory corruption error, and it happens because you're deleteing things you didn't new.

This code is wrong:

wsk1 -=29;
wsk2 -=29;
wsk3 -=29;

You have thirty loop iterations, meaning thirty calls to ++, meaning you need to -= 30 also.

When you get that wrong, the pointers you pass to delete are incorrect.

Also, return wsk1, wsk2; does not do what you think it does, not that you're using the return values of those functions anyway.

Share:
10,153
Admin
Author by

Admin

Updated on June 08, 2022

Comments

  • Admin
    Admin about 2 years

    I'm having this problem:

    Debug Assertion Failed!

    File:f:\dd\vctools\crt_bld\self_x86\crt\dbgdel.cpp

    Line 52

    Expression" _BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)

    My program returns all values properly to the screen that I'm expecting, but this issue makes me nervous...

    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    
    
    using namespace std;
    
    double * wsk1;
    double * wsk2;
    double * wsk3;
    double * kopiowanie(double *wsk1,double *wsk2, double *wsk3);
    double * zaladuj(double *wsk1,double*wsk2);
    int main()
    {
        wsk1 = new double [30]; // tak inicjalizuje sie dynamicznie tablice
        wsk2 = new double [30];
        wsk3= new double [30];
        zaladuj(wsk1,wsk2);
        kopiowanie(wsk1,wsk2,wsk3);
        for (int i=0;i<30;i++)
        {
            cout << setw(10) << *wsk1 << setw(10) << *wsk2 << setw(10) << *wsk3 << endl;
            wsk1++;
            wsk2++;
            wsk3++;
        }
        wsk1 -=29;
        wsk2 -=29;
        wsk3 -=29;
    
    
        delete[] wsk1;
        delete[] wsk2;
        delete[] wsk3;
    
        system("pause");
    }
    
    double * zaladuj(double * wsk1, double * wsk2)
    {
    
            for(int i=0;i < 30;i++)
            {
                *wsk1 = i;
                *wsk2 = i;
                wsk1++;
                wsk2++;
            }
            wsk1 -=29 ;
            wsk2 -= 29;
    
            return wsk1, wsk2;
    
    }
    
    double * kopiowanie(double *wsk1,double*wsk2, double*wsk3)
    {
            for(int i=0; i<30;i++)
            {
                *wsk3 = (*wsk1)  * (*wsk2); 
                wsk3++;
                wsk2++;
                wsk1++;
            }
            wsk1 -=29;
            wsk2 -=29;
            wsk3 -=29;
            return wsk1,wsk2,wsk3;
    }