1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Array whose dimensions are determined in the run-time

Discussion in 'C' started by go4expert, Aug 24, 2004.

  1. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    7
    Trophy Points:
    0
    hello!

    I want to define a 2-dimensional array whose dimensions are determined in the run-time.(in c language, not c++)

    I want to use dynamic memory allocation functions of the standard library. Is there anybody who can help?
     
    Last edited: Aug 24, 2004
  2. Amit Ray

    Amit Ray New Member

    Joined:
    Jul 12, 2004
    Messages:
    75
    Likes Received:
    4
    Trophy Points:
    0
    Occupation:
    Software Developer
    Home Page:
    It is usually best to allocate an array of pointers, and then initialize each pointer to a dynamically-allocated ``row.'' Here is a two-dimensional example:

    #include <stdlib.h>

    int **array1 = (int **)malloc(nrows * sizeof(int *));
    for(i = 0; i < nrows; i++)
    array1 = (int *)malloc(ncolumns * sizeof(int));


    (In real code, of course, all of malloc's return values would be checked.)

    You can keep the array's contents contiguous, while making later reallocation of individual rows difficult, with a bit of explicit pointer arithmetic:

    int **array2 = (int **)malloc(nrows * sizeof(int *));
    array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
    for(i = 1; i < nrows; i++)
    array2 = array2[0] + i * ncolumns;


    In either case, the elements of the dynamic array can be accessed with normal-looking array subscripts: arrayx[j] (for 0 <= i < NROWS and 0 <= j < NCOLUMNS).

    If the double indirection implied by the above schemes is for some reason unacceptable, you can simulate a two-dimensional array with a single, dynamically-allocated one-dimensional array:

    int *array3 = (int *)malloc(nrows * ncolumns * sizeof(int));​


    However, you must now perform subscript calculations manually, accessing the i,jth element with array3[i * ncolumns + j]. (A macro could hide the explicit calculation, but invoking it would require parentheses and commas which wouldn't look exactly like multidimensional array syntax, and the macro would need access to at least one of the dimensions, as well.)

    Finally, you could use pointers to arrays:

    int (*array4)[NCOLUMNS] = (int (*)[NCOLUMNS])malloc(nrows * sizeof(*array4));​


    but the syntax starts getting horrific and at most one dimension may be specified at run time.

    With all of these techniques, you may of course need to remember to free the arrays when they are no longer needed, and you cannot necessarily intermix dynamically-allocated arrays with conventional, statically-allocated ones

    All of these techniques can also be extended to three or more dimensions. Hope that helped.

    For more on Arrays and Pointers the following would make a good read :

    http://www.eskimo.com/~scs/C-faq/s6.html

    Cheers,
    Amit Ray. :)
     
  3. elec.shabnam

    elec.shabnam New Member

    Joined:
    Feb 13, 2008
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    sir your explanation is nice.even new people like me can understand
     

Share This Page