Re: arrays of strings and pointers
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 07 Nov 2005 05:24:06 GMT
Dave Thompson <david.thompson1@xxxxxxxxxxxxxxxx> writes:
> On Thu, 27 Oct 2005 21:11:15 GMT, Keith Thompson <kst-u@xxxxxxx>
> wrote:
> <snip>
>> There are at least four different ways to implement a data structure
>> that acts like 2-dimensional array.
> <snip>
>> If you want a variable number of fixed-size arrays, you can declare
>> an array of pointers:
>> int *arr2[10];
>>
>> If you want a fixed number of variable-size arrays, you can declare
>> a pointer to an array:
>> int (*arr3)[10];
>>
> You have the antecedent clauses swapped. (Or both the consequents and
> code, which I assumed is less likely.)
You're right, I goofed. Thanks for catching it.
>> If you want maximum flexibility, you can declare a pointer-to-pointer:
>> int **arr4;
>>
>> For arr2, arr3, and arr4, you have to do your own memory management.
>> For arr4, you have to allocate an array of pointers *and* multiple
>> arrays of int, one for each row of the 2d-array-like data structure.
>>
> For array-of-pointer (2) and pointer-to-pointers (4) you don't have to
> allocate each row separately; you can allocate one big chunk, or even
> several midsized ones, and set pointers into them. But only if you
> don't need to independently change the size(s) of rows that are
> allocated together; and you must either know a priori or keep track of
> which ones are the original allocations (thus) to be free'd. This is
> enough of a pain that I would usually stick with the simple way.
I was assuming a potential need to reallocate the individual chunks,
or to allocate them separately rather than all at once. Certainly you
can sometimes optimize by allocating multiple chunks together, if you
have enough information.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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.
.
- References:
- Re: arrays of strings and pointers
- From: Dave Thompson
- Re: arrays of strings and pointers
- Prev by Date: Re: how to replace a substring in a string using C?
- Next by Date: Re: Segmentation Fault in Craps program
- Previous by thread: Re: arrays of strings and pointers
- Next by thread: Re: manipulate string
- Index(es):
Relevant Pages
|