allocating for an array of pointers

Discussion in 'C Programming' started by Erik S. Bartul, Feb 21, 2004.

  1. lets say i want to fill up a multidimentional array, but i wish to allocate
    memory for it on the fly.

    i assume i declare,
    char **a;

    but how do i allocate memory for the pointers, so i can then allocate to the
    pointers to which those pointers point?

    essentially lets say i during runtime deturmine i must allocate space for 60
    pointers,

    a = malloc(size of(char *) * 60);

    what is this in effect allocating for?

    and subsequently when i wish to allocate memory for a pointer to which one
    of the pointers point,

    a[0] = malloc(128); /* to allocate to the first pointer in the array of
    pointers, 128 bytes */

    im probably overlooking something very simple, and it comes to mind that ive
    been in this pickle awhile back... i just cant seem to remember what that
    solution was. and i have read over the C faq.
     
    Erik S. Bartul, Feb 21, 2004
    #1
    1. Advertising

  2. Erik S. Bartul

    David Rubin Guest

    Erik S. Bartul wrote:
    > lets say i want to fill up a multidimentional array, but i wish to allocate
    > memory for it on the fly.
    >
    > i assume i declare,
    > char **a;
    >
    > but how do i allocate memory for the pointers, so i can then allocate to the
    > pointers to which those pointers point?


    int n, m, i;

    /* read n and m */
    a = malloc(n * sizeof *a); /* allocates n char* (type of *a) */
    for(i=0; i < n; i++){
    /* allocates m char (type of *a) */
    a = malloc(m * sizeof *a);
    }

    This allocates n x m chars.

    HTH,

    /david

    --
    "As a scientist, Throckmorton knew that if he were ever to break wind in
    the echo chamber, he would never hear the end of it."
     
    David Rubin, Feb 21, 2004
    #2
    1. Advertising

  3. i appologize for the hasty post, the allocation method i was using worked
    fine... it was an error elsewhere in the code. i should have debugged for
    awhile before assuming i messed up on the allocation :p
     
    Erik S. Bartul, Feb 21, 2004
    #3
  4. Erik S. Bartul

    Lewis Bowers Guest

    "Erik S. Bartul" wrote:

    > lets say i want to fill up a multidimentional array, but i wish to allocate
    > memory for it on the fly.
    >
    > i assume i declare,
    > char **a;
    >
    > but how do i allocate memory for the pointers, so i can then allocate to the
    > pointers to which those pointers point?
    >
    > essentially lets say i during runtime deturmine i must allocate space for 60
    > pointers,
    >
    > a = malloc(size of(char *) * 60);
    >
    > what is this in effect allocating for?
    >
    > and subsequently when i wish to allocate memory for a pointer to which one
    > of the pointers point,
    >
    > a[0] = malloc(128); /* to allocate to the first pointer in the array of
    > pointers, 128 bytes */
    >
    > im probably overlooking something very simple, and it comes to mind that ive
    > been in this pickle awhile back... i just cant seem to remember what that
    > solution was. and i have read over the C faq.


    Have you read and comprehended faq question 6.16?
    It is located at: http://www.eskimo.com/~scs/C-faq/q6.16.html

    This question does a good job on explaining various ways of allocating a
    two-dimensional array.
     
    Lewis Bowers, Feb 21, 2004
    #4
    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. Paminu

    Allocating space for array of pointers?

    Paminu, Feb 9, 2006, in forum: C Programming
    Replies:
    3
    Views:
    456
    Paminu
    Feb 9, 2006
  2. Paminu

    Allocating space for array of pointers?

    Paminu, Feb 9, 2006, in forum: C Programming
    Replies:
    6
    Views:
    763
    Mark McIntyre
    Feb 11, 2006
  3. dev_15
    Replies:
    5
    Views:
    525
    Default User
    Nov 15, 2007
  4. Rakesh Kumar
    Replies:
    5
    Views:
    694
    James Kanze
    Dec 21, 2007
  5. bintom
    Replies:
    7
    Views:
    764
    Ian Collins
    Dec 2, 2010
Loading...

Share This Page