From f900f3a1320ac42369789288d8b9b56fa40ee42f Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 27 Jan 2024 05:12:41 -0500 Subject: [PATCH] cc: fix returning type of sel_types (#3181) Fix the following compiler warning: ``` /home/runner/work/deepmd-kit/deepmd-kit/source/api_c/src/c_api.cc:1336:17: warning: returning address of local temporary object [-Wreturn-stack-address] return (int*)&(dcm->dcm.sel_types())[0]; ^~~~~~~~~~~~~~~~~~~~~~ 1 warning generated. ``` by returning the reference of `sel_type`. `DataChargeModifier.sel_types` is not used anywhere, even in the test, so we don't have a chance to determine if there is a possible segfault, and this warning has no actual impact. It seems `DeepTensor` has returned a reference since the beginning (https://github.com/deepmodeling/deepmd-kit/pull/137). (perhaps because `DeepTensor.sel_types` is used) `DeepTensor` and `DataChargeModifier` have different returned types. --- source/api_cc/include/DataModifier.h | 4 ++-- source/api_cc/include/DataModifierTF.h | 2 +- source/api_cc/src/DataModifier.cc | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/api_cc/include/DataModifier.h b/source/api_cc/include/DataModifier.h index 1e611a3930..6d443d9b9c 100644 --- a/source/api_cc/include/DataModifier.h +++ b/source/api_cc/include/DataModifier.h @@ -84,7 +84,7 @@ class DipoleChargeModifierBase { * @brief Get the list of sel types. * @return The list of sel types. */ - virtual std::vector sel_types() const = 0; + virtual const std::vector& sel_types() const = 0; }; /** @@ -161,7 +161,7 @@ class DipoleChargeModifier { * @brief Get the list of sel types. * @return The list of sel types. */ - std::vector sel_types() const; + const std::vector& sel_types() const; private: bool inited; diff --git a/source/api_cc/include/DataModifierTF.h b/source/api_cc/include/DataModifierTF.h index c0021c6947..cd1d696c3c 100644 --- a/source/api_cc/include/DataModifierTF.h +++ b/source/api_cc/include/DataModifierTF.h @@ -84,7 +84,7 @@ class DipoleChargeModifierTF : public DipoleChargeModifierBase { * @brief Get the list of sel types. * @return The list of sel types. */ - std::vector sel_types() const { + const std::vector& sel_types() const { assert(inited); return sel_type; }; diff --git a/source/api_cc/src/DataModifier.cc b/source/api_cc/src/DataModifier.cc index 38d1fc879a..bac2e13da5 100644 --- a/source/api_cc/src/DataModifier.cc +++ b/source/api_cc/src/DataModifier.cc @@ -92,6 +92,6 @@ double DipoleChargeModifier::cutoff() const { return dcm->cutoff(); } int DipoleChargeModifier::numb_types() const { return dcm->numb_types(); } -std::vector DipoleChargeModifier::sel_types() const { +const std::vector& DipoleChargeModifier::sel_types() const { return dcm->sel_types(); }