Re: arrays of strings and pointers



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



Relevant Pages

  • Re: Maximum Size of Byte Array
    ... I do not allocate 400 MB in every case of course. ... The idea to trade the image in chunks is very good. ... Software Developer ... But I need to use this big array. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Cons cell archaic!?
    ... from s-expression or XML or other syntax you keep the bloated array ... For using vectors to emulate lists that ... Allocate 2, move 1 element: ... What do you think of that algorithm? ...
    (comp.lang.lisp)
  • Storing/Retrieving TYPEs with ALLOCATABLE components (TR) (long)
    ... tBrd, including array descriptor of tEn )). ... Without previous DEALLOCATE, the allocate line fails at run time with message ... the fact that I'm loading an invalid descriptor tBrd%tEn from the file... ... status (which is not possible according to Standard, but then BINARY files ...
    (comp.lang.fortran)
  • Re: Storing the size of an array in the structure itself
    ... >> I think every C programmer can relate to the frustrations that malloc ... >> the size of an array must be stored separately to be a nightmare. ... is anything more than just that - a chunk of memory. ... > Otherwise you couldn't tell it how much to allocate. ...
    (comp.lang.c)
  • Re: Interoperability with C
    ... have a procedure return a variable size array that you didn't allocate ... Then allocate an array ... with 'sufficient' buffer and the routine stores the data. ...
    (comp.lang.fortran)