-2

I am currently trying to execute the following cpp program on Windows:

#include <cstdlib>
#include <iostream>
#include "math.h"
#include <fstream>

using std:: cin; using std:: cout; using std:: endl; using std::ofstream;



int main()
{
    //numerical method parameters
    
    const int Nx = 500; // nodes count in x-space
    const int Ny = 500; // nodes count in y-space
    double hx, hy, tau;
    hx = 1; // x variable step
    hy = 1; // y step
    
    tau = (hx > hy) ? hy :hx; // time step = min(hx,hy)
    
    const int Nt = 1000;

    // grid
    double *x = new double[2];  double *y = new double[2]; 
    x[0] = 0.0;  y[0] = 0.0;  

    for (int i = 0; i < Nx; i++)
    {
        x[i + 1] = x[i] + hx;
    }
    for (int j=0; j < Ny; j++)
    {
        y[j + 1] = y[j] + hy;
    }
    

    double **Up = new double*[Nx]; // storing values from the previous step U(n+1/2)
    double **Up1 = new double*[Nx]; // U(n)
    double **Heat_Coeff = new double*[Nx]; // thermal conductivity (lambda)

    double A, C, B; // coeff-s of the tridiagonal matrix 
    double F; // F - right side (system of linear equations)

    double **alpha = new double *[Nx]; // numerical method parameters for "grid function" computation
    double **betta = new double *[Nx]; 

    for(int i=0; i<Nx; i++)
    {
        alpha[i] = new double[Ny];    
        betta[i] = new double[Ny];

        Up[i] = new double[Ny];
        Up1[i] = new double[Ny];
        Heat_Coeff[i] = new double[1];
    }
    //==================================

    for(int i=0; i<Nx; i++)
    {
        for(int j=0; j<Ny; j++)
        {
            Up1[i][j]=300; // init. cond 
            Heat_Coeff[i][j] = 1;
            alpha[i][j] = 0.0;
            //Up[i][j] = Up1[i][j]; // copy for the numerical method
        }
    }

    //Upper and lower boundary conditions
    for (int j = 0; j < Ny; j++)
    {
        Up1[0][j] = Up1[j][1];
        Up1[Nx-1][j] = Up1[Nx-j-1][Ny-2];
        //Up[0][j] = Up1[0][j];
        //Up[Nx-1][j] = Up1[Nx-1][j];
        betta[0][j] = Up1[0][j];
        betta[Nx-1][j] = Up1[Nx-1][j];
    }

    //left and right boundary conditions
    for (int i = 0; i < Nx; i++)
    {
        Up1[i][0] = 50;
        Up1[i][Ny-1]= 100;
        //Up[i][0] = Up1[i][0];
        //Up[i][Ny-1] = Up1[i][Ny-1];
        betta[i][0] = Up1[i][0];
        betta[i][Ny-1] = Up1[i][Ny-1];      
    }


    for(int time=0; time<Nt; time++) // time loop
    {
        cout << "time: " << time << endl << endl; 
              
        for(int i=0; i< Nx; i++)
        {
            for(int j=0; j<Ny; j++)
            {
                Up[i][j] = Up1[i][j];
            }
        }


        for(int j=1; j< Ny-1; j++) // y-loop
        {     
            for(int i=1; i< Nx-1; i++)
            {
                A = - (Heat_Coeff[i-1][j] + Heat_Coeff[i][j]) / (4*hx*hx);
                B = - (Heat_Coeff[i+1][j] + Heat_Coeff[i][j]) / (4*hx*hx);      
                C = 1/tau - A - B;  
                F = Up[i][j]/tau + ((Heat_Coeff[i][j+1] + Heat_Coeff[i][j]) *(Up[i][j+1]-Up[i][j])-(Heat_Coeff[i][j-1]+Heat_Coeff[i][j])*(Up[i][j]-Up[i][j-1]))/(4*hy*hy);
                    
                alpha[i][j] = -B / (C+A*alpha[i-1][j]);
                betta[i][j] = (F - A*betta[i-1][j])/(C + A*alpha[i-1][j]);
                //cout << "Received beta: " << betta[i][j] << endl << endl; //getch();
            }
    
            for(int i=Nx-1; i>1; i--)
            {
                Up1[i-1][j]=alpha[i-1][j]*Up1[i][j]+betta[i-1][j]; // find Up
                //Up[i][j] = Up1[i][j];
            }
        }

        
        for (int i = 0; i < Nx; i++)
        {
            for (int j = 0; j < Ny; j++)
            {
                Up[i][j] = Up1[i][j]; // remember array
            }   
        }
                
        for(int i=1; i<Nx-1; i++) // x-loop
        {
            for(int j=1; j< Ny-1; j++)
            {
                A = - (Heat_Coeff[i][j-1] + Heat_Coeff[i][j]) / (4*hy*hy);
                B = - (Heat_Coeff[i][j+1] + Heat_Coeff[i][j]) / (4*hy*hy);
                C = 1/tau - A - B;
                
                F = Up[i][j]/tau + ((Heat_Coeff[i+1][j] + Heat_Coeff[i][j])*(Up[i+1][j]-Up[i][j])-(Heat_Coeff[i-1][j]+Heat_Coeff[i][j])*(Up[i][j]-Up[i-1][j]))/(4*hx*hx);
                
                alpha[i][j] = -B / (C+A*alpha[i][j-1]);
                betta[i][j] = (F - A*betta[i][j-1])/(C + A*alpha[i][j-1]);
            }

            for(int j=Ny-1; j>1; j--)
            {
                Up1[i][j-1]=alpha[i][j-1]*Up1[i][j]+betta[i][j-1]; // find Up1
                //Up[i][j] = Up1[i][j];
            }
        }

    }

    ofstream outt;
    //outt.precision(3);
    outt.open("heat.txt");
    for (int i = 0; i < Nx; i++)
    {
        for (int j = 0; j < Ny; j++)
        {
            outt <<  Up1[i][j] << "  ";
        }
        outt << ";" << endl;
    }
    outt.close();
    

    //system ("Pause");
    return 0;
}

However every time I launch the a.exe output I get the following error message:

[process exited with code 3221226356 (0xc0000374)]

The fun fact is that whenever I execute this program on macOS, I don't get any error at all, I really don't know what goes wrong here on windows.

Thank you for your help

  • You have `double *x = new double[2];` which allocates an array of *two* elements. Then you use the loop `for (int i = 0; i < Nx; i++)` which with the value of `Nx` being `500` will go way out of bounds of your two-element array, and you will have *undefined behavior*. Same problem with `y` and `Ny`. – Some programmer dude May 06 '22 at 11:17
  • 1
    The problem in the shown code is called "pointless use of pointers". Absolutely nothing shown requires pointers, or `new`, or `delete`. The crash is caused by flawed use of pointers, `new`, and `delete`. Replacing them with proper use of containers, iterators, and algorithms will eliminate the reason for the crash in the first place. – Sam Varshavchik May 06 '22 at 11:17
  • It seems like x is a double array with size of 2, and you try to access after its end. Some OSs crash on this error. – user107511 May 06 '22 at 11:17
  • This is very C-type code and you probably messed up with some pointer. Try to use `std::vector`. – WhatsUp May 06 '22 at 11:18
  • 1
    why are you using `new` to allocate arrays of fixed size? You shouldnt use `new` at all, even when the size is not known at compile time (then use `std::vector`) – 463035818_is_not_a_number May 06 '22 at 11:19
  • @Someprogrammerdude thank you for your answer, I am not gonna lie I really have no Idea how to fix this! – Luca Girotti May 06 '22 at 11:19
  • start be removing all `new` and all `*` from the code. Use `std::array` when the size is known and `std::vector` when the size is dynamic. Then use their member `at()` to access elements to find out where you are accessing out of bounds – 463035818_is_not_a_number May 06 '22 at 11:21
  • First of all use `std::vector` instead of your own dynamic allocation (as mentioned by others). Second, you will access up to `501` elements of your array, so initialize it with that many elements (e.g. `std::vector x(501);`) – Some programmer dude May 06 '22 at 11:21
  • https://social.technet.microsoft.com/Forums/windows/en-US/578396fd-29c0-4dd3-b002-5e2e8df1b398/exception-code-0xc0000374?forum=w7itprogeneral - first hit on Google – Paul Sanders May 06 '22 at 11:23
  • 2
    btw you also made the mistake of writing too much code at once. I'd expect the program to crash also when you do not write the results to a file. I guess only a fraction of the code is sufficient to get a crash. Writing lots of code before testing and debugging it results in lots of code to be tested and debugged. If you write little code and only continue to write more after testing and debugging then you need to test and debug only little code – 463035818_is_not_a_number May 06 '22 at 11:25
  • 1
    I'd start reading some good [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) – Jabberwocky May 06 '22 at 11:38
  • 1
    Why `#include "math.h"` instead of `#include `? (Or do you have a project header file named `math.h`, which would be ill-advised.) – Eljay May 06 '22 at 12:00
  • 0xc0000374 says you have a heap corruption [https://james.darpinian.com/decoder/?q=0xc0000374](https://james.darpinian.com/decoder/?q=0xc0000374) – drescherjm May 06 '22 at 12:16

0 Answers0