Book e.g.: Code printing garbage values instead of actual values

This is a discussion on Book e.g.: Code printing garbage values instead of actual values within the C Programming forums, part of the General Programming Boards category; I have just started learning C/C++ and this is my first post to this forum. This is the e.g which ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    2

    Book e.g.: Code printing garbage values instead of actual values

    I have just started learning C/C++ and this is my first post to this forum.

    This is the e.g which I am practicing from a book and I take no credit for it.

    bst_adt.h

    Code:
    #include <stdbool.h>
    #include <stdlib.h>
    
    typedef struct node{
        void* dataPtr;
        struct node* left;
        struct node* right;
    } NODE;
    
    
    typedef struct{
        int count;
        int (*compare) (void* argu1, void* argu2);
        NODE* root;
    } BST_TREE;
    
    
    BST_TREE* BST_Create (int (*compare) (void* argu1, void* argu2));
    //BST_TREE* BST_Destroy (BST_TREE* tree);
    
    
    bool BST_Insert (BST_TREE* tree, void* dataPtr);
    //bool BST_Delete (BST_TREE* tree, void* dltKey);
    void* BST_Retrieve (BST_TREE* tree, void* keyPtr);
    void  BST_Traverse (BST_TREE* tree, void (*process)(void* dataPtr));
    
    
    static NODE* _insert(BST_TREE* tree, NODE* root, NODE* newPtr);
    static NODE* _delete(BST_TREE* tree, NODE* root, void* dataPtr, bool* success);
    static void* _retrieve(BST_TREE* tree, void* dataPtr, NODE* root);
    static NODE* _insert(BST_TREE* tree, NODE* root, NODE* newPtr);
    static void* _retrieve(BST_TREE* tree, void* dataPtr, NODE* root);
    static void _traverse(NODE* root, void (*process) (void* dataPtr));
    
    
    BST_TREE* BST_Create(int (*compare) (void* argu1, void* argu2)){
        BST_TREE* tree;
    
    
        tree = (BST_TREE*) malloc (sizeof (BST_TREE));
        if (tree){
            tree->root = NULL;
            tree->count = 0;
            tree->compare = compare;
        }
    
    
        return tree;
    }
    
    bool BST_Insert(BST_TREE* tree, void* dataPtr){
        NODE* newPtr;
        newPtr = (NODE*)malloc(sizeof(NODE));
        if (!newPtr)
            return false;
    
    
        newPtr->right = NULL;
        newPtr->left  = NULL;
        newPtr->dataPtr = newPtr;
    
    
        if (tree->count == 0)
             tree->root = newPtr;
        else
            _insert(tree, tree->root, newPtr);
    
    
        (tree->count)++;
    }
    
    
    NODE* _insert (BST_TREE* tree, NODE* root, NODE* newPtr){
        if (!root)
            return newPtr;
    
    
        if (tree->compare(newPtr->dataPtr, root->dataPtr) < 0){
            root->left = _insert(tree, root->left, newPtr);
            return root;
        } else {
            root->right = _insert(tree, root->right, newPtr);
            return root;
        } 
        
        return root;
    }
    
    
    void* BST_Retrieve (BST_TREE* tree, void* keyPtr){
        if (tree->root)
            return _retrieve (tree, keyPtr, tree->root);
        else
            return NULL;
    }
    
    
    void* _retrieve(BST_TREE* tree, void* dataPtr, NODE* root){
        if (root){
            if (tree->compare(dataPtr, root->dataPtr) < 0)
                return _retrieve(tree, dataPtr, root->left);
            else if (tree->compare(dataPtr, root->dataPtr) > 0)
                return _retrieve(tree, dataPtr, root->right);
            else
                return root->dataPtr;
        } else {
            return NULL;
        }
    }
    
    
    void BST_Traverse(BST_TREE* tree, void (*process) (void* dataPtr)){
        _traverse(tree->root, process);
        return;
    }
    
    
    void _traverse(NODE* root, void (*process) (void* dataPtr)){
        if (root){
            _traverse(root->left, process);
            process(root->dataPtr);
            _traverse(root->right, process);
        }
        return;
    }
    bst_integer.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "bst_adt.h"
    
    
    
    int compareInt (void* num1, void* num2);
    void printBST (void* num1);
    
    
    int main(void){
    
    
       BST_TREE* BSTRoot;
       int* dataPtr;
       int dataIn = dataIn + 1;
       printf("Begin BST Demonstration\n");
    
    
       BSTRoot = BST_Create(compareInt);
    
    
       printf("Enter a list of positive integers\n");
       printf("Enter a negative number to stop.\n");
    
    
       do{
           printf("Enter a number:");
           scanf("%d", &dataIn);
           if (dataIn > -1){
              dataPtr = (int*) malloc (sizeof (int));
              if (!dataPtr){
                  printf("Memory overflow");
                  exit (100);
              }
              *dataPtr = dataIn;
              BST_Insert (BSTRoot, dataPtr);
            }
    
    
       } while (dataIn > -1);
    
    
       printf("\nBST contains:\n");
       BST_Traverse(BSTRoot, printBST);
    
    
       printf("\nEND BST Demonstration\n");
       return 0;
    }
    
    
    int compareInt(void* num1, void* num2){
        int key1;
        int key2;
        key1 = *(int*)num1;
        key2 = *(int*)num2;
        if (key1 < key2)
            return -1;
        if (key1 == key2)
            return 0;
        return +1;
    }
    
    
    void printBST(void* num1){
        printf("%4d\n", *(int*)num1);
        return;
    }
    Code when compiled and run prints garbage values instead of actual input values. Not sure what am I missing?

    Code:
    gcc -o bst_int bst_integer.c
    ./bst_int
    Begin BST Demonstration
    Enter a list of positive integers
    Enter a negative number to stop.
    Enter a number:1
    Enter a number:2
    Enter a number:3
    Enter a number:4
    Enter a number:-1


    BST contains:
    817904016
    817904064
    817904112
    817904160


    END BST Demonstration


  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In BST_Insert line 60 should be:
    Code:
    newPtr->dataPtr = dataPtr;  // not newPtr
    Also, BST_Insert is supposed to return a bool at the end (presumably true).

    Compiling with warnings enabled would have alerted you to these problems.
    gcc -Wall -Wextra ...

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    2
    Thanks for catching the bug. It fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. garbage values
    By j.vick in forum C Programming
    Replies: 2
    Last Post: 04-06-2010, 03:31 PM
  2. problem of garbage values
    By aldajlo in forum C Programming
    Replies: 5
    Last Post: 10-02-2004, 04:41 PM
  3. Displaying pointer's actual values in inheritance
    By Poof in forum C++ Programming
    Replies: 14
    Last Post: 07-29-2004, 07:34 AM
  4. garbage values even after initialization
    By kashifk in forum C Programming
    Replies: 3
    Last Post: 03-20-2003, 06:12 PM
  5. Garbage values
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-03-2002, 05:17 PM

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