Not fully comprehending arrays and dynamic memory.

Discussion in 'C Programming' started by Mark Healey, Apr 13, 2006.

  1. Mark Healey

    Mark Healey Guest

    I'd like to allocate some memory for a two dimensional array.

    The problem is that whenever I try to use pointer arithmetic I screw up.
    I just want to use conventional array type statements to get at the memory.

    Is there a way to do this?

    --
    Mark Healey
    marknews(at)healeyonline(dot)com
    Mark Healey, Apr 13, 2006
    #1
    1. Advertising

  2. Mark Healey <> writes:
    > I'd like to allocate some memory for a two dimensional array.

    [...]

    <http://www.c-faq.com/aryptr/dynmuldimary.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, Apr 13, 2006
    #2
    1. Advertising

  3. Mark Healey

    Ben C Guest

    On 2006-04-13, Mark Healey <> wrote:
    > I'd like to allocate some memory for a two dimensional array.
    >
    > The problem is that whenever I try to use pointer arithmetic I screw up.
    > I just want to use conventional array type statements to get at the memory.
    >
    > Is there a way to do this?


    It can be done, but it's a bit tricky, and even easier to screw up I
    would say.

    You can allocate one like this:

    int (*d)[3][3] = malloc(sizeof *d);

    And use it like this:

    (*d)[2][1] = 100;

    ...

    free(d);

    etc.

    It might be easier to use a typedef:

    typedef int matrix_t[3][3];
    matrix_t *m = malloc(sizeof *m);

    ...

    (*m)[2][1] = 100;

    ...

    free(m);

    With this way of doing it both d and m are pointers to a single block of
    memory, which only has to be freed once. Other solutions involve making
    arrays of pointers (one pointer for each row), and potentially calling
    malloc to allocate each row. It's important to be clear which you're
    doing and to call free the right number of times.
    Ben C, Apr 13, 2006
    #3
  4. Groovy hepcat Ben C was jivin' on 13 Apr 2006 09:45:02 GMT in
    comp.lang.c.
    Re: Not fully comprehending arrays and dynamic memory.'s a cool scene!
    Dig it!

    >On 2006-04-13, Mark Healey <> wrote:
    >> I'd like to allocate some memory for a two dimensional array.
    >>
    >> The problem is that whenever I try to use pointer arithmetic I screw up.
    >> I just want to use conventional array type statements to get at the memory.
    >>
    >> Is there a way to do this?

    >
    >It can be done, but it's a bit tricky, and even easier to screw up I
    >would say.
    >
    >You can allocate one like this:
    >
    > int (*d)[3][3] = malloc(sizeof *d);
    >
    >And use it like this:
    >
    > (*d)[2][1] = 100;


    Or, even easier, like this:

    #define ROWS 3
    #define COLS 3
    int (*d)[COLS] = malloc(ROWS * sizeof *d);
    .... /* Checking for errors, etc. */
    d[2][1] = 100;

    Or, slightly trickier (because you have to handle multiple
    allocations and deallocations), like so:

    #define ROWS 3
    #define COLS 3
    int i;
    int **d = malloc(ROWS * sizeof *d);
    .... /* Checking for errors, etc. */
    for(i = 0; i < ROWS; i++)
    {
    d = malloc(COLS * sizeof *d);
    ... /* Checking for errors, etc. */
    }

    d[2][1] = 100;

    Or, even better still, simpler (because there are only two
    allocations):

    #define ROWS 3
    #define COLS 3
    int i;
    int **d = malloc(ROWS * sizeof *d);
    .... /* Checking for errors, etc. */
    *d = malloc(ROWS * COLS * sizeof *d);
    .... /* Checking for errors, etc. */
    for(i = 1; i < ROWS; i++)
    {
    d = d[i - 1] + COLS;
    }

    d[2][1] = 100;

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Apr 16, 2006
    #4
  5. Mark Healey

    Ben C Guest

    On 2006-04-16, Peter "Shaggy" Haywood <> wrote:
    > Groovy hepcat Ben C was jivin' on 13 Apr 2006 09:45:02 GMT in
    > comp.lang.c.
    > Re: Not fully comprehending arrays and dynamic memory.'s a cool scene!
    > Dig it!


    >>On 2006-04-13, Mark Healey <> wrote:
    >>> I'd like to allocate some memory for a two dimensional array.
    >>>
    >>> The problem is that whenever I try to use pointer arithmetic I screw up.
    >>> I just want to use conventional array type statements to get at the memory.
    >>>
    >>> Is there a way to do this?

    >>
    >>It can be done, but it's a bit tricky, and even easier to screw up I
    >>would say.
    >>
    >>You can allocate one like this:
    >>
    >> int (*d)[3][3] = malloc(sizeof *d);
    >>
    >>And use it like this:
    >>
    >> (*d)[2][1] = 100;


    > Or, even easier, like this:
    >
    > #define ROWS 3
    > #define COLS 3
    > int (*d)[COLS] = malloc(ROWS * sizeof *d);
    > ... /* Checking for errors, etc. */
    > d[2][1] = 100;


    This is certainly nicer, and makes sense because after all when you
    allocate a one-dimensional array of N ints, you usually do it with:

    int *p = malloc(N * sizeof *p);

    not with:

    int (*p)[N] = malloc(sizeof *p);

    > Or, slightly trickier (because you have to handle multiple allocations
    > and deallocations), like so: [...]


    I would say it's better to avoid multiple allocations except when you
    need them (when the rows are different lengths, say).
    Ben C, Apr 16, 2006
    #5
    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. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
  2. Replies:
    0
    Views:
    314
  3. Replies:
    9
    Views:
    314
  4. Viren

    Fully multiple, editable dynamic datagrid.

    Viren, Oct 20, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    133
    Alvin Bruney
    Oct 20, 2003
  5. fir

    fully fast resizable arrays in c (c2)

    fir, Mar 18, 2013, in forum: C Programming
    Replies:
    21
    Views:
    373
Loading...

Share This Page