Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > allocating for an array of pointers

Reply
Thread Tools

allocating for an array of pointers

 
 
Erik S. Bartul
Guest
Posts: n/a
 
      02-21-2004
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(12; /* 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.


 
Reply With Quote
 
 
 
 
David Rubin
Guest
Posts: n/a
 
      02-21-2004
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[i]) */
a[i] = malloc(m * sizeof *a[i]);
}

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

 
Reply With Quote
 
 
 
 
Erik S. Bartul
Guest
Posts: n/a
 
      02-21-2004
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


 
Reply With Quote
 
Lewis Bowers
Guest
Posts: n/a
 
      02-21-2004


"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(12; /* 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically allocating an array of pointers bintom C++ 7 12-02-2010 01:01 AM
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
Allocating array of pointers to structs dev_15 C++ 5 11-15-2007 07:56 PM
Allocating space for array of pointers? Paminu C Programming 6 02-11-2006 12:38 PM
Allocating space for array of pointers? Paminu C Programming 3 02-09-2006 07:55 PM



Advertisments