Board index » cppbuilder » resizing array

resizing array


2003-08-31 06:14:13 AM
cppbuilder76
I'm so sorry to ask such a stupid question. (I know that; there is no stupid
question but there are stupid answers, so let say novice question).
I have a structure of the form:
AnsiString myarray[10][12];
I want to resize the array dynamically. I tried realloc but I could not
success. What should I do?
Thanks...
Huseyin Candan
 
 

Re:resizing array

"Huseyin Candan" < XXXX@XXXXX.COM >wrote:
Quote
[...] I have a structure of the form: AnsiString myarray[10][12];
I want to resize the array dynamically.
Use a dynamic array:
// define a 2d array of ansi strings
DynamicArray< DynamicArray < AnsiString>>My2DArray;
// allocate 10 'rows'
My2DArray.Length = 10;
// allocate 12 'cols' for each 'row'
// note that each row may be variable size of 'cols'
for( int x = 0; x < My2DArray.Length; ++x ) My2DArray[x].Length = 12;
// add and extra 'row'
My2DArray.Length++;
// add an extra 'col' to 'row' 1
My2DArray[ 1 ].Length++;
// deallocate
for( int x = 0; x < My2DArray.Length; ++x ) My2DArray[x].Length = 0;
My2DArray.Length = 0;
~ JD
 

Re:resizing array

"Huseyin Candan" < XXXX@XXXXX.COM >writes:
Quote
I have a structure of the form:
AnsiString myarray[10][12];
I want to resize the array dynamically. I tried realloc but I could not
success. What should I do?
You can't resize an array defined like this.
What you can do is use a class whose instances behave as if they were arrays,
but which manages its interna dynamically, such as std::vector. E.g.:
std::vector< std::vector<AnsiString>>myarray;
 

{smallsort}