MCS-201 · Unit 7

Pointers

Section 6 of 7

Block 2 · Unit 7Pointers10 min · Section 6 of 7

7.6Arrays of Pointers and Dynamic Memory Allocation

Covers IGNOU MCS-201 §7.8

An ordinary array’s size is fixed when the program is written. Often the size is known only when the program runs: the number of rows the user asks for, for example. C handles this by requesting memory at run time and keeping its address in a pointer. An array of pointers then lets you manage several such blocks.

Arrays of pointers

int *ptr[3]; declares an array of pointers: three elements, each an int *. It is different from a two-dimensional array:

int arr[3][5];int *ptr[3];
What is created15 int objects in one block3 pointers and no int objects
RowsFixed length 5, stored togetherEach pointer must be made to point to separately obtained memory
Row lengthsAll equalMay differ
Element accessarr[i][j]ptr[i][j], once each ptr[i] points to valid memory

Dynamic memory allocation

The functions are declared in <stdlib.h>:

FunctionPurpose
malloc(n)Allocates n bytes; the contents are indeterminate (garbage)
calloc(count, size)Allocates count × size bytes, all set to zero
realloc(p, n)Changes the size of a previously allocated block
free(p)Releases a block obtained from the functions above

Rules for using malloc:

  1. Ask for count * sizeof(type) bytes: p = malloc(n * sizeof(int));.
  2. malloc returns a void *, which C converts automatically to the pointer type on the left; a cast such as (int *) is allowed but not required.
  3. Check the result: malloc returns NULL if the memory cannot be provided.
  4. Use the block like an array: p[0] to p[n - 1].
  5. Call free(p) exactly once when finished. Memory that is never freed is a memory leak.

Key takeaways

  • type *a[n]; is an array of n pointers; each must point to valid memory before use.
  • malloc, calloc and realloc allocate memory at run time; free releases it.
  • Always multiply by sizeof(type), check for NULL, and free each block once.
  • Dynamically allocated blocks can be used with subscript or pointer notation.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which header declares malloc and free?

Show answer

Answer: C. <stdlib.h>

The dynamic memory functions are declared in <stdlib.h>.

Question 2 · multiple choice

Which call correctly allocates memory for 25 values of type float?

Show answer

Answer: B. malloc(25 * sizeof(float));

malloc takes a number of bytes, so the count must be multiplied by the size of one element. malloc(25) gives only 25 bytes.

Question 3 · multiple choice

What does malloc return if the memory cannot be allocated?

Show answer

Answer: D. NULL

A null pointer signals failure, which is why the result must be tested before use.

Question 4 · multiple choice

What does int *ptr[4]; declare?

Show answer

Answer: A. An array of 4 pointers to int

[] binds more tightly than *, so ptr is an array whose elements are int *. No integers are created.

Question 5 · multiple choice

What is a memory leak?

Show answer

Answer: C. Allocated memory that is never released with free

Memory obtained with malloc remains reserved until freed; forgetting to free it wastes memory for as long as the program runs.

Question 6 · short answer

Write a fragment that asks the user for a number n, allocates an array of n integers, fills it with the squares 0, 1, 4, …, prints the last one, and releases the memory.

Show worked answer

int n, i; int *sq; scanf("%d", &n); sq = malloc(n * sizeof(int)); if (sq == NULL) { printf("Out of memory\n"); return 1; } for (i = 0; i < n; i++) sq[i] = i * i; printf("%d\n", sq[n - 1]); free(sq);. malloc reserves n × sizeof(int) bytes and returns the address, which is checked before use; sq[i] then works exactly like an array element; for n = 5 the last square printed is 16; free returns the block to the system.

↑ ↓ move↵ openTab switch type