#### Archived

This topic is now archived and is closed to further replies.

# Multi-Dimensional Arrays

This topic is 5816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do you allocate multi-dimensional arrays and how do you clean them up? I can''t seem to find it in my c++ reference for some reason.

##### Share on other sites
Here''s how you do it in C, YMMV

  // 2D array allocation#include <stdlib.h>#include <stdio.h>/*-------------------------------------------------------------------------------------  FUNCTION		: array_int_2D(int nrows, int ncols)  PURPOSE		: Template for allocating arrays. Could be used for any 2D array.  RETURN     	: Start address of array  PARAMS     	: nrows  - number of rows				: ncols  - number of columns--------------------------------------------------------------------------------------*/int **array_int_2D(int nrows, int ncols){	int i, j;	// allocate the memory for rows	int **arr = malloc(nrows * sizeof(int *));	if(!arr) return 0;	printf("Pointers to Rows allocated: %p\n", arr);	// allocate the rows i.e. number of columns/row	for(i = 0; i < nrows; i++){		arr[i] = malloc(ncols * sizeof(int));		if (!arr[i]){			for (int k=0; k<(i-1); k++)				free(arr[k]);  // release memory for all previously allocated			free(arr);		    return 0;		}		printf("Row %2d allocated: %p\n", i, arr[i]);	}	// fill in some values	for (i=0; i < nrows; i++)		for (j=0; j < ncols; j++)			arr[i][j] = (i+1) * (j+1); // rows	return arr;}/*-------------------------------------------------------------------------------------  FUNCTION		: array_free_2D(int ***arr, int nrows, int ncols)  PURPOSE		: Free memory allocated for 2D array.  RETURN     	: NONE  PARAMS     	: arr    - pointer to 2D array				: nrows  - number of rows				: ncols  - number of columns--------------------------------------------------------------------------------------*/void array_free_2D(int ***arr, int nrows, int ncols){	int **array = *arr;		// dereference pointer to array	if (array){		for( int i = 0; i < nrows; i++){			free(array[i]);			printf("Row %2d de-allocated: %p\n", i, array[i]);		}		free(array);		printf("Pointers to Rows de-allocated: %p\n", array);		*arr = 0;	}}int main (void){	int i, j;	int nrows = 24;	int ncols = 8;	int **matric;	matric = array_int_2D(nrows, ncols);	printf("\n------------------------------------------------------------------------------\n");	printf("Dumping array contents\n");	printf("\n------------------------------------------------------------------------------\n");	if (matric){		printf("Column |");		for (i=0; i < ncols; i++)			printf("\t%3d",i);		printf("\n------------------------------------------------------------------------------\n");		for (i=0; i < nrows; i++) {			printf("Row %2d |", i);			for (j=0; j < ncols; j++)				printf("\t%3d", matric[i][j]);			printf("\n");		}	}else{		printf("No mem");	}	printf("\n------------------------------------------------------------------------------\n");	// release the memory	printf("Address of array before free %p\n", matric);	array_free_2D(&matric, nrows, ncols);	printf("Address of array after free %p\n", matric);	return 0;}

##### Share on other sites
Thanks, found exactly what I was looking for. Sorry for not searching first.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631737
• Total Posts
3001944
×