Dynamic Memory Allocation


Lecture 24

Your computer's memory is a resource - it can run out. The memory usage for program data can increase or decrease as your program runs.
Up until this point, the memory allocation for your program has been handled automatically when compiling. However, sometimes the computer doesn't know how much memory to set aside (for example, when you have an unsized array).
The following functions give you the power to dynamically allocate memory for your variables at RUN-TIME (whilst the program is running). For the past tutorials, memory was allocated when the program was compiled (i.e. COMPILE-TIME).
To use the four functions discussed in this section, you must include the stdlib.h header file.

malloc ( )

malloc requires one argument - the number of bytes you want to allocate dynamically.
If the memory allocation was successful, malloc will return a void pointer - you can assign this to a pointer variable, which will store the address of the allocated memory.
If memory allocation failed (for example, if you're out of memory), malloc will return a NULL pointer.
Passing the pointer into free will release the allocated memory - it is good practice to free memory when you've finished with it.
This example will ask you how many integers you'd like to store in an array. It'll then allocate the memory dynamically using malloc and store a certain number of integers, print them out, then releases the used memory using free.

void main(){
      clrscr();
      int num;
      int*ptr;
      printf("how many numbers:");
      scanf("%d",&num);
      ptr=(int *) malloc(num*sizeof(int));
      for (int i=0;i<num;i++){
            *ptr=i;
            ptr++;
      }
      ptr--;
      for (i=num;i>0;i--){
            printf("\n%d",*ptr);
            ptr--;
      }
      free (ptr);
      getch();
}

calloc ( )

calloc is similar to malloc, but the main difference is that the values stored in the allocated memory space is zero by default. With malloc, the allocated memory could have any value.
calloc requires two arguments. The first is the number of variables you'd like to allocate memory for. The second is the size of each variable.
Like malloc, calloc will return a void pointer if the memory allocation was successful, else it'll return a NULL pointer.
This example shows you how to call calloc and also how to reference the allocated memory using an array index. The initial value of the allocated memory is printed out in the for loop.
void main(){
      clrscr();
      int num;
      int*ptr;
      printf("how many numbers:");
      scanf("%d",&num);
      ptr=(int *) calloc(num,sizeof(int));
      for (int i=0;i<num;i++){
            *ptr=i;
            ptr++;
      }
      ptr--;
      for (i=num;i>0;i--){
            printf("\n%d",*ptr);
            ptr--;
      }
      free (ptr);
      getch();
}

realloc ( )

Now suppose you've allocated a certain number of bytes for an array but later find that you want to add values to it. You could copy everything into a larger array, which is inefficient, or you can allocate more bytes using realloc, without losing your data.
realloc takes two arguments. The first is the pointer referencing the memory. The second is the total number of bytes you want to reallocate.
Passing zero as the second argument is the equivalent of calling free.
Once again, realloc returns a void pointer if successful, else a NULL pointer is returned.
This example uses calloc to allocate enough memory for an int array of five elements. Then realloc is called to extend the array to hold seven elements.
void main(){
      clrscr();
      int num;
      int*ptr;
      printf("how many numbers:");
      scanf("%d",&num);
      ptr=(int *) calloc(num,sizeof(int));
      for (int i=0;i<num;i++){
            *ptr=i;
            ptr++;
      }
      ptr--;
      for (i=num;i>0;i--){
            printf("\n%d",*ptr);
            ptr--;
      }
      ptr=(int *) realloc(ptr,7*sizeof(int));
      free (ptr);
      getch();
}

1 comments:

Unknown said...

Very informative article. aircon chemical cleaning

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms