Do you really need to allocate memory here in this line ?
struct bst *temp=malloc(sizeof(struct bst));

there is memory leak here.
temp=node

And the problem statement is when you free(temp). Since temp is still
pointing to the tree parent node where you insert last value.

On Fri, Sep 4, 2015, 9:47 AM Puneet Gautam <puneet.nsi...@gmail.com> wrote:

> HI, I am running the following peice of code on Dev-cpp compiler
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<stddef.h>
> struct bst
> {
>        int info;
>        struct bst *leftnode;
>        struct bst *rightnode;
> };
> void insert(struct bst *node,int val1)
> {
>      struct bst *newnode=malloc(sizeof(struct bst));
>      struct bst *temp=malloc(sizeof(struct bst));
>      newnode->info=val1;
>      temp=node;
>      while(temp)
>      {        printf("\n Inside while loop");
>     *          if(val1<=temp->info)*
>                       if(temp->leftnode!=NULL)
>                       {                    temp=temp->leftnode;
>                                            printf("\n IN if of left");
>                                            }
>                       else
>                                           break;
>               else
>                       if((temp->rightnode)!=NULL)
>                       {                   temp=temp->rightnode;
>                                            printf("\n IN if of right");
>                                            }
>                       else
>                                           break;
>      }
>      if(!temp)
>      {        free(temp);
>               return;
>      }
>       else if(val1<=temp->info)
>       {                temp->leftnode=newnode;
>                        printf("\n Inserted to the left");
>                       }
>       else
>        {               temp->rightnode=newnode;
>                         printf("\n Inserted to the right");
>        }
>       newnode->leftnode=NULL;
>       newnode->rightnode=NULL;
>       free(temp);
> }
> int main()
> {
>     struct bst *start=malloc(sizeof(struct bst));
>    // struct bst *newnode=malloc(sizeof(struct bst));
>     start->info=10;
>     start->leftnode=NULL;
>     start->rightnode=NULL;
>     printf("Data is :%d",start->info);
>     getchar();
>     insert(start,20);
>   * insert(start,5);*
>     getchar();
>     free(start);
>     return 0;
>     }
>
> The compiler reports *Access violation* at the highlighted lines i.e.
> *insert(start,5);*
> But at the run of insert(start,20); , there is no problem as such.
> Please help !!
>
> Regards
> Puneet
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to algogeeks+unsubscr...@googlegroups.com.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to algogeeks+unsubscr...@googlegroups.com.

Reply via email to