From 5e28cb037803ff6f5a9f4ae9fc65a609b8f06541 Mon Sep 17 00:00:00 2001 From: linpz Date: Sat, 21 Mar 2026 01:28:52 +0800 Subject: [PATCH] Refactor: add nullptr for uninitialized pointer --- source/source_base/mcd.c | 2 +- source/source_lcao/module_deepks/LCAO_deepks.h | 2 +- source/source_lcao/module_gint/gint_rho.h | 2 +- source/source_lcao/module_gint/gint_rho_gpu.h | 2 +- source/source_lcao/module_gint/gint_tau.h | 2 +- source/source_lcao/module_gint/gint_tau_gpu.h | 2 +- source/source_lcao/module_lr/lr_spectrum.cpp | 6 +++--- .../module_lr/operator_casida/operator_lr_hxc.cpp | 4 ++-- source/source_lcao/module_lr/potentials/xc_kernel.cpp | 2 +- 9 files changed, 12 insertions(+), 12 deletions(-) diff --git a/source/source_base/mcd.c b/source/source_base/mcd.c index cf04c7161a9..3ec039270f5 100644 --- a/source/source_base/mcd.c +++ b/source/source_base/mcd.c @@ -705,7 +705,7 @@ int MCD_sscanf(const char *str,const char *fmt,char*fun,char*file,int line,...) /* scanf etc helper function */ void scan_args(const char *fmt,va_list argptr,char*fun,char*file,int line) { - char **ptr; + char ** ptr = nullptr; void * dummy = nullptr; // clear up the unused warning for(;*fmt;fmt++) { diff --git a/source/source_lcao/module_deepks/LCAO_deepks.h b/source/source_lcao/module_deepks/LCAO_deepks.h index 0fe4815a956..a9c022d153d 100644 --- a/source/source_lcao/module_deepks/LCAO_deepks.h +++ b/source/source_lcao/module_deepks/LCAO_deepks.h @@ -90,7 +90,7 @@ class LCAO_Deepks std::vector pdm; /// dE/dD, autograd from loaded model(E: Ry) - double** gedm; //[tot_Inl][(2l+1)*(2l+1)] + double** gedm = nullptr; //[tot_Inl][(2l+1)*(2l+1)] // functions for hr status: 1. get value; 2. set value; int get_hr_cal() diff --git a/source/source_lcao/module_gint/gint_rho.h b/source/source_lcao/module_gint/gint_rho.h index cb31bf05936..227dd6ab911 100644 --- a/source/source_lcao/module_gint/gint_rho.h +++ b/source/source_lcao/module_gint/gint_rho.h @@ -35,7 +35,7 @@ class Gint_rho : public Gint const bool is_dm_symm_; // output - double **rho_; + double ** rho_ = nullptr; // Intermediate variables std::vector> dm_gint_vec_; diff --git a/source/source_lcao/module_gint/gint_rho_gpu.h b/source/source_lcao/module_gint/gint_rho_gpu.h index d8a8fe6e01d..e411c0ff540 100644 --- a/source/source_lcao/module_gint/gint_rho_gpu.h +++ b/source/source_lcao/module_gint/gint_rho_gpu.h @@ -40,7 +40,7 @@ class Gint_rho_gpu: public Gint const bool is_dm_symm_; // output - double **rho_; + double ** rho_ = nullptr; // Intermediate variables std::vector> dm_gint_vec_; diff --git a/source/source_lcao/module_gint/gint_tau.h b/source/source_lcao/module_gint/gint_tau.h index 07f850b4b50..aa309af2413 100644 --- a/source/source_lcao/module_gint/gint_tau.h +++ b/source/source_lcao/module_gint/gint_tau.h @@ -29,7 +29,7 @@ class Gint_tau : public Gint const int nspin_; // output - double **kin_; + double ** kin_ = nullptr; // Intermediate variables std::vector> dm_gint_vec_; diff --git a/source/source_lcao/module_gint/gint_tau_gpu.h b/source/source_lcao/module_gint/gint_tau_gpu.h index 638892ff133..d3fefc18d2f 100644 --- a/source/source_lcao/module_gint/gint_tau_gpu.h +++ b/source/source_lcao/module_gint/gint_tau_gpu.h @@ -35,7 +35,7 @@ class Gint_tau_gpu : public Gint const int nspin_; // output - double **kin_; + double ** kin_ = nullptr; // Intermediate variables std::vector> dm_gint_vec_; diff --git a/source/source_lcao/module_lr/lr_spectrum.cpp b/source/source_lcao/module_lr/lr_spectrum.cpp index f698541c303..cb2677d1807 100644 --- a/source/source_lcao/module_lr/lr_spectrum.cpp +++ b/source/source_lcao/module_lr/lr_spectrum.cpp @@ -53,7 +53,7 @@ ModuleBase::Vector3 LR::LR_Spectrum::cal_transition_dipole_istat for (int is = 0;is < this->nspin_x;++is) { // 2. transition density - double** rho_trans; + double** rho_trans = nullptr; LR_Util::_allocate_2order_nested_ptr(rho_trans, 1, this->rho_basis.nrxx); ModuleBase::GlobalFunc::ZEROS(rho_trans[0], this->rho_basis.nrxx); ModuleGint::cal_gint_rho({ DM_trans.get_DMR_vector().at(is) }, 1, rho_trans, false); @@ -90,8 +90,8 @@ ModuleBase::Vector3> LR::LR_Spectrum>: for (int is = 0;is < this->nspin_x;++is) { // 2. transition density - double** rho_trans_real; - double** rho_trans_imag; + double** rho_trans_real = nullptr; + double** rho_trans_imag = nullptr; LR_Util::_allocate_2order_nested_ptr(rho_trans_real, 1, this->rho_basis.nrxx); LR_Util::_allocate_2order_nested_ptr(rho_trans_imag, 1, this->rho_basis.nrxx); diff --git a/source/source_lcao/module_lr/operator_casida/operator_lr_hxc.cpp b/source/source_lcao/module_lr/operator_casida/operator_lr_hxc.cpp index 5b008589e1d..5a45cad5602 100644 --- a/source/source_lcao/module_lr/operator_casida/operator_lr_hxc.cpp +++ b/source/source_lcao/module_lr/operator_casida/operator_lr_hxc.cpp @@ -56,7 +56,7 @@ namespace LR // 2. transition electron density // \f[ \tilde{\rho}(r)=\sum_{\mu_j, \mu_b}\tilde{\rho}_{\mu_j,\mu_b}\phi_{\mu_b}(r)\phi_{\mu_j}(r) \f] - double** rho_trans; + double** rho_trans = nullptr; const int& nrxx = this->pot.lock()->nrxx; LR_Util::_allocate_2order_nested_ptr(rho_trans, 1, nrxx); // currently gint_kernel_rho uses PARAM.inp.nspin, it needs refactor ModuleBase::GlobalFunc::ZEROS(rho_trans[0], nrxx); @@ -90,7 +90,7 @@ namespace LR // 2. transition electron density - double** rho_trans; + double** rho_trans = nullptr; const int& nrxx = this->pot.lock()->nrxx; LR_Util::_allocate_2order_nested_ptr(rho_trans, 1, nrxx); // nspin=1 for transition density diff --git a/source/source_lcao/module_lr/potentials/xc_kernel.cpp b/source/source_lcao/module_lr/potentials/xc_kernel.cpp index b28d44a0c6f..ec1bd5ca223 100644 --- a/source/source_lcao/module_lr/potentials/xc_kernel.cpp +++ b/source/source_lcao/module_lr/potentials/xc_kernel.cpp @@ -52,7 +52,7 @@ LR::KernelXC::KernelXC(const ModulePW::PW_Basis& rho_basis, if (lr_init_xc_kernel[0] == "from_charge_file") { assert(lr_init_xc_kernel.size() >= 2); - double** rho_for_fxc; + double** rho_for_fxc = nullptr; LR_Util::_allocate_2order_nested_ptr(rho_for_fxc, nspin, nrxx); double ef = 0.0; int prenspin = 1;