Skip to content

Commit

Permalink
cc: fix returning type of sel_types (deepmodeling#3181)
Browse files Browse the repository at this point in the history
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
(deepmodeling#137). (perhaps because
`DeepTensor.sel_types` is used) `DeepTensor` and `DataChargeModifier`
have different returned types.
  • Loading branch information
njzjz committed Jan 27, 2024
1 parent 2e5333d commit f900f3a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions source/api_cc/include/DataModifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class DipoleChargeModifierBase {
* @brief Get the list of sel types.
* @return The list of sel types.
*/
virtual std::vector<int> sel_types() const = 0;
virtual const std::vector<int>& sel_types() const = 0;
};

/**
Expand Down Expand Up @@ -161,7 +161,7 @@ class DipoleChargeModifier {
* @brief Get the list of sel types.
* @return The list of sel types.
*/
std::vector<int> sel_types() const;
const std::vector<int>& sel_types() const;

private:
bool inited;
Expand Down
2 changes: 1 addition & 1 deletion source/api_cc/include/DataModifierTF.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class DipoleChargeModifierTF : public DipoleChargeModifierBase {
* @brief Get the list of sel types.
* @return The list of sel types.
*/
std::vector<int> sel_types() const {
const std::vector<int>& sel_types() const {
assert(inited);
return sel_type;
};
Expand Down
2 changes: 1 addition & 1 deletion source/api_cc/src/DataModifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ double DipoleChargeModifier::cutoff() const { return dcm->cutoff(); }

int DipoleChargeModifier::numb_types() const { return dcm->numb_types(); }

std::vector<int> DipoleChargeModifier::sel_types() const {
const std::vector<int>& DipoleChargeModifier::sel_types() const {
return dcm->sel_types();
}

0 comments on commit f900f3a

Please sign in to comment.