Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3604722
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:02:02+00:00 2026-05-18T21:02:02+00:00

Possible Duplicate: Where should I call Free() function? When I used valgrind –leak-check=full –show-reachable=yes

  • 0

Possible Duplicate:
Where should I call Free() function?

When I used
“valgrind –leak-check=full –show-reachable=yes ./Main”
to check the memory leak, I had some errors because of not using Free(), but I don’t know where can I use Free() in this method? I need to allocate the memory for new data, if I call free? It means I will delete the data I have put in ??? This is a bit silly:(


In header file

typedef struct TreeNode * Node;

In this .c file:

struct TreeNode {
char* theData;
Node Left;
Node Right;};

This is a recursive function !

Node InsertString(Node tree, char* data) {

if (tree == NULL) {
    tree = malloc(sizeof (struct TreeNode));//Error
    if (tree == NULL) {
        printf("Out of Space!\n");
    } else {
        tree->theData = malloc(sizeof (char) * strlen(data));//Error
        strcpy(tree->theData, data);
        tree->Left = tree->Right = NULL;


    }
} else {
        if (strcmp(data, tree->theData) < 0) {
            tree->Left = InsertString(head, tree->Left, data);//Error
        } else {
            if (strcmp(data, tree->theData) > 0) {
                tree->Right = InsertString(head, tree->Right, data);//Error
            } else {
                printf("This String already Existed\n");
            }
        }
    } 

}
return tree;}

And this is how I call this function in another .c file

currentTree = InsearchString(currentTree,"String");

Thank you for reading my post. Happy new Year 😛

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:02:03+00:00Added an answer on May 18, 2026 at 9:02 pm

    The exact problem is because of how you declared your function, specifically with the first parameter. The way you pass Node makes the TreeNode instance being passed as an in/out parameter; but the pointer itself is passed by value to the function and is considered an in parameter. Thus, when you attempt to modify the value of the pointer, by calling malloc, the modified value will not be passed back to the function caller. And since the function caller does not have access to the newly allocated memory, it won’t be able to free it, and the memory block will be leaked.

    Valgrind detects this and warns you properly; however, since it doesn’t know the semantic of your code, it can only tell you that you need to delete the memory block allocated in the function.

    The right fix, based on the intended semantic would be to pass Node * or Node & to the function, which would allow you to manipulate the original value of the pointer, instead of the call stack copy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Tasks should show up only if the user has been assigned it
Possible Duplicate: Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter()? Am I responsible for closing the
Possible Duplicate: Timeout on a Python function call I want to implement that when
Possible Duplicate: Jquery: Return the value of a ajax call to caller function? In
Possible Duplicate: Call to a member function on a non-object What is wrong with
Possible Duplicate: How to force child same virtual function call its parent virtual function
Possible Duplicate: How many lines of code should a function/procedure/method have? Out team has
Possible Duplicate: Should I use != or <> for not equal in TSQL? Behavior
Possible Duplicate: Should Usings be inside or outside the namespace sa1200 All using directives
Possible Duplicate: Should C++ eliminate header files? In languages like C# and Java there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.