Thread: structure dynamic memory allocation?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    structure dynamic memory allocation?

    I have an assignment that's basis is about dynamic memory allocation. I'm given two files. One is a file with the list of months from January-December. I have to allocate space to store all of these. I did that.

    The second file has a bunch of dates such as January 17, 2004. I have to store/update a count for each month and year it is in. I wanted to go about this by doing using structures. I read linked lists would be good for this but we don't learn that until next week. Anyways one of the requirements is...

    "The memory for storing the counts should by dynamicallyallocated"
    So if I do use structures I have to dynamically allocate the memory.

    Here's what I have so far. I commented out the struct dates because it wasn't working. But that's to try to help show along the lines of what I'm thinking if this makes sense.

    Thanks for any help.


    Code:
    struct dates
    {
           char months[20];
           int years;
    };
    
    int main(void)
    {
        FILE *fp;
        char buffer[50];
        char **month_and_year = NULL;
        char **list_of_months = NULL;
        int counter1 = 0;
        int counter2 = 0;
        int line_len;
        char *month;
        char *year;
        char *del = ",";
       
        //Opens the first file and stores the months individually in list_of_months 
        if ( (fp = fopen("months.txt", "r" )) == NULL )
        {
        printf("Couldn't open file\n");
        exit(1); 
        }
    
        while(fgets(buffer, sizeof(buffer), fp))
        {
            list_of_months = realloc(list_of_months, (counter1+1) *sizeof(char*) );
            line_len = strlen(buffer);
            list_of_months[counter1] = calloc(line_len+1, sizeof(char));
            strcpy(list_of_months[counter1],buffer);
            counter1++;         
        }   
    fclose( fp );
           
       //Opens the second file and stores the month in month and year in year.                
    
        if ( (fp = fopen("dates.csv", "r" )) == NULL )
        {
        printf("Couldn't open file\n");
        exit(1); 
        }
    
        while(fgets(buffer, sizeof(buffer), fp))
        {
            month_and_year = realloc(month_and_year, (counter2+1) *sizeof(char*) );
            line_len = strlen(buffer);
            month_and_year[counter2] = calloc(line_len+1, sizeof(char));
            strcpy(month_and_year[counter2],buffer);
            month = strtok (month_and_year[counter2], del);
            year = strtok (NULL, del);
            //struct dates *array = realloc( array, (counter2+1)*sizeof(char*));
            counter2++;         
        }      
    fclose( fp );                                
    free(list_of_months);
    free(month_and_year); 
    
     getchar();
     return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mgracecar
    One is a file with the list of months from January-December.
    What exactly is the point of this file when the months from January to December are well known in advance?

    Quote Originally Posted by mgracecar
    The second file has a bunch of dates such as January 17, 2004. I have to store/update a count for each month and year it is in. I wanted to go about this by doing using structures.
    Yes, you could use a structure that contains the month, year and count. As you read the file, you parse each line into the day, month and year. Then, you search the used portion of the array of structures for a matching month and year. If it is found, increment the count, otherwise add the details to the next unused structure. If there are no structures in the array left unused, expand the array first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    "Assignment: Write a program that reads a file of dates and counts the number of dates per month
    per year. The process you will use is as follows:
    • read the file of month names (see below); they should be stored in dynamically allocated
    memory (e.g., array of structures, array of pointers to char, etc.)
    • read the dates file once. As you read the dates file, update the count for the appropriate month
    in the appropriate year. The memory for storing the counts should by dynamically
    allocated.
    • print the overall results as a table"

    There's the main rules of the assignment. So you actually make a good point lol. I didn't even see that. But it's a requirement to read the months and store them. Which I did correctly I think.

    And I understand what you wrote but what exactly would the syntax look like?


    is it anything similar to
    Code:
    struct dates *array = realloc( array, (counter2+1)*sizeof(char*))

    ? I'm understand the how structures work pretty well now I believe. But I don't understand how to allocate memory for structures as I go.



  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mgracecar
    So you actually make a good point lol. I didn't even see that. But it's a requirement to read the months and store them. Which I did correctly I think.
    Clarify with your instructor. It is pointless to just read the file and do nothing with the contents, and in fact it could be an indication that you're doing something wrong.

    For example, maybe the file actually contains months and years, and you're supposed to read it and use it to initialise your array of structures. Then, you don't need to expand the array when you read the second file; you just increment the counts.

    Quote Originally Posted by mgracecar
    is it anything similar to
    Yes, except that array would already exist, and you would use sizeof(*array) not sizeof(char*).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Ok, yea I just sent my instructor an email asking about it. Reread the assignment and don't see any more clarification. The best I can think of is that he gave us the months so that I don't have to sort duplicate months since that's not the point of this assignment. I shall see when he emails me back. Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory Allocation
    By exclusive in forum C Programming
    Replies: 4
    Last Post: 11-10-2011, 07:16 AM
  2. Dynamic Memory Allocation
    By schifers in forum C Programming
    Replies: 12
    Last Post: 05-14-2008, 01:49 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 02:07 AM
  5. dynamic memory allocation - Please help
    By Space_Cowboy in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2002, 05:20 PM