diff --git a/src/sundials_matrix_wrapper.cpp b/src/sundials_matrix_wrapper.cpp index cb38b52541..f4f705490d 100644 --- a/src/sundials_matrix_wrapper.cpp +++ b/src/sundials_matrix_wrapper.cpp @@ -13,7 +13,7 @@ SUNMatrixWrapper::SUNMatrixWrapper( sunindextype M, sunindextype N, sunindextype NNZ, int sparsetype, SUNContext sunctx ) - : matrix_(SUNSparseMatrix(M, N, NNZ, sparsetype, sunctx)) + : matrix_(M * N != 0 ? SUNSparseMatrix(M, N, NNZ, sparsetype, sunctx) : nullptr) , id_(SUNMATRIX_SPARSE) , sparsetype_(sparsetype) { @@ -34,7 +34,7 @@ SUNMatrixWrapper::SUNMatrixWrapper( SUNMatrixWrapper::SUNMatrixWrapper( sunindextype M, sunindextype N, SUNContext sunctx_ ) - : matrix_(SUNDenseMatrix(M, N, sunctx_)) + : matrix_(M * N != 0 ? SUNDenseMatrix(M, N, sunctx_) : nullptr) , id_(SUNMATRIX_DENSE) { if (M && N && !matrix_) throw std::bad_alloc(); diff --git a/src/vector.cpp b/src/vector.cpp index d217e2a5f9..3da0eff64e 100644 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -59,7 +59,7 @@ void AmiVector::copy(AmiVector const& other) { void AmiVector::synchroniseNVector() { if (nvec_) N_VDestroy_Serial(nvec_); - nvec_ = N_VMake_Serial( + nvec_ = vec_.empty() ? nullptr : N_VMake_Serial( gsl::narrow(vec_.size()), vec_.data(), sunctx_ ); }