Need help in C... about malloc....

  • dmx1999
  • Newbie
  • Newbie
  • dmx1999
  • Posts: 12

Post 3+ Months Ago

I really need help in this one... i know it's kindof messy, but please..... :cry:

------------------------------------------------------------------------------------
Position Insert( char *x, Position T)
{

if(T==NULL)
{
T=(Position)malloc( sizeof (struct TreeNode) ); /*error here but why?*/
if (T==NULL)
{
printf("Out of space!!!\n");
return 1;
}
else
{
strcpy(T->s,x);
T->counter = 1;
T->Left = T->Right = NULL;
}
}

else
if(strcmp (x,T->s) < 0)
T->Left = Insert(x, T->Left);
else
if(strcmp (x,T->s) > 0)
T->Right = Insert(x, T->Right);
else
(T->counter)++;

return T;
}

--------------------------------------------------------------------------------------
struct TreeNode
{
char *s;
Position Left;
Position Right;
int counter;
};

--------------------------------------------------------------------------------------

the program should insert a string into a tree... but somehow it gives one of those Windows error and tell me to send the error report to Microsoft...
I track it down and it happens on the "malloc" line after the 2nd insert...
don't really see how the 1st one works and the 2nd one doesn't....

please help... thank you...
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ModernDestroyer
  • Professor
  • Professor
  • User avatar
  • Posts: 794
  • Loc: California

Post 3+ Months Ago

Sorry to bring this to your attention but this post will probably be moved to the programing forum. Once one of the moderators sees it :D
  • dmx1999
  • Newbie
  • Newbie
  • dmx1999
  • Posts: 12

Post 3+ Months Ago

:oops:
wrong forum.... where is the programming forum may i ask
  • ModernDestroyer
  • Professor
  • Professor
  • User avatar
  • Posts: 794
  • Loc: California

Post 3+ Months Ago

it's on the main page :)
  • UNFLUX
  • Genius
  • Genius
  • User avatar
  • Posts: 6376
  • Loc: twitter.com/unflux

Post 3+ Months Ago

Moved to the Programming Forum.
  • billstack
  • Newbie
  • Newbie
  • billstack
  • Posts: 5

Post 3+ Months Ago

Hi dmx1999,

You have not mentioned what Position is defined to be. However, I replaced all instances of Position with TreeNode*, and then made a small modification to make your program work.

It didn't investigate why malloc crashes, I just noticed that you are not allocating space for the new string, T->s. In addition, you were trying to return 1 from the function, instead of T. After these changes were made, the code worked fine.

I added a KillTree function that will deallocate the entire tree. This is just to make sure things are cleaned up properly. It uses a simple depth first search to find the nodes. You should be able to modify this function to make other functions which provide useful tree operations.

Hope this helps.

Code: [ Select ]
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct TreeNode
{
  char *s;
  TreeNode* Left;
  TreeNode* Right;
  int counter;
};

TreeNode* Insert( char *x, TreeNode* T)
{
  if(T == NULL)
  {
    T=(TreeNode*)malloc( sizeof(TreeNode) );
    if (T == NULL)
    {
      printf("Out of space!!!\n");
      return T;
    }
    else
    {
      T->s = (char*)malloc( sizeof(char)*(strlen(x)+1) );
      strcpy(T->s,x);
      T->counter = 1;
      T->Left = T->Right = NULL;
    }
  }
  else if(strcmp(x,T->s) < 0)
    T->Left = Insert(x, T->Left);
  else if(strcmp(x,T->s) > 0)
    T->Right = Insert(x, T->Right);
  else
    (T->counter)++;
  return T;
}

TreeNode* KillTree(TreeNode* T)
{
  if (T != NULL)
  {
    T->Left = KillTree(T->Left);
    T->Right = KillTree(T->Right);

    printf("%s\n", T->s);
    if (T->s != NULL)
      free(T->s);
    free(T);
  }

  return 0;
}

int main(void)
{
  TreeNode* t = NULL;

  t = Insert("b", t);
  t = Insert("a", t);
  t = Insert("c", t);
  t = Insert("d", t);
  t = Insert("ba", t);
  t = Insert("b", t);

  t = KillTree(t);

  return 0;
}
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. struct TreeNode
  5. {
  6.   char *s;
  7.   TreeNode* Left;
  8.   TreeNode* Right;
  9.   int counter;
  10. };
  11. TreeNode* Insert( char *x, TreeNode* T)
  12. {
  13.   if(T == NULL)
  14.   {
  15.     T=(TreeNode*)malloc( sizeof(TreeNode) );
  16.     if (T == NULL)
  17.     {
  18.       printf("Out of space!!!\n");
  19.       return T;
  20.     }
  21.     else
  22.     {
  23.       T->s = (char*)malloc( sizeof(char)*(strlen(x)+1) );
  24.       strcpy(T->s,x);
  25.       T->counter = 1;
  26.       T->Left = T->Right = NULL;
  27.     }
  28.   }
  29.   else if(strcmp(x,T->s) < 0)
  30.     T->Left = Insert(x, T->Left);
  31.   else if(strcmp(x,T->s) > 0)
  32.     T->Right = Insert(x, T->Right);
  33.   else
  34.     (T->counter)++;
  35.   return T;
  36. }
  37. TreeNode* KillTree(TreeNode* T)
  38. {
  39.   if (T != NULL)
  40.   {
  41.     T->Left = KillTree(T->Left);
  42.     T->Right = KillTree(T->Right);
  43.     printf("%s\n", T->s);
  44.     if (T->s != NULL)
  45.       free(T->s);
  46.     free(T);
  47.   }
  48.   return 0;
  49. }
  50. int main(void)
  51. {
  52.   TreeNode* t = NULL;
  53.   t = Insert("b", t);
  54.   t = Insert("a", t);
  55.   t = Insert("c", t);
  56.   t = Insert("d", t);
  57.   t = Insert("ba", t);
  58.   t = Insert("b", t);
  59.   t = KillTree(t);
  60.   return 0;
  61. }
  • billstack
  • Newbie
  • Newbie
  • billstack
  • Posts: 5

Post 3+ Months Ago

Oh yeah, I forgot to mention that using a variable named "T" is not a good idea. With the advent of C++ and templates, T has become a standard (and very popular) type-name placeholder for template parameters. Using "T" in other ways can therefore be a little misleading.

Cheers!

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 87 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.