KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q39976: Dynamic Allocation for Two Dimensional Arrays

Article: Q39976
Product(s): See article
Version(s): 4.00 5.x | 5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | s_quickc | mspl13_c
Last Modified: 4-JAN-1989

Question:

How can I dynamically allocate memory for a two dimensional array?

Response:

The best method depends on the flexibility needed and the amount of
information concerning the array known at coding time. The examples
below set up two dimensional arrays of characters. Any element can be
accessed with double sub-scripts, such as array[i][j]. Symbols in
uppercase are constants, while those in lowercase are variables.

If one of the dimensions of the array is known, either of the
following two methods can be used. The first example creates more
overhead due to the number of malloc calls, but it is more flexible
because each malloc can be of a different size.

The following is the first example:

    char *array[DIM_ONE];
    int   i;

    for (i = 0; i < DIM_ONE; i++) {
        array[i] = (char *)malloc(sizeof(char) * dim_two);
        if (array[i] == NULL) {
            printf("Not enough memory for columns in row %d!\n", i);
            exit(1);
        }
    }

The following is the second example:

    char *array[DIM_ONE];
    int   i;

    array[0] = (char *)malloc(sizeof(char) * DIM_ONE * dim_two);
    if (array[0] == NULL) {
        printf("Not enough memory for columns!\n");
        exit(1);
    }
    for (i = 1; i < DIM_ONE; i++) {
        array[i] = (array[0] + (i * dim_two));
    }

If neither of the dimensions is known at coding time, one of the
following two methods can be used. The pros and cons of each example
are the same as those for the previous examples.

The following is the third example:

    char **array;
    int    i;

    array = (char **)malloc(sizeof(char *) * dim_one);
    if (array == NULL) {
        printf("Not enough memory for rows!\n");
        exit(1);
    }
    for (i = 0; i < dim_one; i++) {
        array[i] = (char *)malloc(sizeof(char) * dim_two);
        if (array[i] == NULL) {
            printf("Not enough memory for columns in row %d!\n", i);
            exit(1);
        }
    }

The following is the fourth example:

    char **array;
    int    i;

    array = (char **)malloc(sizeof(char *) * dim_one);
    if (array == NULL) {
        printf("Not enough memory for rows!\n");
        exit(1);
    }
    array[0] = (char *)malloc(sizeof(char) * dim_one * dim_two);
    if (array[0] == NULL) {
        printf("Not enough memory for columns!\n");
        exit(1);
    }
    for (i = 1; i < dim_one; i++) {
        array[i] = (array[0] + (i * dim_two));
    }

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.