dynamically allocating a 2d array

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Sep 10, 2005.

  1. Guest

    What is the correct way of dynamically allocating a 2d array ?

    I am doing it the following way. Is this correct ?

    #include <stdlib.h>
    int main(void)
    {
    int (*arr)(3);
    arr = malloc(sizeof(*arr) * 4);
    /* I want to dynamically allocate
    int arr[4][3] */

    arr[2][3] = 100; /* Can I initialize the 3rd column of
    2nd row in this manner ? */
    }

    Thanx in advance for any help ...
     
    , Sep 10, 2005
    #1
    1. Advertising

  2. wrote:
    > What is the correct way of dynamically allocating a 2d array ?


    There are a few correct ways.

    > I am doing it the following way. Is this correct ?
    >
    > #include <stdlib.h>
    > int main(void)
    > {
    > int (*arr)(3);


    This is wrong, it should be 'int (*arr)[3]' (pointer to array of three
    ints). Even better, use a typedef: "typedef int row[3];".

    > arr = malloc(sizeof(*arr) * 4);
    > /* I want to dynamically allocate
    > int arr[4][3] */
    >
    > arr[2][3] = 100; /* Can I initialize the 3rd column of
    > 2nd row in this manner ? */


    This code is correct.

    An alternative way which works even it the presence of variable dimensions
    is to allocate an array of size X*Y and then compute the index in the 1D
    array from the position in the 2D array and its size.

    Uli
     
    Ulrich Eckhardt, Sep 10, 2005
    #2
    1. Advertising

  3. wrote:
    >What is the correct way of dynamically allocating a 2d array ?


    This is a FAQ, see section 6.16 in
    http://www.faqs.org/faqs/C-faq/faq/

    I suggest you read the whole thing, once you got it.

    >I am doing it the following way. Is this correct ?


    At least you could've tried to compile it before you posted.

    >#include <stdlib.h>
    >int main(void)
    >{
    > int (*arr)(3);

    syntax error
    <snip>

    Rhetorical question: What would you do with a pointer to a function
    taking constant 3 returning int, anyway?

    Best regards.
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc.
     
    Irrwahn Grausewitz, Sep 10, 2005
    #3
  4. Guest

    Irrwahn Grausewitz wrote:
    > wrote:
    > >What is the correct way of dynamically allocating a 2d array ?

    >
    > This is a FAQ, see section 6.16 in
    > http://www.faqs.org/faqs/C-faq/faq/
    >
    > I suggest you read the whole thing, once you got it.
    >
    > >I am doing it the following way. Is this correct ?

    >
    > At least you could've tried to compile it before you posted.
    >
    > >#include <stdlib.h>
    > >int main(void)
    > >{
    > > int (*arr)(3);

    > syntax error
    > <snip>
    >
    > Rhetorical question: What would you do with a pointer to a function
    > taking constant 3 returning int, anyway?
    >

    Sorry, it was a typo. I meant int (*arr)[3].
     
    , Sep 10, 2005
    #4
  5. Nerox Guest

    Stick to the following rule:
    -Generally, an array name in an expression is evaluated as a pointer to
    its first element.

    Thus,
    int array[Y][X] is a 2d array, an array of Y arrays of X integers.
    You can write its dynamic counterpart as follows:
    int (*array)[X] which is a pointer to an array of X integers.
    And then allocate it dynamically via malloc:
    array = malloc(sizeof(*array) * Y );
     
    Nerox, Sep 10, 2005
    #5
  6. sahu Guest

    i have a soln. just try it out it might work:
    for dynamically allocating a 2d array of let it be something like
    a[5][8].
    int **t;
    t=(int **)malloc(5*2);
    for(i=0;i<5;i++)
    {
    *(t+i)=(int *)malloc(8*2);
    }
     
    sahu, Sep 10, 2005
    #6
  7. sahu wrote on 11/09/05 :
    > i have a soln. just try it out it might work:
    > for dynamically allocating a 2d array of let it be something like
    > a[5][8].
    > int **t;
    > t=(int **)malloc(5*2);


    Why 5 * 2 ? What is the cast made for ?

    int **t; = malloc (5 * sizeof *t);

    if (t != NULL)
    {

    > for(i=0;i<5;i++)


    Better to define some abstraction for this hard coded '5' ...

    > {
    > *(t+i)=(int *)malloc(8*2);


    why 8 * 2 ? Be simple:

    t = malloc (8 * sizeof *t);

    > }


    The generic expression to allocate a variable (n = 1) or an array of n
    variables of type T is:

    T *p = malloc (n * sizeof *p);

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Sep 10, 2005
    #7
  8. "Nerox" <> writes:
    > Stick to the following rule:
    > -Generally, an array name in an expression is evaluated as a pointer to
    > its first element.


    Unless it's the argument of a sizeof or unary "&" operator.

    --
    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 11, 2005
    #8
  9. Old Wolf Guest

    Ulrich Eckhardt wrote:
    > wrote:
    >> What is the correct way of dynamically allocating a 2d array ?

    >
    > There are a few correct ways.
    >
    >> I am doing it the following way. Is this correct ?
    >>
    >> #include <stdlib.h>
    >> int main(void)
    >> {
    >> int (*arr)(3);

    >
    > This is wrong, it should be 'int (*arr)[3]' (pointer to array of
    > three ints). Even better, use a typedef: "typedef int row[3];".
    >
    >> arr = malloc(sizeof(*arr) * 4);
    >>
    >> arr[2][3] = 100; /* Can I initialize the 3rd column of
    >> 2nd row in this manner ? */

    >
    > This code is correct.


    arr[2][3] is the 4th column of the 3rd row. Arrays index
    from 0 in C. Depending on how you read the Standard,
    accessing arr[2][3] either causes undefined behaviour, or
    ends up accessing arr[3][0].
     
    Old Wolf, Sep 11, 2005
    #9
  10. John Bode Guest

    wrote:
    > What is the correct way of dynamically allocating a 2d array ?
    >
    > I am doing it the following way. Is this correct ?
    >
    > #include <stdlib.h>
    > int main(void)
    > {
    > int (*arr)(3);


    I'm assuming you meant

    int (*arr)[3];

    > arr = malloc(sizeof(*arr) * 4);
    > /* I want to dynamically allocate
    > int arr[4][3] */


    I'm assuming you meant

    int arr[3][4]

    >
    > arr[2][3] = 100; /* Can I initialize the 3rd column of
    > 2nd row in this manner ? */
    > }
    >
    > Thanx in advance for any help ...


    Well, it's different (never thought to do it that way before). It
    appears to work, though. However, this method requires one dimension
    be fixed. If you want to allocate a 2D array of int and be able to
    specify both dimensions dynamically, here's one method:

    #include <stdlib.h>

    int **new2DIntArray(size_t rows, size_t cols)
    {
    int **arr;
    int mallocError = 0;
    size_t lastRow = 0;

    arr = malloc(sizeof arr[0] * rows);
    if (arr)
    {
    size_t i;
    for (i = 0; i < rows && !mallocError; i++)
    {
    arr = malloc(sizeof arr[0] * cols);
    if (arr)
    {
    size_t j;
    lastRow = i;
    for (j = 0; j < cols; j++)
    {
    arr[j] = 0;
    }
    }
    else
    {
    mallocError = 1;
    break;
    }
    }

    if (mallocError)
    {
    size_t i;
    for (i = lastRow; i >= 0; i--)
    {
    free(arr);
    }
    free(arr);
    arr = NULL;
    }
    }

    return arr;
    }

    int main(void)
    {
    int **arr1 = new2DIntArray(4,3); /* int arr[4][3] */
    int **arr2 = new2DIntArray(3,4); /* int arr[3][4] */
    /* etc. */

    /* do something with arrays */

    return 0;
    }

    You'll want to add another function to free up the arrays when you're
    done with them, but that should be straightforward (basically it's the
    if(mallocError) branch above).
     
    John Bode, Sep 12, 2005
    #10
  11. On 12 Sep 2005 03:56:48 -0700, "John Bode" <>
    wrote:

    > wrote:
    >> What is the correct way of dynamically allocating a 2d array ?
    >>
    >> I am doing it the following way. Is this correct ?
    >>
    >> #include <stdlib.h>
    >> int main(void)
    >> {
    >> int (*arr)(3);

    >
    >I'm assuming you meant
    >
    > int (*arr)[3];
    >
    >> arr = malloc(sizeof(*arr) * 4);
    >> /* I want to dynamically allocate
    >> int arr[4][3] */

    >
    >I'm assuming you meant
    >
    > int arr[3][4]


    I hope not. He has allocated space for an array of 4 arrays of 3 int
    each.

    >
    >>
    >> arr[2][3] = 100; /* Can I initialize the 3rd column of
    >> 2nd row in this manner ? */


    The third column of the second row is arr[1][2].

    >> }
    >>
    >> Thanx in advance for any help ...

    >
    >Well, it's different (never thought to do it that way before). It
    >appears to work, though. However, this method requires one dimension
    >be fixed. If you want to allocate a 2D array of int and be able to
    >specify both dimensions dynamically, here's one method:
    >
    >#include <stdlib.h>
    >
    >int **new2DIntArray(size_t rows, size_t cols)
    >{
    > int **arr;
    > int mallocError = 0;
    > size_t lastRow = 0;
    >
    > arr = malloc(sizeof arr[0] * rows);
    > if (arr)
    > {
    > size_t i;
    > for (i = 0; i < rows && !mallocError; i++)
    > {
    > arr = malloc(sizeof arr[0] * cols);
    > if (arr)
    > {
    > size_t j;
    > lastRow = i;
    > for (j = 0; j < cols; j++)
    > {
    > arr[j] = 0;
    > }
    > }
    > else
    > {
    > mallocError = 1;
    > break;
    > }
    > }
    >
    > if (mallocError)
    > {
    > size_t i;
    > for (i = lastRow; i >= 0; i--)


    This for loop can never end. size_t is an unsigned type so i will
    always be >= 0.

    > {
    > free(arr);
    > }
    > free(arr);
    > arr = NULL;
    > }
    > }
    >
    > return arr;
    >}
    >
    >int main(void)
    >{
    > int **arr1 = new2DIntArray(4,3); /* int arr[4][3] */
    > int **arr2 = new2DIntArray(3,4); /* int arr[3][4] */
    > /* etc. */
    >
    > /* do something with arrays */
    >
    > return 0;
    >}
    >
    >You'll want to add another function to free up the arrays when you're
    >done with them, but that should be straightforward (basically it's the
    >if(mallocError) branch above).



    <<Remove the del for email>>
     
    Barry Schwarz, Sep 13, 2005
    #11
    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. Chris Lambacher
    Replies:
    0
    Views:
    387
    Chris Lambacher
    Jun 8, 2005
  2. Chris Lambacher
    Replies:
    0
    Views:
    671
    Chris Lambacher
    Jun 8, 2005
  3. Rakesh Kumar
    Replies:
    5
    Views:
    686
    James Kanze
    Dec 21, 2007
  4. bintom
    Replies:
    7
    Views:
    749
    Ian Collins
    Dec 2, 2010
  5. mx
    Replies:
    13
    Views:
    818
    Tim Rentsch
    May 7, 2012
Loading...

Share This Page