Você está na página 1de 4

Dynamic Memory Allocation

calloc() and malloc() are predefined functions in C


that allows us to dynamically create space for arrays
and structures. At runtime, we can allocate a particular
amount of space using these functions.

Using calloc()
• calloc() stands for contiguous allocation
• SYNTAX:
calloc(<numOfCells>, <bytesPerCell>)
where
<numOfCells> is an unsigned int and it refers to
the number of cells to be allocated, and
<bytesPerCell> is an unsigned int that refers to
the number of bytes allocated for each cell.
Ex:
#include<stdio.h>
#include<stdlib.h>
main() {
int dynArr; /* to be used as an array */
int num; /* size of the array */
printf(“How many cells do you want? ”);
scanf(“%d”, &num);
/* get space */
dynArr = (int *) calloc(num, sizeof(int));
... /* use dynArr as an array */
}
• Allocates contiguous memory for an array with
<numOfCells> elements, with each element having
<bytesPerCell> bytes.
• Space is initialized with all bits to zero.
• If calloc() is successful, a void pointer pointing to
the base address will be returned. Otherwise, NULL
will be returned.

Using malloc()
• malloc() stands for memory allocation
• Syntax:
malloc(<totalBytes>)
where
<totalBytes> is an unsigned int that refers to the
total number of bytes to be allocated for an array.
Ex:
#include<stdio.h>
#include<stdlib.h>
main() {
int dynArr; /* to be used as an array */
int num; /* size of the array */
int i;
printf(“How many cells do you want? ”);
scanf(“%d”, &num);
/* get space */
dynArr = (int *) malloc(num*sizeof(int));
/* use dynArr as an array */
for (i = 0; i < num; i++) {
printf(“%d”, dynArr[i]);
}
}
• If malloc() is successful, a void pointer pointing
to the base address will be returned. Otherwise,
NULL will be returned.
• Unlike calloc(), it does not initialize space in
memory.
• For large programs, it may take less time to use
malloc() compared to using calloc().

Freeing Space
• Space dynamically allocated (i.e., at runtime) with
calloc() or malloc() does not get returned to the
system upon function exit. Programmer must use
free() to explicitly return space.
• Syntax:
free(<ptr>);
where
<ptr> is a pointer dynamically allocated array.
• Causes space in memory pointed to by a pointer
to be deallocated. If <ptr> is NULL, this statement
would have no effect. Otherwise, <ptr> must point
to the base address of space previously allocated.
• Ex:
#include<stdio.h>
#include<stdlib.h>
main() {
int dynArr; /* to be used as an array */
int num; /* size of the array */
int i;
printf(“How many cells do you want? ”);
scanf(“%d”, &num);
/* get space */
dynArr = (int *) malloc(num*sizeof(int));
/* use dynArr as an array */
for (i = 0; i < num; i++) {
printf(“%d”, dynArr[i]);
}
/* finished using the memory location */
free(dynArr);
}
An Example
Create a program that gets a set of grades from the
user and computes the average of the given grades.
Ask the user for the number of grades to be entered
and store all the grades in a float array.

#include<stdio.h>

main()
{
float *grade, sum;
int num, i;
printf(“How many grades will you enter?”);
scanf(“%d”, &num);
/* allocate space for grade */
grade=(float *)malloc(num*sizeof(float));
/* grade is now ready for use */
for (i = 0; i < num; i++)
{
printf(“Enter grade%d: ”, i+1);
scanf(“%f”, &grade[i]);
/* scanf(“%f”, grade+i); */
}
for (i = 0; i < num; i++)
{
sum += grade[i];
/* sum += ? */
}
printf(“Ave: %f”, sum/num);
}

Você também pode gostar