12,068,967 members (62,988 online)
alternative version

67K views
14 bookmarked
Posted

# Allocating memory to a 'Pointer to Pointer' variable

, 27 Nov 2005 CPOL
 Rate this:
Describes how to allocate memory to and retrieve values from pointer to pointer variables.

## Introduction

I am hoping this article would help beginners. It describes how memory can be dynamically allocated, how values are assigned or retrieved, and how memory is freed up for 'pointer to pointer' variables.

#### Pointers, Reference operator (&), and Dereference operator (*)

Before discussing about 'pointer to pointer' variables, we will see what a pointer variable is and what the reference operator (`&`) and dereference operator (`*`) are used for.

A pointer variable can be declared as:

`type* variable;`

Example:

`int* a;`

The above declaration means that the pointer variable "`a`" can hold the address of memory allocated to an integer variable.

```int val;
val=10;
a=&val;
printf("%d", *val);
*val=15;
printf("%d", *val);```

In the above sample code, the reference operator (`&`) is used to refer the address of the memory location assigned for the integer variable '`val`'. Also, the dereference operator (`*`) is used to point to the content of the memory location whose address is held by the pointer variable '`val`'.

The first `printf` statement prints '10', and the second `printf` statement prints '15'.

#### Pointer to pointer variables

A pointer to pointer variable can hold the address of another pointer variable. It can be declared as:

`type** variable;`

#### Allocating memory

Think of the memory to be allocated to a pointer to pointer variable as two dimensional. It has 'rows' and 'columns'; i.e., if the size is m x n, there will be 'm' rows, and for each row, there will be 'n' columns.

1. First, you have to allocate memory for 'm' rows.
2. Secondly, you have to allocate 'n' columns for each of the 'm' rows.

We will take an example for allocating memory to a pointer to pointer to float values. Let the number of rows be '4' and the number of columns '3'.

```float **float_values;
//allocate memory for rows
float_values = (float**)malloc(4 *sizeof(float*));
//for each row allocate memory for columns
for(int i=0; i<4; i++)
{
*(float_values+i) = (float*)malloc(3 *sizeof(float));
}```

#### Assigning values

We need to know how we can access each location before assigning values to them.

There are two ways to access these two dimensional memory locations.

1. Use the `[]` operator in the same way we use for two dimensional arrays.
2. ```float val;
for(int i=0;i<4;i++)
for(int j=0;j<3;j++)
{
scanf("%f", &val);
float_values[i][j] = val;
}```
3. Use the pointer operator ("`*`")
4. ```float val;
for(int i=0;i<4;i++)
for(int j=0;j<3;j++)
{
scanf("%f", &val);
*(*(float_values+i)+j) = val;
}```

#### Display or retrieve values

For retrieving, we can use any of the two operators which we used for assigning values. An example for using the "`*`" operator is given below.

```for(int i=0; i<2; i++)
for(int j=0; j<3; j++)
{
printf("%f\n", *(*(floats+i)+j));
}```

#### Freeing up the allocated memory

We should not forget to free the memory which we have allocated dynamically, starting from the lowest level, i.e., in the reverse order of allocations. If we free up the higher level pointers first, the lower level pointers would be lost and we would not be able to free up the memory allocated to those pointers. This would lead to memory leaks. Given below is how we should free up memory:

```//freeing up memory allocated to second level pointers
for(int i = 0; i < 4; i++)
{
free(float_values[i]);
}
//freeing up memory allocated to first level pointers
free(float_values);```

## Share

 Software Developer United States
No Biography provided

## You may also be interested in...

 First Prev Next
 My vote of 1 beginnergSOAP12-Jan-12 2:53 beginnergSOAP 12-Jan-12 2:53
 My vote of 1 phuong303010-Jan-12 22:58 phuong3030 10-Jan-12 22:58
 My vote of 5 Athrej17-Apr-11 8:44 Athrej 17-Apr-11 8:44
 *val should be *a clausscheiblauer9-Oct-10 0:52 clausscheiblauer 9-Oct-10 0:52
 Allocating memory MidNightCoder01019-Aug-10 9:33 MidNightCoder010 19-Aug-10 9:33
 help for beginner sihotamarpal28-Aug-06 3:34 sihotamarpal 28-Aug-06 3:34
 why you did not get higher rating ... Maximilien28-Nov-05 3:43 Maximilien 28-Nov-05 3:43
 Re: why you did not get higher rating ... Rajesh R Nair28-Nov-05 5:24 Rajesh R Nair 28-Nov-05 5:24
 Last Visit: 31-Dec-99 19:00     Last Update: 8-Feb-16 22:01 Refresh 1