/* ----------------------------------------------------------------- * Sample program that compares statically defined 2D arrays with * two different ways of implementing dynamically allocated 2D arrays * * CS 314 Doug DeCarlo 3/10/04 * ----------------------------------------------------------------- */ #include #include /* Define size of test array */ #define AROWS 3 #define ACOLS 4 /* ----------------------------------------------------------------- */ /* Dynamically allocate a 2D "array" of integers, that is rows X cols * and accessed as arr[i][j] where i=0..ROWS-1, and j=0..COLS-1 * * It is stored as a set rows, each of which holds COLS integers; * an array of ROWS integer pointers points to its appropriate row */ int **make2Darray(int rows, int cols) { int i; int **p; /* Allocate array of pointers */ p = (int **)malloc(rows * sizeof(int *)); if (p == NULL) return NULL; for (i = 0; i < rows; i++) { /* Allocate row #i */ p[i] = (int *)malloc(cols * sizeof(int)); if (p[i] == NULL) return NULL; } return p; } /* Free the memory associated with the 2D array, given number of rows */ void free2Darray(int **p, int rows) { int i; for (i = 0; i < rows; i++) { /* Free row #i */ free(p[i]); } /* Free array of pointers */ free(p); } /* ----------------------------------------------------------------- */ /* Dynamically allocate a 2D "array" of integers, that is rows X cols * and accessed as arr[i][j] where i=0..rows-1, and j=0..cols-1 * * It is stored as a 1D array of ROWS*COLS integers, with an array * of ROWS integer pointers which refer to the appropriate places in this * larger array */ int **make2DarrayFlat(int rows, int cols) { int i; int **p, *base; /* Allocate array of pointers and 1D block of integers */ p = (int **)malloc(rows * sizeof(int *)); base = (int *)malloc(rows * cols * sizeof(int)); if (p == NULL || base == NULL) return NULL; /* Set pointers for each row */ for (i = 0; i < rows; i++) { p[i] = &base[cols * i]; } return p; } /* Free the memory associated with the 2D array */ void free2DarrayFlat(int **p) { /* Free block for array storage */ free(p[0]); /* Free array of pointers */ free(p); } /* ----------------------------------------------------------------- */ /* Print out a 2D array defined dynamically */ void print2DarrayDynamic(int **p, int rows, int cols) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf(" %2d", p[i][j]); } printf("\n"); } } /* Print out a 2D array defined statically */ void print2DarrayStatic(int p[][ACOLS], int rows, int cols) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf(" %2d", p[i][j]); } printf("\n"); } } /* Print out a 1D array */ void print1Darray(int p[], int rows) { int i; for (i = 0; i < rows; i++) { printf(" %2d", p[i]); } printf("\n"); } /* ----------------------------------------------------------------- */ int main() { int i, j; /* -- Create 3 different types of 2D arrays: 1 static, 2 dynamic */ int m1[AROWS][ACOLS] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; int **m2 = make2Darray(AROWS, ACOLS); int **m3 = make2DarrayFlat(AROWS, ACOLS); /* Copy m into m2 and m3 */ for (i = 0; i < AROWS; i++) { for (j = 0; j < ACOLS; j++) { m2[i][j] = m1[i][j]; m3[i][j] = m1[i][j]; } } /* Print m1,m2,m3 as two-dimensional arrays */ printf("m1 is:\n"); print2DarrayStatic(m1, AROWS, ACOLS); printf("m2 is:\n"); print2DarrayDynamic(m2, AROWS, ACOLS); printf("m3 is:\n"); print2DarrayDynamic(m3, AROWS, ACOLS); /* Print m1 as a flat array */ printf("m1 flat is:\n"); print1Darray((int *)m1, AROWS*ACOLS); /* Can't view m2 as flat, as isn't not stored as one chunk * (If we tried, it would access inappropriate memory locations!) * print1Darray(m2[0], AROWS*ACOLS); <-- doesn't work */ /* Print m3 as a flat array */ printf("m3 flat is:\n"); print1Darray(m3[0], AROWS*ACOLS); /* Free the dynamic arrays */ free2Darray(m2, AROWS); free2DarrayFlat(m3); return 0; } /* Compile and run: * % gcc twod.c -o twod * % ./twod * m1 is: * 1 2 3 4 * 5 6 7 8 * 9 10 11 12 * m2 is: * 1 2 3 4 * 5 6 7 8 * 9 10 11 12 * m3 is: * 1 2 3 4 * 5 6 7 8 * 9 10 11 12 * m1 flat is: * 1 2 3 4 5 6 7 8 9 10 11 12 * m3 flat is: * 1 2 3 4 5 6 7 8 9 10 11 12 * */