Re: [algogeeks] Acces violation problem

2015-09-05 Thread shravan kumar C
Hi Puneet,

First of all you have to do the type convertion for the below

struct bst *start=malloc(sizeof(struct bst));
as
struct bst *start= (struct bst *)malloc(sizeof(struct bst));

Reason behind is malloc will return void pointer.

Try to run after making this change at all the places where you are
calling malloc.

Regards,
Shravan

On Fri, Sep 4, 2015 at 9:47 AM, Puneet Gautam  wrote:
> HI, I am running the following peice of code on Dev-cpp compiler
>
> #include
> #include
> #include
> 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.



-- 
Thanks & Regards,
Shravan Kumar Chiluveri

-- 
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.


Re: [algogeeks] Acces violation problem

2015-09-04 Thread pardeep rathee
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  wrote:

> HI, I am running the following peice of code on Dev-cpp compiler
>
> #include
> #include
> #include
> 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.