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 8262107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:39:10+00:00 2026-06-08T03:39:10+00:00

I have this code : typedef struct node { int data; struct node *left;

  • 0

I have this code :

        typedef struct node
    {
        int data;
        struct node *left;
        struct node *right;
} node;

void Build (node *root , int i)
{
        if (i < 7)
    {
        root = (node *)malloc (sizeof(node));
        root->data = i;
        Build(root->left,2*i+1);
        Build(root->right,2*i+2);
    }
    else
        root = NULL;
}
void Print (node *root)
{
    if (root)
    {
        printf ("%d  ",root->data);
        Print(root->left);
        Print(root->right);
    }
}
void main()
{
    node *tree;

    Build(tree,0);
    Print(tree);
}

two things that I dont understand ,
1. why can’t I pass Build(tree,0) ? it says it’s uninitialized , but why shuold I care if it’s uninitialized ? I’m allocating all the memory needed straight away so it’s gonna be pointing on the new allocated node.

how can I fix this code? thank you!!!

  • 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-06-08T03:39:12+00:00Added an answer on June 8, 2026 at 3:39 am

    Your node * to tree is uninitialized.

    node *tree;
    

    That matters because the code line

    root = (node *)malloc (sizeof(node));
    

    allocates memory to a local copy of root. Once you leave function scope of Build, the copy of root goes out of scope. Memory leak.

    Remember, everything is passed by value in C.

    If you really want Build to allocate the memory, the signature would have to be

    void Build (node **root , int i)
    

    and your code in that method would have to refer to *root instead of root.

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

Sidebar

Related Questions

I have this structure: typedef struct { int data[10]; } small_structure; and this code:
I have a question about this code: typedef struct pop { unsigned long int
I have this (working) CPU code: #define NF 3 int ND; typedef double (*POT)(double
I have this snippet of C code: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct Date {
so usually this typedef makes code cleaner: typedef struct { int x; } X;
For example, I have this structure and code: typedef struct { long long *number;
//binary_tree.h file typedef struct node node; struct node { node():left(0), right(0), value(-1){}; ~node(){if(left) delete
At the moment I have the following code: typedef struct _hexagon { int *vertice[6];
In sample code, I have seen this: typedef enum Ename { Bob, Mary, John}
I have this code <div id=main style=background:#aaaaaa;float:left;height:160px;margin:5px;position:relative;display:block;width:630px;> <div id=1 class=item style=background:#ffaacc;float:left;width:200px;height:150px;margin:5px;position:absolute;left:0px;top:0px;> </div> <div id=2

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.