Skip to content

Fix IntArray class and a few math classes with memory management and code style improvements#7112

Open
mohanchen wants to merge 12 commits intodeepmodeling:developfrom
mohanchen:fix_intarray
Open

Fix IntArray class and a few math classes with memory management and code style improvements#7112
mohanchen wants to merge 12 commits intodeepmodeling:developfrom
mohanchen:fix_intarray

Conversation

@mohanchen
Copy link
Collaborator

Changes:

  1. Fix memory management issues:

    • Add exception handling for memory allocation failures
    • Use two-stage memory allocation in create() methods to avoid memory leaks
    • Add null pointer checks in zero_out() and operator=(int)
  2. Improve class design:

    • Implement copy constructor to avoid shallow copy issues
    • Implement move constructor and move assignment operator for better performance
    • Add comprehensive boundary checks for negative indices
  3. Code style improvements:

    • Change braces to be on separate lines
    • Replace tabs with spaces
    • Change Chinese comments to English
  4. Ensure code robustness:

    • Add error messages for memory allocation failures
    • Maintain object validity even when memory allocation fails

This fix ensures the IntArray class is more robust, secure, and efficient, while following consistent code style guidelines.

Reminder

  • Have you linked an issue with this pull request?
  • Have you added adequate unit tests and/or case tests for your pull request?
  • Have you noticed possible changes of behavior below or in the linked issue?
  • Have you explained the changes of codes in core modules of ESolver, HSolver, ElecState, Hamilt, Operator or Psi? (ignore if not applicable)

Linked Issue

Fix #...

Unit Tests and/or Case Tests for my changes

  • A unit test is added for each new feature or bug fix.

What's changed?

  • Example: My changes might affect the performance of the application under certain conditions, and I have tested the impact on various scenarios...

Any changes of core modules? (ignore if not applicable)

  • Example: I have added a new virtual function in the esolver base class in order to ...

Changes:
1. Fix memory management issues:
   - Add exception handling for memory allocation failures
   - Use two-stage memory allocation in create() methods to avoid memory leaks
   - Add null pointer checks in zero_out() and operator=(int)

2. Improve class design:
   - Implement copy constructor to avoid shallow copy issues
   - Implement move constructor and move assignment operator for better performance
   - Add comprehensive boundary checks for negative indices

3. Code style improvements:
   - Change braces to be on separate lines
   - Replace tabs with spaces
   - Change Chinese comments to English

4. Ensure code robustness:
   - Add error messages for memory allocation failures
   - Maintain object validity even when memory allocation fails

This fix ensures the IntArray class is more robust, secure, and efficient, while following consistent code style guidelines.
@mohanchen mohanchen requested a review from ErjieWu March 23, 2026 01:15
@mohanchen mohanchen added the Refactor Refactor ABACUS codes label Mar 23, 2026
abacus_fixer added 2 commits March 23, 2026 10:37
Changes:
1. Add memory allocation failure test using custom operator new override
2. Fix move constructor and move assignment operator to properly reset all member variables
3. Improve test class initialization by moving member initialization to constructor
4. Ensure all tests pass with the new functionality

This commit enhances the robustness of the IntArray class by ensuring it properly handles memory allocation failures and correctly implements move semantics.
assert(ptr != nullptr);zero_out();
size = d1 * d2 * d3;assert(size>0);
dim = 3;
bound1 = d1;bound2 = d2;bound3 = d3;bound4 = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems bound4 is useless in 3d IntArray, bound3 and bound4 are useless in 2d IntArray.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this document is "basic_types_class.md", but the content is entirely focused on IntArray. I noticed that "index.rst" is also created for developers to read. Should the file name be adjusted, or should other contents be added to this document later?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the document, the document is intended for tool classes

@mohanchen mohanchen changed the title Fix IntArray class with memory management and code style improvements Fix IntArray class and a few math classes with memory management and code style improvements Mar 24, 2026
Copy link
Collaborator

@ErjieWu ErjieWu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

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

Labels

Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants