Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nullptr initialization of pointer member variables in class definition #223

Open
dreamer2368 opened this issue Apr 19, 2024 · 0 comments
Open

Comments

@dreamer2368
Copy link
Collaborator

dreamer2368 commented Apr 19, 2024

Currently, mgmol classes do not initialize pointer member variables in their class definitions. For example of MGMol<OrbitalsType>,

template <class OrbitalsType>
class MGmol : public MGmolInterface
{
private:
    std::ostream& os_;

    MPI_Comm comm_;

    XConGrid* xcongrid_;                        // <--

    OrbitalsType* current_orbitals_;            // <--

    AOMMprojector* aomm_;                       // <--

    Ions* ions_;                                // <--

    ....

This is very error-prone and hard to debug, as they have uninitialized pointers which exhibit unexpected behaviors. This practice should be changed as soon as possible, as below:

template <class OrbitalsType>
class MGmol : public MGmolInterface
{
private:
    std::ostream& os_;

    MPI_Comm comm_;

    XConGrid* xcongrid_ = nullptr;

    OrbitalsType* current_orbitals_ = nullptr;

    AOMMprojector* aomm_ = nullptr;

    Ions* ions_ = nullptr;

    ....

This is usually the safest and shortest initialization. An alternative is to define them as nullptr in the constructor (For example, MGmol<OrbitalsType>::MGmol(...)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant