Thread: malloc and realloc

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    malloc and realloc

    Hi everyone,

    I was wondering if someone could go over my code to check to see if I did this right. Basically what this program is supposed to do is allocate enough memory for 10 random characters and assigns them to memory. Next it reallocates the memory to hold 15 elements and adds 5 more random characters. It then writes all of the characters to disk. To my knowledge I covered all of this, but my gut tells me I went about it the wrong way.

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>

    #define Max 10

    int i, randNum, NewMax;


    int main(void)
    {
    char array[] = {"abcdefghijklmnopqrstuvwxyz0123456789"};
    char *ptr_array = array;
    char RandChar;

    ptr_array = malloc(10); //Allocates 150 bytes of memory
    ptr_array = realloc ( ptr_array, 15 );
    NewMax = Max + 5;

    srand(time(NULL));

    for (i=0; i<NewMax; i++)
    {

    RandChar = array[rand() % (sizeof(array)-1)];

    printf("%c\n", RandChar);

    }
    free(ptr_array);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >To my knowledge I covered all of this
    More or less. I believe the requirement was to allocate 10 spaces and fill them, then reallocate to 15 spaces without losing the first 10. You should also be more careful with return values and error checking when it comes to memory allocation:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define Max 10
    
    int main(void)
    {
      int i, NewMax;
      char array[] = "abcdefghijklmnopqrstuvwxyz0123456789";
      char *ptr_array;
      
      /* Allocates 10 bytes of memory */
      ptr_array = malloc(Max * sizeof *ptr_array);
      
      /* Check that it worked */
      if (ptr_array != NULL)
      {
        char *hold;
        
        srand((unsigned)time(NULL));
        
        /* Fill the 10 spots */
        for (i=0; i<Max; i++)
          ptr_array[i] = array[rand() % (sizeof(array)-1)];
    
        /* Print all 10 characters */
        for (i=0; i<Max; i++)
          printf("%c", ptr_array[i]);
    
        printf("\n");
        
        /* Reallocated to 15, save in a second pointer in case of failure */
        NewMax = Max + 5;
        hold = realloc ( ptr_array, NewMax * sizeof *ptr_array );
        
        /* Check that it worked */
        if (hold != NULL)
        {
          /* Save the realloc'd memory to the original pointer */
          ptr_array = hold;
          
          /* Fill the next 5 spots */
          for (i=Max; i<NewMax; i++)
            ptr_array[i] = array[rand() % (sizeof(array)-1)];
          
          /* Print all 15 characters */
          for (i=0; i<NewMax; i++)
            printf("%c", ptr_array[i]);
    
          printf("\n");
        }
        
        free(ptr_array);
      }
      
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    Thanks Prelude. Very much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To malloc or not realloc on struct members
    By gh0st in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 05:27 AM
  2. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  3. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. Need help on malloc and realloc
    By YevGenius in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:55 AM