user3385889
user3385889

Reputation: 39

Debug Assertion Failed _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

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;
}

Upvotes: 4

Views: 10437

Answers (3)

user3385889
user3385889

Reputation: 39

I add another pointers which are used to delocate them over "height" of table and it all works thanks a lot guys for an advice ;)

Upvotes: -1

Mats Petersson
Mats Petersson

Reputation: 129524

As Lightness Races in Orbit says, you are calculating the "old" pointer wrong.

As a matter of principle, when you allocate something (with new, malloc, or some other method), it's always best to not modify the "original pointer". Make a copy instead, and work on that. Or in this case, you probably get better code from the compiler if you use an index variable, e.g. wsk1[i] instead of wsk1++ to get to the point you want.

Also, in your functions, you try to reset the pointer, which is completely unnecessary, since you are operating on a local copy (but again, you could use the [i] to index instead, which would make just as much sense, really).

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

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.

Upvotes: 5

Related Questions