Click here to Skip to main content
Click here to Skip to main content

Allocating memory to a 'Pointer to Pointer' variable

, 27 Nov 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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));
}

2 dimensional view

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);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rajesh R Nair
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberbeginnergSOAP12-Jan-12 1:53 
GeneralMy vote of 1 Pinmemberphuong303010-Jan-12 21:58 
GeneralMy vote of 5 PinmemberAthrej17-Apr-11 7:44 
General*val should be *a Pinmemberclausscheiblauer8-Oct-10 23:52 
GeneralAllocating memory PinmemberMidNightCoder01019-Aug-10 8:33 
Generalxor cipher code PinmemberSadru30-Aug-09 14:53 
Generalhelp for beginner Pinmembersihotamarpal28-Aug-06 2:34 
Generalwhy you did not get higher rating ... PinmemberMaximilien28-Nov-05 2:43 
GeneralRe: why you did not get higher rating ... PinmemberRajesh R Nair28-Nov-05 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 27 Nov 2005
Article Copyright 2005 by Rajesh R Nair
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid