Program stopped working, when tried to run

You have your main problem here.

double *tabT1;
double *tabT2;
tabT1[0]=1;
tabT1[1]=tabX[j]*(-1);
tabT2[0]=1;
tabT2[1]=tabX[i]*(-1);

You haven’t allocated the memory, instead, you have just declared the double ptrs tabT1 and tabT2 and accessing them by pretending you have allocated.

double *tabT1 = new double[2];
double *tabT2 = new double[2];

will fix this, however, I strongly suggest you to use smart pointers instead, which is much safer in your case, as it looks like you have a lot of pointer arrays.


Edit: Above fix will lead to memory leak as you are not bothering about deleting these pointer arrays, after their use, each time end of the for loop. One possible smart pointer fix could be:

std::unique_ptr<double[]> tabTT = nullptr;  // change
.....
.....

if(tabX[i] != tabX[j] && i<j)
{
   std::unique_ptr<double[]> tabT1 = std::unique_ptr<double[]>(new double[2]);  // change
   std::unique_ptr<double[]> tabT2 = std::unique_ptr<double[]>(new double[2]);  // change
   .....
   .....
   tabTT = mnozenie(std::move(tabT1), std::move(tabT2),sizeW,sizeW);
   .....
}

And in the function:

auto mnozenie(std::unique_ptr<double[]> A, std::unique_ptr<double[]> B, int m, int n)
{
   std::unique_ptr<double[]> prod = std::unique_ptr<double[]>(new double[m+n-1]);
   // calculations....    
   return std::move(prod);
}

As a side note: double tabX[wierz]; type of declaration(variable length array) has been forbidden in ISO C++ standards. The alternative is std::vector<>, which will also give you dynamically allocated a contiguous array.

Leave a Comment