Debugging error _BLOCK_TYPE_IS_VALID (pHead-> nBlockUse)

I have a problem:

Failed to run debug check!

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

Line 52

Expression "_BLOCK_TYPE_IS_VALID (pHead-> nBlockUse)

My program will return all the values ​​to the screen that I expect, but this problem 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;
}
+4
source share
3 answers

This is a memory corruption error, and this is due to what you are deletenot doing new.

This code is incorrect:

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

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

, , delete, .

, return wsk1, wsk2; , , , , , .

+5

Lightness Races , "" .

, - (new, malloc - ), " ". . , , , , . wsk1[i] wsk1++, .

, reset , , ( , [i], , ).

0

I add another pointer, which is used to distinguish them by the "height" of the table, and all this thanks to the large number of guys for the consultation;)

-1
source

Source: https://habr.com/ru/post/1530336/


All Articles