I just tried two piece of code to allocate contiguous memory for a 2D array and use g++ to compile the code. The second piece of code works well but the fisrt one will lead to Segmentation fault (core dumped). What's wrong with the first piece of code and why they are different.
Thanks for your help!
void mem_alloc2D(double **U, unsigned Nx, unsigned Ny) {
U = new double*[Nx];
U[0] = new double[Nx*Ny];
for (unsigned r = 1; r < Nx; ++r)
U[r] = U[0] + r*Ny;
}
double **mem_alloc2D(unsigned Nx, unsigned Ny) {
double **U;
U = new double*[Nx];
U[0] = new double[Nx*Ny];
for (unsigned r = 1; r < Nx; ++r)
U[r] = U[0] + r*Ny;
return U;
}
Aucun commentaire:
Enregistrer un commentaire