-
Notifications
You must be signed in to change notification settings - Fork 13
feat: cpp memory improve #160
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
Conversation
prevent memory leaks and improve memory management in ListNode and Node classes
improve memory management in ListNode handling and prevent leaks
update daily problem ID and enhance memory management in solutions
delete root
dev problem 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves memory management across generated solution code by replacing heap-allocated sentinels with stack-based dummies, adding delete
calls to free allocated structures, and introducing destructors in model classes to prevent leaks or double‐frees.
- Use stack‐allocated dummy nodes in list‐building functions to avoid manual cleanup.
- Add explicit
delete
calls after converting solution results to JSON. - Define destructors in model classes (
TreeNodeNext
,NodeRandom
,NodeNeighbors
) for automated cleanup.
Reviewed Changes
Copilot reviewed 27 out of 27 changed files in this pull request and generated 5 comments.
Show a summary per file
File | Description |
---|---|
problems/problems_236/Solution.cpp | Added delete root after JSON conversion |
problems/problems_23/Solution.cpp | Switched dummy list nodes to stack allocation and added free |
problems/problems_2/Solution.cpp | Switched dummy list nodes to stack allocation and added free |
problems/problems_19/Solution.cpp | Added node deletion in removeNthFromEnd and free head |
problems/problems_1656/Solution.cpp | Changed make_shared to make_unique for OrderedStream |
problems/problems_160/Solution.cpp | Added cleanup logic for linked‐list cycle handling |
problems/problems_146/Solution.cpp | Added ~LRUCache destructor and switched to make_unique |
problems/problems_142/Solution.cpp | Added cycle break and head deletion for detectCycle |
problems/problems_141/Solution.cpp | Added cycle break and head deletion for hasCycle |
problems/problems_138/Solution.cpp | Added deletion of original and copied random lists |
problems/problems_1379/Solution.cpp | Added deletion of original and cloned trees |
problems/problems_114/Solution.cpp | Added delete root after flattening |
problems/problems_105/Solution.cpp | Added delete res_ptr after building tree |
daily-problems.json | Updated "daily" value |
cpp/models/TreeNodeNext.h | Added destructor to delete left and right |
cpp/models/NodeRandom.h | Added destructor to delete next |
cpp/models/NodeNeighbors.h | Added destructor to delete neighbor pointers |
cpp/models/NodeNeighbors.cpp | Changed parameter to reference and initialized vector slots |
cpp/models/ListNode.h | Changed functions to use const refs and return by value |
cpp/models/ListNode.cpp | Switched list builders to stack‐allocated dummies |
Comments suppressed due to low confidence (1)
cpp/models/ListNode.h:26
- Changing the signature from non-const to
const&
and returning by raw pointer may break existing callers. Ensure this API change is acceptable or version it accordingly.
ListNode *IntArrayToListNode(const std::vector<int> &arr);
implement graph cloning and memory management functions
Correctly handle memory leaks or free twice in generated code.
Some of cases are commented, let user decide.