Code.GeekInterview.com
 
Code Samples C
 

Basic Memory Management


Code ResourceAuthor: jbode  

Difficulty Level: Beginner

Published: 16th Jan 2011   Read: 2935 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 

Basic Memory Management; Allocating and Resizing a Buffer

There are times when you don't know ahead of time how large of a buffer you need. The following code shows one approach to dynamically allocating and resizing a buffer. Note that passing a NULL pointer to "realloc" has the same effect as calling "malloc". If the call to "realloc" fails, it will return a NULL pointer. The following sample works with a buffer of integer values.

 


Sample Code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /**
  5.  * Allocate or resize a buffer of integers.  If the buffer
  6.  * cannot be resized, the original buffer is left intact.
  7.  *
  8.  * @param buffer  - pointer to the base of the buffer.  
  9.  * @param cursize - number of elements currently allocated
  10.  *                  to the buffer.
  11.  * @param extent  - number of elements by which to extend
  12.  *                  the buffer.
  13.  * @return int    - 1 if allocation/reallocation is
  14.  *                  successful, 0 otherwise
  15.  */
  16. int resize(int **buffer, size_t *cursize, size_t extent)
  17. {
  18.   int result = 1;
  19.   /**
  20.    * Call realloc on the specified buffer; if the buffer
  21.    * is NULL, then this is the same as calling malloc.
  22.    */
  23.   int *tmp = realloc(*buffer, (*cursize + extent) * sizeof **buffer);
  24.   if (tmp)
  25.   {
  26.     *buffer = tmp;
  27.     *cursize += extent;
  28.   }
  29.   else
  30.   {
  31.     result = 0;
  32.   }
  33.  
  34.   return result;
  35. }
  36.  
  37. /**
  38.  * Sample driver (assume numbers.dat contains a sequence
  39.  * of integer values and no other characters).
  40.  */
  41. #define EXTENT 5 /* Expand the array by 5 elements at a time */
  42. int main(void)
  43. {
  44.   int    *arr         = NULL;
  45.   FILE   *data        = fopen("numbers.dat", "r");
  46.   size_t  arraySize   = 0;
  47.   size_t  numbersRead = 0;
  48.   int     nextValue;
  49.  
  50.   if (data)
  51.   {
  52.     while (fscanf(data,"%d", &nextValue) == 1)
  53.     {
  54.       if (arraySize <= numbersRead)
  55.       {
  56.         if (!resize(&arr, &arraySize, EXTENT))
  57.         {
  58.           printf("Error while attempting to resize the arrayn");
  59.           break;
  60.         }
  61.       }
  62.       arr[numbersRead++] = nextValue;
  63.     }
  64.     fclose(data);
  65.   }
  66.  
  67.   if (numbersRead > 0)
  68.   {
  69.     size_t i;
  70.     for (i = 0; i < numbersRead; i++)
  71.     {
  72.       printf("buf[%lu] = %dn", (unsigned long) i, buf[i]);
  73.     }
  74.   }
  75.  
  76.   /**
  77.    * We must manually release the memory we allocated.
  78.    */
  79.   free(buf);
  80.   return 0;
  81. }
Copyright GeekInterview.com


Next Article: Finding a number is even or odd without using arithmatic operators


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

Error on line 52 -- the call to scanf should be fscanf(data, "%d", &nextValue)
Comment posted by: jbode on 2011-02-10T13:43:40

Popular Coders

# Coder NameHits
1. kaivalya198933967
2. mano.mithun21650
3. deepu0817875
4. meefriend4ever8124
5. chandrikakr8092
6. sadasivathavamani7363
7. shashivaishnav7162
8. venkatakrishnansvpr7078
9. Jimmy Zorald6713
10. sripri5712

Active Coders

Refined Tags