Thread: Overlapping memory addresses in a struct :/

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Overlapping memory addresses in a struct :/

    I have a binary tree and a struct with three tree pointers and a void pointer for the key of the node.When trying to insert a node,even though the key gets the value that it is expected,then when i set the tree pointers to NULL,the left pointer seems to overwrite the void pointer and as a result i get a key equal to zero always!

    The insertion function
    Code:
    Treeptr addtree(Treeptr p, void* v ,int sizeOfType,int (*compare)
         ( void* arg1, void* arg2))
                                         /* Insert value v into tree p */
    {
      Treeptr y = NULL;
      Treeptr x = p;
                                        /* Allocate space for new node */
      Treeptr z = malloc(sizeof(Treeptr));
      printf("The value inside the function %d\n",*(int*)v);
      z->value = malloc(sizeOfType);      /* Allocate memory for value */
      memcpy( z->value , v , sizeOfType );               /* Copy value */
      printf("The value of node BEFORE setting the pointers to null %d\n",*(int*)z->value);
      z->left = NULL;
      z->right = NULL;
      z->parent = NULL;
      printf("The value of node AFTER setting the pointers to null %d\n",*(int*)z->value);
      while(x != NULL) {
         y = x;
         if( compare(z->value,x->value) == -1 )
            x = x->left;
         else
            x = x->right;
      }
      z->parent = y;
      if(y == NULL) {
         p = z;
      }
      else if( compare(z->value,y->value) == -1 ) {
         y->left = z;
      }
      else {
         y->right = z;
      }
      return p;
    }
    My struct
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct tnode *Treeptr;
    
    typedef struct tnode {
      Treeptr left;
      Treeptr parent;
      Treeptr right;
      void* value;
    } Treenode;
    
    Treeptr addtree(Treeptr p, void* v ,int sizeOfType,int (*compare)
         ( void* arg1, void* arg2));
    void nodesGeneric(Treeptr,void(*)(void*));
    Treeptr minnode(Treeptr p);
    Treeptr successor(Treeptr x);
    Treeptr treesearch(Treeptr p, void* v);
    Treeptr deletenode(Treeptr p ,void* v);
    The main
    Code:
    int main(void)
    { Treeptr p;
      char answer;
      void* v;
      int value,n,i;
      p = NULL;                               /* Initialize binary tree */
    
      printf("Please insert number of nodes of the tree\n");
      scanf("%d",&n);
      if(n < 1)
      {
        printf("\nValue less than one inserted.Exiting...\n");
        return -1;
      }
      printf("Please insert nodes of tree.\n");
      for( i = 0 ; i < n ; i++ ) {
        scanf("%d", &value);                  /* Read values from input */
                    v = &value;                /* and insert them into the tree */
        p = addtree(p, v , sizeof(int) , int_sorter);
      }
      .....
      return 0;
    }
    I tried to play around with the position of the pointers,where i set them to null and where they are placed inside the struct,but again i had the same problem.Actually once i got a segmentation fault...What is the problem?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Treeptr z = malloc(sizeof(Treeptr));
    If you ever wanted a reason for not typdef'ing pointer types, then this is it!

    You allocated space for a pointer, NOT space for what it points at.
    So yes, welcome to

    0, segfault house,
    lost way,
    overrun city,
    horrible state,
    00000

    Try something like
    Treeptr z = malloc(sizeof(*z));
    and you never have to play "guess the type" again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes i understand what you are saying.Thank you very much!

    PS - i really like your picture
    ( I told him to take a look at your picture)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with opaque programming with struct and addresses.
    By DarkAngel4ever in forum C Programming
    Replies: 11
    Last Post: 03-30-2011, 08:19 PM
  2. Replies: 12
    Last Post: 04-11-2010, 07:14 AM
  3. Working with memory addresses
    By Fujy in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2010, 09:31 AM
  4. Overlapping Memory
    By hammer1234 in forum C Programming
    Replies: 1
    Last Post: 04-05-2006, 03:05 AM
  5. Memory Addresses
    By Breetai in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2002, 08:10 AM