Coder's Guild Mailing List

Re: Resizable arrays

Posted by Garfield Benjamin on 1999-03-24

>Is it possible to have a plain old raw array that can be resized on
>the fly ??

   Hmm... well, if you make your own routines of course, anything
   is possible.


>(I imagine so) and if so how would i declare such an array in c++.

   Well, at a  very basic level, you would simply create a new class
   something like...

   class DynamicArray (int numelements)...

   and that class would allow you to type the data and contain
   a pointer to the start of the current memory hunk and the size
   (last element) of the array.

   When an element is referenced in an assignment which is larger
   than the current array size, you would need to allocate a new hunk
   of memory, copy the old array values into the new memory hunk,
   deallocate the previous memory hunk, store the value into the
   element in the reference, then update the current array size.
    
   That is more or less the way any BASIC or interpreted language
   handles it.

Garfield Benjamin    e-mail: gbenjam@xxxxxx.xxx
Website: http://www.sosbbs.com/~gbenjam