Thread: Allocating again and again? HELP

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    12

    Allocating again and again? HELP

    Code:
    int foodcalorie (int *num,struct recipe **foodcal)
    {
         char ans='Y';
         system("cls");
         while(ans=='Y' && *foodcal!=NULL)    
          { 
            system("cls");
            if((*foodcal=malloc(sizeof(struct recipe)*(*num)))!=NULL){
             printf("Input name of food\n");
             scanf(" %s",(*foodcal+(*num))->name);
             printf("Input calorie count\n");
             scanf(" %d",&(*foodcal+(*num))->calorie);
             printf("Input specific amount with unit\n");
             scanf(" %f %s",&(*foodcal+(*num))->quantity,(*foodcal+(*num))->unit);    
             printf("Do you wish to add again?  [Y]es or [N]o\n");
             scanf(" %c",&ans);
             if(ans=='Y')
             *num+=1;
             }
          } 
            
             return 1;
         
    }
    I would just like to ask, is it possible to allocate to the same variable
    again after incrementing the multiplier for "malloc"? "realloc" also can't work >_< Checked where the error was and it was after looping once then gives me the "Access Violation Segmentation Fault. Thank you very much
    Last edited by givmefive5; 10-12-2011 at 03:57 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by givmefive5
    I would just like to ask, is it possible to allocate to the same variable
    again after incrementing the multiplier for "malloc"? "realloc" also can't work
    realloc would be a correct approach, though you should assign the result to another pointer first, then if that pointer is not a null pointer, assign it to the original pointer.

    In your case, you are using a pointer to a pointer because you want to change the pointer from the caller and have it reflected in the caller. Although messing around with the extra level of indirection can work, one approach is to simplify with the help of another pointer, e.g.,
    Code:
    struct recipe *temp_foodcal;
    if ((temp_foodcal = realloc(*foodcal, sizeof(*temp_foodcal) * *num)) != NULL) {
        /* ... */
        printf("Input calorie count\n");
        scanf(" %d", &temp_foodcal[*num].calorie);
        /* ... */
        *foodcal = temp_foodcal;
    }
    Notice that I used array notation. It will make your life easier.

    Incidentally, you are expanding the dynamic array on each iteration. This is slow. A better approach is to keep track of the dynamic array's size (number of elements in use) and capacity (number of numbers for which space is allocated), then expand by some factor greater than 1 when size is about to exceed capacity.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. safe gets (allocating)?
    By TriKri in forum C Programming
    Replies: 1
    Last Post: 08-21-2008, 04:02 AM
  2. allocating a vector?
    By epidemic in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2007, 02:23 PM
  3. Allocating ram
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 12-19-2005, 03:58 PM
  4. allocating memory?
    By SamuraiDave in forum C Programming
    Replies: 2
    Last Post: 09-21-2005, 02:45 PM
  5. allocating memory
    By viaxd in forum C Programming
    Replies: 2
    Last Post: 10-12-2003, 07:13 PM