
array pointer/pointer array
[Top-posting undone.]
Quote:
> On Sat, 5 Apr 2003 03:54:07 -0800, "Lawrence"
> >Dear all,
> >int y;
> >double *ptr[30][6];
> >For me, it is an pointer array with 180 pointers in it.
Yes, that is a correct reading of the above code.
Quote:
> >I was told that * means it is an array of y length.
Nothing in the posted code suggests that.
Quote:
> >Does it mean taht *ptr[30][6] can be ptr[y][30][6]
No.
Quote:
> >or ptr [30][6][y]?
Not quite. That expression references past
the bounds of the array as defined.
However, if the pointer ptr[I][j] is indexed
via y, (as ptr[I][j][y]), where at least (y+1)
doubles were allocated at the memory block
to which ptr[I][j] points, (implying that a
new[] or other allocation was done previously),
then that would work.
Quote:
> >If it can represent a 3D array, how can I allocate
> >memeory to *ptr[30][6] and make it to ptr[y][30][6]?
You would be better off with alternatives as I
have mentioned below. If you are determined to
do this with bare-metal C'ish code, then you
should study up on the relationship between
array expressions and pointer arithmetic. That
is too vast a topic to repeat here when it is
covered so well in C references.
Quote:
> Lawrence,
> I am not entirely sure what you're wanting to accomplish here, so let
> me re-state my interpretation of your question and, if I get it wrong,
> please tell me, and I will try and correct the answer. :)
> First, it sonds like you are wanting to create a 3-dimensional array,
> but you want to use a variable for at least one of the dimensions, so
> that you can create an array of an indeterminate size at run-time?
I imagine it becomes determinate at runtime. <g>
Quote:
> I
> am assuming this becuase your use of int y for one of the dimensions
> in your example.
> C++ uses two kinds of storage: Static and dynamic. Static storage is
> storage that C++ knows about and sets up at compile time. Dynamic
> storage is storage that C++ doesn't know about at compile time, but
> allocates instead "on the fly" at run time as its needed. Arrays are
> an example of statically allocated storage. That is, C++ needs to
> know exactly how much space the array will take up when the program
> compiles. For this reason, you cannot have an array of a variable
> length. You would create a three dimensional array of pointers like
> this:
> double *ptrArray[10][30][60]; // Array of 1800 pointers to double's
> You do have some options if you really "need" to create an array of
> variable lenght. One thing you could do is to create a class (call it
> a "node" or something) and make a linked list out of it. Include a
> static member of type node* called pRoot, and a non-static member
> pRoot* pNext. Overload the [] operator to return a reference to the
> requested node.
Warning: Do not use this technique unless
you are quite sure that O(n) access is going
to be satisfactory for your application.
Quote:
> Use the new() operator to add new noes to the list.
> If you want help with this technique. let me know. It's quite easy
> once you have the hang of it.
> Alternately, if you are using MS Visual C++ and MFC, you can use
> something called the COleSafeArray. Safe Arrays are basically arrays
> of COleVAriant's and they can be of an arbitrary type and length.
I would avoid SAFEARRAY and its kin unless
you need COM interoperability.
A few other alternatives are:
- std::vector, nested as necessary to get
the required number of dimensions.
- std::valarray for the fixed dimensions
- boost::multi_array (at www.boost.org)
The latter is the easiest once the fixed
overhead of starting to use the Boost
library is incurred. That will yield many
other benefits over time.
Quote:
> Wow.... I sure hope I answered your question, but again if not,
> please let me know and I'll try again. :)
> -- Jim
--
-Larry Brasfield
(address munged, s/sn/h/ to reply)