allocating for an array of pointers

E

Erik S. Bartul

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.
 
D

David Rubin

Erik said:
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
 
E

Erik S. Bartul

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
 
L

Lewis Bowers

Erik S. Bartul said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top