C - Dynamic Memory Allocation                                            Home : www.sharetechnote.com

 

 

 

 

Dynamic Memory Allocation

 

In most cases especially at early stage when you learn C language you wouldn't think much of dynamic memory allocation. However as your programming skills gets deeper (especailly as you start using pointers more frequently) you would come across the case where you need to secure a block of memory on your own and clear it when you don't need it.  This kind of memory allocation is called Dynamic Memory Allocation.

Usually dynamic memery allocation is used when you (programmer or compiler) don't know exactly how much of the memory you need when you are writing the code and you want to allocate the memory block (in some case with variable size) while the program is running.

There are several functions that are commonly used for Dynamic Memory Allocation. Definately malloc() is the most commonly used function.

 

Function

Usage

Easier(?) Form

malloc()

void *malloc(size_t size)

(cast-type*) malloc(the size of memory in bytes)

calloc()

void *calloc(size_t nitems, size_t size)

(cast-type*)calloc(number of elements, size of a single element)

free()

void free(void *ptr)

void free(pointer to a memory)

realloc()

void *realloc(void *ptr, size_t size)

(cast-type*) malloc(pointer to memory, new size)

 

At first the syntax would not look easy and sometimes confusing when you compare the function prototype in the API document and the real usage in many source code examples, but don't get disappointed too much. It is not only you who feels like this. It just take time and a little bit of practice to get familiar with it.

 

 

Allocating Memory for Character Array

 

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

 

 

int main(int argc, char* argv[])

{

    char *myStrPtr;

    int i = 0;

 

    myPtr = &myVar;

 

    printf("Address of myStrPtr : %#X\n",&myStrPtr);

 

    myStrPtr=(char *)malloc(20);

    printf("myStrPtr (The address value stored at myStrPtr) : %#X\n",myStrPtr);

 

    strcpy(myStrPtr,"Hello World");

    printf("A string pointed by myStrPtr : %s\n",myStrPtr);

    

    for(i = 0; i < 11; i++)

    {

        printf("Address Stored at (myStrPtr+%d),Value pointed by (myStrPtr+%d = %#X) : %c\n",

                 i,i,(myStrPtr+i),*(myStrPtr+i));

    };

 

    return 0;

}

 

 

Result >

: Following is the result of the program. You may see different values labeled in blue because this is memory address and your system may allocate different locations when you execute the program. For more detailed explanation on this result refer to Memory Allocation for Character Array in Pointer Page.

 

Address of myStrPtr : 0X24FD68

myStrPtr (The address value stored at myStrPtr) : 0X327EE8

A string pointed by myStrPtr : Hello World

Address Stored at (myStrPtr+0),Value pointed by (myStrPtr+0 = 0X327EE8) : H

Address Stored at (myStrPtr+1),Value pointed by (myStrPtr+1 = 0X327EE9) : e

Address Stored at (myStrPtr+2),Value pointed by (myStrPtr+2 = 0X327EEA) : l

Address Stored at (myStrPtr+3),Value pointed by (myStrPtr+3 = 0X327EEB) : l

Address Stored at (myStrPtr+4),Value pointed by (myStrPtr+4 = 0X327EEC) : o

Address Stored at (myStrPtr+5),Value pointed by (myStrPtr+5 = 0X327EED) :

Address Stored at (myStrPtr+6),Value pointed by (myStrPtr+6 = 0X327EEE) : W

Address Stored at (myStrPtr+7),Value pointed by (myStrPtr+7 = 0X327EEF) : o

Address Stored at (myStrPtr+8),Value pointed by (myStrPtr+8 = 0X327EF0) : r

Address Stored at (myStrPtr+9),Value pointed by (myStrPtr+9 = 0X327EF1) : l

Address Stored at (myStrPtr+10),Value pointed by (myStrPtr+10 = 0X327EF2) : d

 

 

Allocating Memory for single struct

 

#include <stdio.h>

 

struct Point {

    

    float x;

    float y;

    

};

 

int main()

{

    

    struct Point *pt;

    

    pt = (struct Point*) malloc(sizeof(struct Point));

    

    pt->x = 1.2;

    pt->y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt->x, pt->y);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Allocating Memory for struct Array

 

#include <stdio.h>

 

struct Point {

    

    float x;

    float y;

    

};

 

int main()

{

    

    struct Point *pt;

    int i = 0;

    

    pt = (struct Point*) malloc(5 * sizeof(struct Point));

    

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

            (pt+i)->x = i;

            (pt+i)->y = 10*i;

    

            printf("The coordinate of (pt+%d) is = (%f,%f)\n", i, (pt+i)->x, (pt+i)->y);

    }

    

    return 1;

}

 

 

Result :

The coordinate of (pt+0) is = (0.000000,0.000000)

The coordinate of (pt+1) is = (1.000000,10.000000)

The coordinate of (pt+2) is = (2.000000,20.000000)

The coordinate of (pt+3) is = (3.000000,30.000000)

The coordinate of (pt+4) is = (4.000000,40.000000)