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

fix memory leaks and other things in 29/btree.c #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sci-42ver
Copy link
Contributor

@sci-42ver sci-42ver commented Sep 4, 2023

I used the valgrind to fix the memory leak errors in the code. Hope this can help the future readers understand the book.
I also did the following:

  1. Use the explicit initialization char *s = calloc(1024 * sizeof(char),1024); to ensure s not having weird characters which will influence strcat(s, indent);.
  2. remove if (j > 0) to give one whole Btree graph.
  3. add free_node function to ensure no crash due to the memory leak with many iterations.

29/btree.c Outdated
@@ -179,7 +190,11 @@ static char *toString(btree_t *btree) {
}

static char *toStringHelper(node_t *h, int ht, char *indent) {
char *s = malloc(1024 * sizeof(char));
// avoid weird characters and subsequent weird behavior when free by explicit initialization.
char *s = calloc(1024 * sizeof(char),1024);
Copy link
Owner

Choose a reason for hiding this comment

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

Isn't this line should be calloc(sizeof(char), 1024)?. I'll review the rest code later, I need some time to refamiliarize the code and data structure...

Copy link
Contributor Author

@sci-42ver sci-42ver Sep 6, 2023

Choose a reason for hiding this comment

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

Yes, my mistake.

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

Successfully merging this pull request may close these issues.

2 participants