Thread: How to set up Dynamic Storage/ Temporary Buffer?

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

    How to set up Dynamic Storage/ Temporary Buffer?

    So this is my code so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define LUNCHES 5
    #define ARRAY 2
    
    
    int main(void)
    {
        int x;
        struct Food
        {
            char *name;                                                            /* “name” attribute of food */
            int weight, calories;                                                  /* “weight” and “calories” attributes of food */
        }lunch[LUNCHES] = { [0] = {"apple", 4, 100}, [1] = {"salad", 2, 80} };
    
    
        for(x = ARRAY; x <= LUNCHES; ++x)
        {
    
    
            printf("Please input \"food\", weight, calories: ");
            scanf("%d %d", &lunch[x].weight, &lunch[x].calories);
            printf("The %s weighs %doz. and contains %d calories", lunch[x].name, lunch[x].weight, lunch[x].calories);
    
    
        }
        
        return 0;
    }
    I need to set a temporary buffer for food. Not sure how to do that. Also allocating memory.

    I think for allocating:

    Code:
    Food *buff = malloc(sizeof(name))
    Along the lines of it, what do you think?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You should test it and find out.If you do not understand the behavior of your program post back.

    Think of how much space does you need to allocate in your memory to store the struct....

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Quote Originally Posted by std10093 View Post
    You should test it and find out.If you do not understand the behavior of your program post back.

    Think of how much space does you need to allocate in your memory to store the struct....

    Well I tried this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define LUNCHES 5
    #define ARRAY 2
    
    
    int main(void)
    {
        int x;
        struct Food
        {
            char *name;                                                            /* “name” attribute of food */
            int weight, calories;                                                  /* “weight” and “calories” attributes of food */
        }lunch[LUNCHES] = { [0] = {"apple", 4, 100}, [1] = {"salad", 2, 80} };
    
    
        for(x = ARRAY; x <= LUNCHES; ++x)
        {
            char *buff = malloc(sizeof(lunch[x].name));
    
    
            printf("Please input \"food\", weight, calories: ");
            scanf("%s", buff);
            scanf("%d %d", &lunch[x].weight, &lunch[x].calories);
            printf("The %s weighs %doz. and contains %d calories.\n", lunch[x].name, lunch[x].weight, lunch[x].calories);
    
    
    
    
        }
    
    
        return 0;
    }
    Shows up as The NULL weighs input and contains input calories

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    Be careful,
    line 17: you've declared an array of struct of 5 elements, you can access to element with 0≤indexf≤4 , there's an out of bound

    It's more versatile to define a structure tag or structure type, for example a structure type in your case will be

    Code:
    typedef struct{      
          char *name;     
          int weight, calories;
    }Food;
    and now to declare an array you can just write

    Code:
    Food lunch[n_elem];
    or dynamically

    Code:
    Food *lunch=malloc(n_elem*sizeof(Food));
    Last edited by root; 11-12-2012 at 04:20 PM.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    Quote Originally Posted by Radiant View Post
    Well I tried this.

    Code:
            char *buff = malloc(sizeof(lunch[x].name));
            printf("Please input \"food\", weight, calories: ");
            scanf("%s", buff);
            scanf("%d %d", &lunch[x].weight, &lunch[x].calories);
            printf("The %s weighs %doz. and contains %d calories.\n", lunch[x].name, lunch[x].weight, lunch[x].calories);
    
        }
    you have to allocate memory for a string, you dont' need buff variable
    Try
    Code:
    lunch[x].name=malloc(50*sizeof(char)); 
    /*I'm assuming that the food name is shorter than 50 characters, write  "=malloc(50);" is the same because char is always 1, 
    but put it always  and you'll never wrong*/
    Last edited by root; 11-12-2012 at 04:21 PM.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Quote Originally Posted by root View Post
    you have to allocate memory for a string, you dont' need buff variable
    Try
    Code:
    lunch[x].name=malloc(50*sizeof(char)); 
    /*I'm assuming that the food name is shorter than 50 characters, write  "=malloc(50);" is the same because char is always 1, 
    but put it always  and you'll never wrong*/
    So that does work. But the last thing is to check for null. I have the following code

    Code:
    if (lunch[x].name == NULL)        {
                printf("There is an error with the string");
                break;
            }
    I think I have to do something like

    Code:
    char *buff;
    if ((buff = (char*)malloc(25 * sizeof(char))) == NULL)
    {
        printf("There has been an error with the string %s", lunch[x].name)
        break;
        /* how do I exit? */
    }

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    Quote Originally Posted by Radiant View Post
    So that does work. But the last thing is to check for null. I have the following code
    Right observation , if you want to exit if the allocation fails you can call the exit function writing
    Code:
    exit(1);

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Cast of what malloc returns is ooooold style. To be exact it is not recommended.

    When main wants to say that something went wrong , it returns -1 instead of 0 ,as every main ought to return right before terminating...

    Also you might use these
    EXIT_FAILURE - C++ Reference
    EXIT_SUCCESS - C++ Reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class for both dynamic allocated buffer and static buffer
    By TotalTurd in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2012, 09:09 PM
  2. Replies: 1
    Last Post: 11-08-2011, 01:13 PM
  3. Dynamic storage class?
    By Xinok in forum C++ Programming
    Replies: 12
    Last Post: 12-20-2005, 01:14 PM
  4. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM
  5. serial programming, threads and dynamic buffer
    By lectrolux in forum C Programming
    Replies: 1
    Last Post: 05-06-2003, 09:59 AM

Tags for this Thread