Like Tree1Likes
  • 1 Post By AndiPersti

Dynamic Memory Allocation

This is a discussion on Dynamic Memory Allocation within the C Programming forums, part of the General Programming Boards category; I am really close to finishing this project. I just have to fix this final problem. Any help is greatly ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    14

    Dynamic Memory Allocation

    I am really close to finishing this project. I just have to fix this final problem. Any help is greatly appreciated. Try compiling this code and adding 2 friends then do option 4 to display all contacts. For some reason a hyphen - will be in place of one of the first names. I can't figure out what is causing this. Any thoughts are greatly appreciated. Thanks!

    I think I have narrowed the problem down to get get or set for first name.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_NAME_LENGTH 40
    #define CONTACTS 50
    
    
    typedef struct {
            char cfirstName[MAX_NAME_LENGTH];
            char clastName[MAX_NAME_LENGTH];
            char chomeNumber[MAX_NAME_LENGTH];
            char ccellNumber[MAX_NAME_LENGTH];
            char * firstName;
            char * lastName;
            char * homeNumber;   
            char * cellNumber;
    } entry;
    
    
    void menu();
    void addfriend(entry * pb, int * ptrX, char buffer[], int *);
    void showfriend(entry * pb, int * ptrX, char buffer[]);
    void showpb(entry * pb, int * ptrX, char buffer[], int *);
    void set_firstName(entry * pb, int * ptrX, char buffer[], int *);
    void get_firstName(entry * pb, int * ptrX, int);
    void set_lastName(entry * pb, int * ptrX, char buffer[], int *);
    void get_lastName(entry * pb, int * ptrX, int);
    void set_homeNumber(entry * pb, int * ptrX, char buffer[], int *);
    void get_homeNumber(entry * pb, int * ptrX, int);
    void set_cellNumber(entry * pb, int * ptrX, char buffer[], int *);
    void get_cellNumber(entry * pb, int * ptrX, int);
    
    
    main()
    {
        entry * pb;
        pb = malloc(CONTACTS * sizeof(*pb));
        int option = 0;
        int length = 0;
        int X = 0;
        int * ptrX;
        ptrX = &X;
        char buffer[100];
        //Variables
            
            do{    
            menu();
            scanf("%d", &option);
          
            if(option == 1){addfriend(pb, ptrX, buffer, &length);}
            if(option == 4){showpb(pb, ptrX, buffer, &length);}
            if(option == 5){
                free(pb);
                printf("Good Bye!");
                exit(0);
            }
            }
            while(1);
            //Functions for phonebook
            system("PAUSE");
    }
    
    
    //Menu
    void menu(){    
        printf("\nPhone Book Application\n");
        printf("1) Add friend\n");
        printf("2) Delete friend\n");
        printf("3) Show a friend\n");
        printf("4) Show phone book\n");
        printf("5) Quit\n\n\n");
        printf("What would you like to do?:");
    }     
    
    
    void set_firstName(entry * pb, int * ptrX, char buffer[], int * length){    
        scanf("%s", buffer);
        * length = strlen(buffer);
        strcpy(pb[*ptrX].cfirstName, buffer);
    }
    
    
    void get_firstName(entry * pb, int * ptrX, int i){
        printf("\nFirst name: %s\n", pb[i].cfirstName);
    }
    
    
    void set_lastName(entry * pb, int * ptrX, char buffer[], int * length){    
        scanf("%s", buffer);
        * length = strlen(buffer);
        strcpy(pb[*ptrX].clastName, buffer);
    }
    
    
    void get_lastName(entry * pb, int * ptrX, int i){
        printf("Last name: %s\n", pb[i].clastName);
    }
    
    
    void set_homeNumber(entry * pb, int * ptrX, char buffer[], int * length){
        scanf("%s", buffer);
        * length = strlen(buffer);
        strcpy(pb[*ptrX].chomeNumber, buffer);
    }
    
    
    void get_homeNumber(entry * pb, int * ptrX, int i){
        printf("Home number: %s\n", pb[i].chomeNumber);
    }
    
    
    void set_cellNumber(entry * pb, int * ptrX, char buffer[], int * length){
        scanf("%s", buffer);
        * length = strlen(buffer);
        strcpy(pb[*ptrX].ccellNumber, buffer);
    }
    
    
    void get_cellNumber(entry * pb, int * ptrX, int i){
        printf("Cell number: %s\n", pb[i].ccellNumber);
    }
    
    
    //Addfriend Function
    void addfriend(entry * pb, int * ptrX, char buffer[], int * length){ 
        int joseph = *length;
        printf("%d", joseph);    
    
    
        printf("First name: ");
        set_firstName(pb, ptrX, buffer, length);                                   
        realloc(pb, *ptrX * sizeof(* length));
        printf("Last name: ");
        set_lastName(pb, ptrX, buffer, length);
        realloc(pb, *ptrX * sizeof(* length));
        printf("Phone number (home): ");
        set_homeNumber(pb, ptrX, buffer, length);
        realloc(pb, *ptrX * sizeof(* length));
        printf("Phone number (cell): ");
        set_cellNumber(pb, ptrX, buffer, length);
        realloc(pb, *ptrX * sizeof(* length));
       
        printf("....Record added to the phone book\n\n\n\n\n");
    
    
       strcpy(buffer, "");
       *ptrX += 1;   
    }
    
    
    //Show phonebook Function
    void showpb(entry * pb, int * ptrX, char buffer[], int * length){
               int i = 0;
    
    
               for(i= 0; i < * ptrX; i++){
                            get_firstName(pb, ptrX, i);
                            printf("%s", pb[i].cfirstName);
                            get_lastName(pb, ptrX, i);
                            get_homeNumber(pb, ptrX, i);
                            get_cellNumber(pb, ptrX, i);
               }
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    14
    I've narrowed the error down to the get_firstName.

    It has to do with the pb[i].cfirstName

    nvm solved....
    Last edited by tictac232434; 11-19-2012 at 11:18 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by tictac232434 View Post
    I am really close to finishing this project.
    I'm afraid I have to spoil the party.

    Quote Originally Posted by tictac232434 View Post
    Try compiling this code and adding 2 friends then do option 4 to display all contacts.
    Have you ever looked at the compiler warnings?
    I wasn't even able to add a person because the program crashes after entering the last name:
    Code:
    $ ./foo
    
    Phone Book Application
    1) Add friend
    2) Delete friend
    3) Show a friend
    4) Show phone book
    5) Quit
    
    
    What would you like to do?:1
    0First name: Fred
    Last name: Flinstone
    *** glibc detected *** ./foo: double free or corruption (top): 0x08904008 ***
    ...
    Aborted
    Why do you have all this realloc() calls in addfriend()?
    Code:
    void addfriend(entry * pb, int * ptrX, char buffer[], int * length){ 
        printf("First name: ");
        set_firstName(pb, ptrX, buffer, length);                                   
        realloc(pb, *ptrX * sizeof(* length));
        printf("Last name: ");
        set_lastName(pb, ptrX, buffer, length);
        realloc(pb, *ptrX * sizeof(* length));
        printf("Phone number (home): ");
        set_homeNumber(pb, ptrX, buffer, length);
        realloc(pb, *ptrX * sizeof(* length));
        printf("Phone number (cell): ");
        set_cellNumber(pb, ptrX, buffer, length);
        realloc(pb, *ptrX * sizeof(* length));
    You also have several unused parameters in your functions and parameters which really should be local variables.

    Bye, Andreas
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By JamesD in forum C Programming
    Replies: 10
    Last Post: 03-12-2011, 12:08 AM
  2. what is dynamic memory allocation
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 06-08-2009, 09:11 AM
  3. Dynamic Memory Allocation
    By BoneXXX in forum C Programming
    Replies: 11
    Last Post: 03-26-2007, 02:43 AM
  4. Help with dynamic memory allocation
    By malooch in forum C Programming
    Replies: 2
    Last Post: 12-13-2006, 01:26 PM
  5. dynamic memory allocation
    By mag_chan in forum C Programming
    Replies: 13
    Last Post: 10-21-2005, 08:54 AM

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