Easy C++


C Tutorial - Lesson 12: Memory Allocation

Malloc and Free

by John Kopp

Support this site at no cost to you

Welcome to easyCPlusPlus.com's free tutorial on C programming. If my recollection is correct, this lesson covers the use of memory allocation. :-). Memory allocation provides a way to dynamically create buffers and arrays. Dynamic means that the space is allocated in memory as the program is executing. On many occasions the sizes of objects will not be known until run time. For instance, the length of a string a user inputs will not be known prior to execution. The size of an array may depend on parameters unknown until program execution. Certain data structures such as linked lists utilize dynamic memory allocation.

How to Allocate and Free Memory
The standard library function malloc is commonly used to allocate memory. Its prototype is:

void *malloc(size_t nbytes);

Malloc returns a void pointer to the allocated buffer. This pointer must be cast into the proper type to access the data to be stored in the buffer. On failure, malloc returns a null pointer. The return from malloc should be tested for error as shown below.

char *cpt;
if ((cpt = (char *) malloc(25)) == NULL)
    printf("Error on malloc\n");

The data type size_t is equivalent to unsigned integer. The number of bytes of memory needed depends on the number of objects to be stored and the size of each object. Char data is stored in one byte. Float data is stored in four bytes. On most 32-bit machines, ints are stored in four bytes. The easiest way to determine the size of the object to be stored is to use the sizeof operator, which returns the size in bytes of an object. Sizeof can be called with either an object or a data type as its argument. Here is a simple program illustrating its use.

#include <stdio.h>

typedef struct employee_st {
    char name[40];
    int id;
} Employee;

int main()
    int myInt;
    Employee john;

    printf("Size of int is %d\n",sizeof(myInt));
        /* The argument of sizeof is an object */
    printf("Size of int is %d\n",sizeof(int));
        /* The argument of sizeof is a data type */

    printf("Size of Employee is %d\n",sizeof(Employee));
        /* The argument of sizeof is an object */
    printf("Size of john is %d\n",sizeof(john));
        /* The argument of sizeof is a data type */

    printf("Size of char is %d\n",sizeof(char));
    printf("Size of short is %d\n",sizeof(short));
    printf("Size of int is %d\n",sizeof(int));
    printf("Size of long is %d\n",sizeof(long));
    printf("Size of float is %d\n",sizeof(float));
    printf("Size of double is %d\n",sizeof(double));

    return 0;


Here is the output.

When using dynamically allocated memory, it is necessary for the programmer to free the memory after its use. This is done, surprisingly enough, using the standard library function free. Its prototype is:

void free(void *pt);

It is not necessary to cast the pointer argument back to void. The compiler handles this conversion.

Previous Page       Next Page