1 of 16

CSO101: Computer Programming

Lecture – 23

O.S.L. Bhavana

2 of 16

Memory Allocation

  • Constant length arrays may often waste space
  • C provides library functions for on-demand memory location
  • The library stdlib.h has the memory allocation functions malloc,

calloc, free, realloc etc.

3 of 16

malloc

  • Takes the number of bytes of memory to be allocated as input
  • Returns a pointer to the first byte in the memory block allocated
  • This pointer is a void* pointer which needs to be type casted later
  • Ex: int * pa = (int *) malloc(20); //
    • allocates a memory block of twenty bytes;
    • Returns the address of first byte as a pointer
    • This pointer is later type casted as an integer pointer in this example

  • Note that the allocated memory cells may have garbage value
  • The input to malloc can also be an expression.

Ex: int n; scanf(“%d”, &n); int * pa = (int *) malloc(n * sizeof(int)); // is valid

4 of 16

malloc

  • The allocated memory can then be used to store data using the returned pointer and the dereferencing operator.

  • If there is no sufficient space for allocation a NULL pointer will be returned

5 of 16

calloc

  • calloc stands for contigous allocation
  • It has two parameters
    • number of elements
    • Size of each element
  • It returns a void pointer for the starting address of a memory block sufficient to store the input number of elements with the given size.
  • int * a = (int *) calloc(10, sizeof(int)); //return a pointer to a block of memory to hold 10 elements each of size 4bytes
  • Each element in the block allocated by calloc is initialized to 0.
  • When there is no sufficient space for allocation a null pointer will be returned

6 of 16

calloc

7 of 16

free()

  • Used to free the memory allocated using malloc, calloc, realloc
  • Takes a pointer as input

8 of 16

free

  • free should not be used on pointers not returned by malloc, calloc, realloc
  • Such use results in errors.

9 of 16

realloc

  • Re-allocates memory
  • Suppose the amount of memory allocated by malloc/calloc needs to be modified, the realloc function may be used
  • realloc takes two parameters:
    • a pointer // this pointer should be a pointer returned by malloc/calloc
    • Number of bytes to be allocated
  • realloc works as follows:
    • Allocates a new memory block with the requested number of bytes
    • Copies the data of the block (given by the input pointer) into this new block
    • Frees the block with starting address given by the pointer

10 of 16

realloc

11 of 16

realloc

  • Re-allocates memory
  • Suppose the amount of memory allocated by malloc/calloc needs to be modified, the realloc function may be used
  • realloc takes two parameters:
    • a pointer // this pointer should be a pointer returned by malloc/calloc
    • Number of bytes to be allocated
  • realloc works as follows:
    • Allocates a new memory block with the requested number of bytes
    • Copies the data of the block (given by the input pointer) into this new block
    • Frees the block with starting address given by the pointer

12 of 16

realloc

13 of 16

getline( )

  • getline is a C library to read a full line from input stream (till \n)
  • It uses (dynamic) memory allocation to store the characters of line of variable length. (i.e the length of the line isn’t fixed)
  • getline code uses realloc to do this memory allocation.

14 of 16

getline( )

  • getline takes the following parameters as input
    • Address of a pointer given by malloc/calloc
    • Address of a variable to store length
    • Input stream (will be stdin for now)
  • To start with assume the input line will have 12 characters
    • size_t len = 13; // a variable to store length
    • We now allocate memory required to store these 13 char

char *str = (char *) malloc(13*sizeof(char));

    • Call getline(&str, &len, stdin);

15 of 16

getline( )

  • getline takes the following parameters as input
    • Address of a pointer given by malloc/calloc
    • Address of an variable to store length
    • Input stream (will be stdin for now)
  • Call getline(&str, &len, stdin);
  • If the input line has more than 12 characters getline reallocates memory required to store the full line.
    • Stores the address of the new re-allocated pointer as address of str.
    • It stores the length of re-allocated buffer in the address &len
    • This is not the length of the input string. Length of the input string should be calculated using strlen. This is only the buffer length

16 of 16

getline()