Please debug this!!!

This is a discussion on Please debug this!!! within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<stdlib.h> int CompareIntegerDefault(void *data1, void *data2) { if(*(int *)data1>*(int *)data2) return (1); else if(*(int *)data1==*(int *)data2) return (0); ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    24

    Please debug this!!!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int CompareIntegerDefault(void *data1, void *data2)
    {
    	
    	if(*(int *)data1>*(int *)data2)
    		return (1);
    	else if(*(int *)data1==*(int *)data2)
    		return (0);
    	else 
    		return (-1);
    }
    typedef struct AVLTreeNode
    {
        void *data ;
        int balfact ;
        
    	struct AVLTreeNode *left ;
        struct AVLTreeNode *right ;
    } AVLTreeNode,*AVLTreeNodePtr;
    
    
    typedef struct ContainerHandle
    {
    	struct AVLTreeNode *AVLRoot;
    	//int h;
    }ContainerHandle_t,*ContainerHandlePtr_t;
    int h;
    void buildtree (AVLTreeNodePtr *root, void *data  )
    {
        struct AVLTreeNode *node1, *node2 ;
    
    
        if ( !root )
        {
            (*root) = ( struct AVLTreeNode * ) malloc ( sizeof ( struct AVLTreeNode ) ) ;
            (*root) -> data = data ;
            (*root) -> left = NULL ;
            (*root) -> right = NULL ;
            (*root) -> balfact = 0 ;
            h=1;
            return ;
        }
      
    }
    
    
    ContainerHandlePtr_t List;
    
    
    int main()
    {
    	int a;
    	List=(ContainerHandlePtr_t)malloc(sizeof(ContainerHandle_t));
    	List->AVLRoot=NULL;
    	a=5;
    	buildtree ( &(List->AVLRoot),&a);	
    	printf("%d",*(int *)(List->AVLRoot->data));
    	return 0;
    }
    This doesnt printf the expected result-5.
    Why???????

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Why didn't you bother to run the code in the debugger?
    If you ever hope to write anything more complicated than "hello world", you're going to get stuck when your code doesn't work as expected, and you can't keep running to every forum you can find to dump your code and ask others to debug it for you.

    Code:
    (gdb) run
    The program being debugged has been started already.
    Start it from the beginning? (y or n) y
    Starting program: /home/user/a.exe
    [New Thread 864.0xe60]
    [New Thread 864.0x7f8]
    
    Breakpoint 1, main () at foo.c:59
    59              buildtree ( &(List->AVLRoot),&a);
    (gdb) s
    buildtree (root=0x20038a40, data=0x22ac64) at foo.c:36
    36          if ( !root )
    (gdb) print root
    $3 = (AVLTreeNodePtr *) 0x20038a40
    (gdb) s
    47      }
    (gdb)
    root isn't NULL, so !root is false, and your function just exits.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using gdb to debug.
    By Kitt3n in forum Linux Programming
    Replies: 2
    Last Post: 12-28-2010, 01:50 PM
  2. How to debug on the fly?
    By sept in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2007, 11:08 AM
  3. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-10-2002, 11:40 PM
  4. debug...
    By jk81 in forum C Programming
    Replies: 2
    Last Post: 10-24-2002, 08:56 PM
  5. gets debug
    By jerryvtts in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 01:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21