creating a 2-d array using dynamic memory allocation

Discussion in 'C Programming' started by kaul, Sep 8, 2004.

  1. kaul

    kaul Guest

    i want to create a 2-d array containg r rows and c columns by dynamic
    memory allocation in a single statement so that i will be able to access
    the ith and jth index as say arr[j]
    how is that possible?
    kaul, Sep 8, 2004
    #1
    1. Advertising

  2. kaul

    Amit Guest

    kaul wrote:
    > i want to create a 2-d array containg r rows and c columns by dynamic
    > memory allocation in a single statement so that i will be able to access
    > the ith and jth index as say arr[j]
    > how is that possible?
    >

    try this:

    int nrows,ncols;
    int **array;
    array = malloc(nrows * sizeof(int *));
    for (i=0;i<nrows;i++)
    array=malloc(ncols * sizeof(int));
    Amit, Sep 8, 2004
    #2
    1. Advertising

  3. Reducing malloc() call for performance, please

    int** int_matrix_create(int width, int height, char* functionName, int
    lineNumber) {
    int** int_matrix;
    int* cursor;
    int i;
    int_matrix = (int** )malloc(width * sizeof(int*) + width * height *
    sizeof(int));
    cursor = (int*)int_matrix + width;
    for (i = 0; i < width; i++) {
    int_matrix = cursor;
    cursor += height;
    }
    return int_matrix;
    }

    "Amit" <> ??? news:gBy%c.3$
    ???...
    > kaul wrote:
    > > i want to create a 2-d array containg r rows and c columns by dynamic
    > > memory allocation in a single statement so that i will be able to access
    > > the ith and jth index as say arr[j]
    > > how is that possible?
    > >

    > try this:
    >
    > int nrows,ncols;
    > int **array;
    > array = malloc(nrows * sizeof(int *));
    > for (i=0;i<nrows;i++)
    > array=malloc(ncols * sizeof(int));
    Andrew Au \(Newsgroup\), Sep 8, 2004
    #3
  4. "kaul" <> writes:
    > i want to create a 2-d array containg r rows and c columns by dynamic
    > memory allocation in a single statement so that i will be able to access
    > the ith and jth index as say arr[j]
    > how is that possible?


    C FAQ 6.16.
    <http://www.eskimo.com/~scs/C-faq/faq.html>
    <http://www.eskimo.com/~scs/C-faq/q6.16.html>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 8, 2004
    #4
  5. kaul

    Mike Tyka Guest

    "Amit" <> wrote in message
    news:gBy%c.3$...
    > kaul wrote:
    > > i want to create a 2-d array containg r rows and c columns by dynamic
    > > memory allocation in a single statement so that i will be able to access
    > > the ith and jth index as say arr[j]
    > > how is that possible?
    > >

    > try this:
    >
    > int nrows,ncols;
    > int **array;
    > array = malloc(nrows * sizeof(int *));
    > for (i=0;i<nrows;i++)
    > array=malloc(ncols * sizeof(int));




    I never though about doing it this way, i always allocate a linear array of
    row*columns elements and
    access them using a little macro:

    #define sqrmat(y,x,width) ((y)*(width) + (x))
    random access then looks like array[sqrmat(myRow,myColumn,nColumns)]

    I wonder if anyone has any views on how the two approches compare in terms
    of performance ?
    In the above method, i assume C must do an internal multiplication of the
    sort i'm implementing
    explicitly ?

    Mike
    Mike Tyka, Sep 8, 2004
    #5
  6. Hi Mike,

    The ultimate answer to this question is through benchmarking.
    See what are your objectives (performance, or space or what)

    There are various approach to create a matrix in C, namely:

    multiple malloc() calls
    single malloc() call with pointer arithmetic
    simulate using 1D array.

    I suggest a benchmarking by creating once, and index the array a lot of
    times and measure the mean allocation time, mean access time, and mean free
    time.

    Andrew

    "Mike Tyka" <> ¦b¶l¥ó news: ¤¤¼¶¼g...
    >
    > "Amit" <> wrote in message
    > news:gBy%c.3$...
    > > kaul wrote:
    > > > i want to create a 2-d array containg r rows and c columns by dynamic
    > > > memory allocation in a single statement so that i will be able to

    access
    > > > the ith and jth index as say arr[j]
    > > > how is that possible?
    > > >

    > > try this:
    > >
    > > int nrows,ncols;
    > > int **array;
    > > array = malloc(nrows * sizeof(int *));
    > > for (i=0;i<nrows;i++)
    > > array=malloc(ncols * sizeof(int));

    >
    >
    >
    > I never though about doing it this way, i always allocate a linear array

    of
    > row*columns elements and
    > access them using a little macro:
    >
    > #define sqrmat(y,x,width) ((y)*(width) + (x))
    > random access then looks like array[sqrmat(myRow,myColumn,nColumns)]
    >
    > I wonder if anyone has any views on how the two approches compare in terms
    > of performance ?
    > In the above method, i assume C must do an internal multiplication of the
    > sort i'm implementing
    > explicitly ?
    >
    > Mike
    >
    >
    >
    Andrew Au \(Newsgroup\), Sep 8, 2004
    #6
  7. kaul

    Old Wolf Guest

    "Andrew Au \(Newsgroup\)" <> wrote:
    > Reducing malloc() call for performance, please


    Increased performance, reduced portability

    > int** int_matrix_create(int width, int height, char* functionName, int
    > lineNumber) {
    > int** int_matrix;
    > int* cursor;
    > int i;
    > int_matrix = (int** )malloc(width * sizeof(int*) + width * height *
    > sizeof(int));


    Casting malloc's return value is almost always silly (read the FAQ).

    > cursor = (int*)int_matrix + width;


    You mean (int *)(int_matrix + width), ie. (int *)&int_matrix[width].
    Your version would only work if sizeof(int *) == sizeof(int).

    For example, systems with 32-bit int and 64-bit (int *) are not
    uncommon.

    > for (i = 0; i < width; i++) {
    > int_matrix = cursor;
    > cursor += height;
    > }
    > return int_matrix;
    > }


    If int is 64-bit with 64-bit alignment, and int* is 32-bit, and
    width is odd, then you get undefined behaviour.

    I'd suggest at least using 2 mallocs: one for the int * array
    and one for the ints.

    Also you have allocated in column-major form whereas C arrays
    are row-major, and your system isn't conducive to re-sizing
    (it would depend on the OP's requirements, whether this is
    important).
    Old Wolf, Sep 9, 2004
    #7
  8. On Wed, 8 Sep 2004 15:46:11 +0800, "Andrew Au \(Newsgroup\)"
    <> wrote:

    >Reducing malloc() call for performance, please
    >
    >int** int_matrix_create(int width, int height, char* functionName, int
    >lineNumber) {
    > int** int_matrix;
    > int* cursor;
    > int i;
    > int_matrix = (int** )malloc(width * sizeof(int*) + width * height *
    >sizeof(int));


    The return from malloc is guaranteed to be properly aligned for any
    type of variable, including int* and int.

    > cursor = (int*)int_matrix + width;


    This arithmetic is performed in units of sizeof(int). If sizeof(int)
    is less than sizeof(int*), the value assigned to cursor will not leave
    enough room for the "width" int* that are initialized in the next
    loop.

    > for (i = 0; i < width; i++) {
    > int_matrix = cursor;
    > cursor += height;
    > }
    > return int_matrix;
    >}




    <<Remove the del for email>>
    Barry Schwarz, Sep 9, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    696
    Eric Sosman
    Mar 22, 2005
  2. Fernando Barsoba
    Replies:
    8
    Views:
    450
    Fernando Barsoba
    Nov 10, 2005
  3. Ken
    Replies:
    24
    Views:
    3,856
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    985
    chris
    Oct 28, 2005
  5. Shivanand Kadwadkar
    Replies:
    4
    Views:
    513
    J. J. Farrell
    Dec 28, 2010
Loading...

Share This Page