issues with freeing memory/ cross platform compatability
===

I made a tree in C, everything works on my computer when I use Visual C++ except for the free function.

I'm also having lots of issues at run time when I compile on any other platform (gcc on both DOS and UNIX). I have no idea whats wrong there.

Here's all my code:

        /*
     * PhoneBook.h
     * Cop 3530 
     * jlewis
     */
     
    #ifndef _phonebook_h
    #define _phonebook_h
    
    /*
     * PhoneBookP is a pointer to the phonebook struct
     * Define the phonebook struct and the node struct 
     * in your (.c) file
     */
    typedef struct PhoneBookT *PhoneBookP;
    
    /*
     * PhoneBook Interface
     */
    
    /*
     * Returns a pointer to a new empty PhoneBook
     * If memory cannot be allocated, returns a NULL pointer
     */
    PhoneBookP newPhoneBook();
    
    /*
     * Locates and displays the desired entry from the phone book
     * If entry is not found, display an appropriate message
     * Parameters: book, firstname, lastname
     */
    void lookupPhoneBook(PhoneBookP, char *);
    
    /*
     * Creates node with the provided data
     * Inserts the node into the correct position in the tree
     * NOTE: Copy the data into the node
     */
    void insertPhoneBook(PhoneBookP, char *, char *);
    
    /*
     * Removes the node containing the matching names
     * Parameters: phonebook, firstname
     * Returns true if successful, else false
     *
     * NOTE: THIS FUNCTION IS FOR BONUS POINTS
     * YOU CAN SIMPLY INSERT A DUMMY FUNCTION THAT 
     * ALWAYS RETURNS ZERO IF YOU CHOOSE
     */
    int removePhoneBook(PhoneBookP, char *);
    
    /*
     * Dislpays all the entries in the Phone book in order
     * Display one person per line, firstname followed by phone number
     */
    void displayPhoneBook(PhoneBookP);
    
    /*
     * Frees the memory used by each node in the book
     * Frees the memory used by this addressbook
     */
    void freePhoneBook(PhoneBookP);
    
    #endif


Heres the .c file

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "PhoneBook.h"
    typedef struct NodeT
    {
    	struct NodeT *Left;
    	struct NodeT *Right;
    	char *Name;
    	char *Number;
    }* NodeP;
    
    struct PhoneBookT
    {
    	NodeP Root;
    };
    static void Insert(PhoneBookP, NodeP, char* Name,char* Number);
    static void traversePrint(NodeP N);
    static NodeP newNode(PhoneBookP P, NodeP, char *Name,char *Number);
    static int find(NodeP N,char *Name);
    static void traverseFree(NodeP N);
    PhoneBookP newPhoneBook()
    {
    	PhoneBookP P =(PhoneBookP) malloc(sizeof(PhoneBookP));
    	P->Root = NULL;
    	return P;
    }
    void lookupPhoneBook(PhoneBookP P, char * Name)
    {
    	if(find(P->Root, Name));
    	else printf("Error\n");
    }
    static int find(NodeP N,char *Name)
    {
    	if(N)
    	{
    		find(N->Left,Name);
    		if(0 == strcmp(Name,N->Name))
    		{
    			printf("Name: %s\nNumber: %s\n", N->Name, N->Number);
    			return 1;
    		}
    		find(N->Right,Name);
    	}
    	else
    		return 0;
    }
    void insertPhoneBook(PhoneBookP P, char *Name, char *Number)
    {
    	if(P->Root)
    		Insert(P, P->Root, Name,Number);
    	else
    		P->Root = newNode(P,P->Root, Name,Number);
    }
    static void Insert(PhoneBookP P,NodeP N,  char* Name,char* Number)
    {
    	if(N)
    	{
    		if(0 > strcmp(Name,N->Name))
    		{
    			if(N->Left)
    			{
    				Insert(P,N->Left, Name, Number);
    			}
    			else
    			{
    				N->Left = newNode(P,N->Left, Name, Number);
    			}
    		}
    		else
    		{
    			if(N->Right)
    			{
    				Insert(P,N->Right, Name, Number);
    			}
    			else
    			{
    				N->Right = newNode(P,N->Right, Name, Number);
    			}
    		}
    	}
    	else
    		N = newNode(P, N, Name, Number);
    }
    static NodeP newNode(PhoneBookP P,NodeP N,char *Name,char *Number)
    {
    	NodeP New = (NodeP) malloc(sizeof(NodeP));
    	N = New;
    	New->Left = NULL;
    	New->Right = NULL;
    	New->Name = Name;
    	New->Number = Number;
    	return New;
    }
    int removePhoneBook(PhoneBookP P, char * Name)
    {
    	return 0;
    }
    void displayPhoneBook(PhoneBookP P)
    {
    	traversePrint(P->Root);
    }
    static void traversePrint(NodeP N)
    {
    	if(N)
    	{
    		traversePrint(N->Left);
    		printf("Name: %s\n", N->Name);
    		printf("Number: %s\n", N->Number);
    		traversePrint(N->Right);
    	}
    }
    void freePhoneBook(PhoneBookP P)
    {
    	traverseFree(P->Root);
    }
    static void traverseFree(NodeP N)
    {
    	if(N)
    	{
    		traverseFree(N->Left);
    		traverseFree(N->Right);
    		free(N);
    	}
    }

Here's The Tester
I didn't make the remove function, so don't use that.


    /*
     * PhoneBookTest.h
     * Cop 3411 Spr11
     * jlewis
     */
     
    #include "PhoneBook.h"
    #include <stdio.h>
    
    int main()
    {
        PhoneBookP myBook = newPhoneBook();
        printf("Book contains (Joe, Sue, Tom, Vince, Zachary)\n");
        insertPhoneBook(myBook, "Sue", "800-444-4444");
        insertPhoneBook(myBook, "Joe", "555-5555");
        insertPhoneBook(myBook, "Tom", "111-1111");
        insertPhoneBook(myBook, "Zachary", "1-888-888-8888");
        insertPhoneBook(myBook, "Vince", "333-3333");
        displayPhoneBook(myBook);
        printf("\nLooking for Sue ... ");
        lookupPhoneBook(myBook, "Sue");
        printf("Looking for Tom ... ");
        lookupPhoneBook(myBook, "Tom");
        printf("Looking for Zac ... ");
        lookupPhoneBook(myBook, "Zachary");
    /*
        fprintf(stderr, "\nRemoving Joe\n");
        removePhoneBook(myBook, "Joe");
        displayPhoneBook(myBook);
    */
        printf("\nAdding 5 more ... Al, Jason, Thomas, Billy, Tommy\n");
        insertPhoneBook(myBook, "Al", "888-8888");
        insertPhoneBook(myBook, "Jason", "888-8888");
        insertPhoneBook(myBook, "Thomas", "888-8888");
        insertPhoneBook(myBook, "Billy", "888-8888");
        insertPhoneBook(myBook, "Tommy", "888-8888");
        displayPhoneBook(myBook);
       /*
        fprintf(stderr, "\nRemoving Thomas\n");
        //removePhoneBook(myBook, "Thomas");
        displayPhoneBook(myBook);
        fprintf(stderr, "\nRemoving Zachary\n");
        //removePhoneBook(myBook, "Zachary");
        displayPhoneBook(myBook);*/
        freePhoneBook(myBook);
        return 0;
    }

Any help Is much appreciated

Also this is due at 4pm central time.

Thanks!

    
     
