Thread: Help!!! (Malloc for beginners )

  1. #1
    Registered User charlyboo's Avatar
    Join Date
    Sep 2012
    Location
    Beersheba, Israel, Israel
    Posts
    3

    Help!!! (Malloc for beginners )

    hello everyone!
    i have an assigment and i dont understand how to write the malloc section.

    in my input i get names and team names.
    there's a struc of "info" that points to the struct that contains the name data.
    i need to use malloc to save all the data and then reuse it.

    how do i do that?

    Thanks!!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Charlyboo!

    What I'd do is start with the inner struct "name_data" prototype first. Then do the outer struct "info". Do these, above the main() function.

    Then create the structs in main(), and do your malloc'ing there. Use sizeof(struct_name), for the malloc(). Don't add up the bytes yourself, because there may well be some padding in there, from the compiler. Pass any struct or array of structs, to any other functions as needed.

    You need to include stdlib.h, and you should not be casting the return value from malloc() - it will automatically adjust to the right size.

    If you post up some code you're having questions or problems with, and ask specific questions, we can be much for helpful. If you're getting any warnings or errors from the compiler, include those, of course.

  3. #3
    Registered User charlyboo's Avatar
    Join Date
    Sep 2012
    Location
    Beersheba, Israel, Israel
    Posts
    3
    wow! thanks for responding quickly!


    so i copied my skeleton here, the parts i'm talking about are bold.

    teamName, first_speaker_name, second_speaker_name - they all get the input from the user. then when i use malloc it has memory but not the input.
    how do i copy my string to where i located the memory?
    and how do i point the struct to it and when do i print FREE?

    thanks!




    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #define MAX_LENGTH 100
    
    
    typedef struct team_info{
        char* teamName;
        int points;
        struct speaker_info* first_speaker;
        struct speaker_info* second_speaker;
    }teamInfo;
    
    
    
    
    typedef struct speaker_info{
        char* speakerName;
        int speakerPoints;
        struct team_info* team;
    }speakerInfo;
    
    
    typedef struct teams_list{
        teamInfo* info;
        struct teams_list* next;
    }teamsList;
    
    
    typedef struct speakers_list{
        speakerInfo* info;
        struct speakers_list* next;
    }speakersList;
    
    
    typedef struct position_{
        teamInfo* team;
        int rank;
        int first_speaker_points;
        int second_speaker_points;
    }position;
    
    
    typedef struct room_info{
        position Opening_Government;
        position Opening_Opposition;
        position Closing_Government;
        position Closing_Opposition;
    }roomInfo;
    
    
    typedef struct rooms_list{
        roomInfo* room;
        struct rooms_list* next;
    }roomsList;
    
    
    void register_team(teamsList** teams,speakersList** speakers,char* teamName,char* first_speaker_name,char* second_speaker_name);
    void print_teams(teamsList* teams);
    void print_speakers(speakersList* speakers);
    roomsList* make_draw(teamsList* teams);
    void print_rooms(roomsList* rooms);
    void update_chart(teamsList* teams, speakersList* speakers, roomsList* rooms);
    void free_allocated_memory(teamsList* teams, speakersList* speakers);
    void free_rooms_list(roomsList* rooms);
    
    
    
    
    int main(){
        int input,room_number;
        teamsList* teams=NULL;
        speakersList* speakers=NULL;
        roomsList* rooms=NULL,*run;
        char teamName[MAX_LENGTH],first_speaker[MAX_LENGTH],second_speaker[MAX_LENGTH];
    
    
        do{
            printf("Menu\n"
                    "**********************\n"
                    "1.Add a team\n"
                    "2.Print teams rank\n"
                    "3.Print speakers rank\n"
                    "4.Make draw\n"
                    "5.Enter results\n"
                    "6.Exit\n"
                    "Enter your choice:\n");
            scanf("%d%*c",&input);
            if(input==1){
                printf("Enter team name:\n");
                gets(teamName);
                printf("Enter the first speaker's name:\n");
                gets(first_speaker);
                printf("Enter the second speaker's name:\n");
                gets(second_speaker);
                register_team(&teams,&speakers,teamName,first_speaker,second_speaker);        
            }
            if(input==2)
                print_teams(teams);
            if(input==3)
                print_speakers(speakers);
            if(input==4){
                if(!rooms)
                    rooms=make_draw(teams);
                print_rooms(rooms);
            }
            if(input==5){
                for(run=rooms,room_number=1;run;run=run->next,room_number++){
                    printf("Room no.%d Opening Government (Rank  First Speaker Points  Second Speaker Points)\n",room_number);
                    scanf("%d%d%d",&run->room->Opening_Government.rank,&run->room->Opening_Government.first_speaker_points,&run->room->Opening_Government.second_speaker_points);
                    printf("Room no.%d Opening Opposition (Rank  First Speaker Points  Second Speaker Points)\n",room_number);
                    scanf("%d%d%d",&run->room->Opening_Opposition.rank,&run->room->Opening_Opposition.first_speaker_points,&run->room->Opening_Opposition.second_speaker_points);
                    printf("Room no.%d Closing Government (Rank  First Speaker Points  Second Speaker Points)\n",room_number);
                    scanf("%d%d%d",&run->room->Closing_Government.rank,&run->room->Closing_Government.first_speaker_points,&run->room->Closing_Government.second_speaker_points);
                    printf("Room no.%d Closing Opposition (Rank  First Speaker Points  Second Speaker Points)\n",room_number);
                    scanf("%d%d%d",&run->room->Closing_Opposition.rank,&run->room->Closing_Opposition.first_speaker_points,&run->room->Closing_Opposition.second_speaker_points);
                }
                update_chart(teams,speakers,rooms);
                free_rooms_list(rooms);
                rooms=NULL;
            }
        
        printf("\n\n");
        }
        while(input!=6);
    
    
    free_allocated_memory(teams,speakers);
    free_rooms_list(rooms);
    return 1;
    }
    
    
    
    void register_team(teamsList** teams,speakersList** speakers,char* teamName,char* first_speaker_name,char* second_speaker_name){
    
        teamName=(char*)malloc(sizeof(char)*(strlen(teamName)+1));
    
    
        first_speaker_name=(char*)malloc(sizeof(char)*(strlen( first_speaker_name)+1));
    
    
            second_speaker_name=(char*)malloc(sizeof(char)*(strlen(second_speaker_name)+1));    
    
    }
    
    
    void print_teams(teamsList* teams){
        
    }
    
    
    void print_speakers(speakersList* speakers){
        
    }
    
    
    
    
    void free_allocated_memory(teamsList* teams, speakersList* speakers){
        
    }
    
    
    
    
    roomsList* make_draw(teamsList* teams){
        
    }
    
    
    void print_rooms(roomsList* rooms){
    
    
        
    }
    
    
    void free_rooms_list(roomsList* rooms){
        
        
    }
    
    
    void update_chart(teamsList* teams, speakersList* speakers, roomsList* rooms){
        
    }
    Last edited by charlyboo; 09-19-2012 at 02:16 AM.

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

    From your code
    Code:
    char teamName[MAX_LENGTH],first_speaker[MAX_LENGTH],second_speaker[MAX_LENGTH];
    Here you did not declare pointers,but arrays!What's the difference in that about memory?Well if you declare a pointer(as you did with teamlist(something like that)) then you allocate memory for a pointer only(actually for an int because pointer has a value that is usually an address ) and it points to nothing(NULL).So where the pointer is pointing right after it's declaration pointerName=NULL; is nothing.In this case you have to use malloc to allocate memory and then use the memory.Once you do this,then assign to this block of memory that malloc returned to you,your data.
    When declaring an array ,as in the code that i copied pasted,then you allocate memory automatically to fit MAX_LENGHT of primitive type char.So actually you have allocated memory,then writting to it by using gets (DANGEROUS),and then you call the function that uses malloc.So what happens is that you probably overwrite the memory you wrote or -most likely - give you another block of memory(in this case the first memory of block is written but left there?memory leak possible.)So what i actually suggest you is not to use malloc when using arrays.

    If you play with pointers,first allocate memory with malloc,then write to it and then free it like this
    Code:
    free(pointerName);
    As for the gets use fgets instead because the first may lead overflow your buffer!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Too late for more, (bed time), but a few suggestions:

    1) don't cast the pointer returned by malloc().

    2) You create a teamName array in main. Then you malloc memory for it in register_team.

    You can't change the address of a static array like teamName. teamName is not a constant pointer, but it is very close to being a constant pointer to the base of the array (or the first element of the array, if you want to think of it that way).

    3) How many teams do you have, at most?

    4) You should be seeing some serious warnings and errors, yes?
    If not, be sure they are turned on and or up.

  6. #6
    Registered User charlyboo's Avatar
    Join Date
    Sep 2012
    Location
    Beersheba, Israel, Israel
    Posts
    3
    yeah it's late for you guys.. here it's 1pm.
    anyway i think i'm starting to understand my mistake.
    3-- teams are a multiply of 4. can be 4, 8,12,16 and so on.
    4-- i have warnings but it's ok. we're allowed to and not supposed to use fgets.
    basicly from what i understand i need to create a list of struct that has "info" and "next" in it. (calling it team list).
    then the info will point to the "team info" struct that has all the data.

    question is how i create the team list struct in my function?

    thanks!!!!

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You want to enter the data into a temp static variable and then assign it/strcpy() it over to the new node's struct member, right?

    What you get from malloc, if it's making memory for another node in the list, is a pointer to a node, not a pointer to char (as you have), but you shouldn't be casting that pointer, anyway. C does that for you.

    If you have nested structs, and the inner struct has pointers to (something, usually a char array), then in addition to allocating memory for the outer node, you have to allocate memory also for the inner struct member's dynamic array, before you try and put data into them.

    OK, I think I get the problem with your creation of the node, in the other function (not main). In main() use sizeof(theStruct'sName), and assign that value to a variable, (like sizeOf). Pass sizeOf, to the function that is mallocing the memory, as a parameter, and use it for the size you need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners Help
    By scott1990 in forum C Programming
    Replies: 6
    Last Post: 05-20-2010, 06:43 PM
  2. Beginners, C++ or C#?
    By Zeusbwr in forum C++ Programming
    Replies: 30
    Last Post: 10-10-2004, 10:54 PM
  3. GUI for beginners
    By Micko in forum C++ Programming
    Replies: 6
    Last Post: 06-24-2004, 05:40 AM
  4. Beginners FAQ
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-14-2004, 07:32 PM
  5. beginners help
    By stormswift in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 03:44 PM

Tags for this Thread