Dynamic arrays

Login to reply to this topic.
Tue, 2003-12-02 12:20
Joined: 2003-10-14
Forum posts: 15
Hi

Could anyone explain me some things about dynamic arrays ?
I want to create a dynamic array containing CData elements.
The code should look like  :

Code:
CArrayFixSeg<CData>*   fields;
fields = new CArrayFixSeg<CData>( ???) ;

what should I put in place of "Huh"  ? The doc says

Quote
CArrayFixSeg(TInt aGranularity);
(...)
TInt aGranularity : The granularity of the array. This value must be positive otherwise the constructor raises an E32USER-CBase 18 panic.

Yeah ! but what is the granularity ? I thought it was the number of elements we wanted to put in each segment of the array (as it is a segmented arary), but ... the same argument is needed to build a CArrayFixFlat, which is not segmented...
The magic book (Symbian C++ for mobile phones) doesn't help, neither does Symbian's examples (don't work) ...

Thanks

Pierre-Rudolf Gerlach


Tue, 2003-12-02 12:55
Joined: 2003-09-25
Forum posts: 24
Dynamic arrays
Hello,

--------
CArrayFixSeg<CData>*   fields;
fields = new CArrayFixSeg<CData>( Huh) ;
what should I put in place of "Huh" ? The doc says
--------
You should put a no. in place of Huh indicating how much size you require of the array.

Granularity: For dynamic arrays as u must have guessed, the size changes dynamically. Initially you specify some size of the array say X and granularity say Y.

Once at runtime, the array size X is reached, dynamic arrays increase it automatically by Y.

e.g. If you specify the array size of 10 and granularity say 5. When at run time when your array will be full with 10 elements, the size will dynamically be increased by 5. So now the size is 15.

Thus granularity value must be positive for obvious reasons.

Hope itÂ’s clear now.

Cheers
- Sym_Pro
Thu, 2005-08-11 22:40
Joined: 2005-05-19
Forum posts: 45
Re: Dynamic arrays
I am a newbie... but I think the granularity is the size of each element.  Since it is a dynamic array, you would do something like the following:

CArrayFixSeg<CData>*   fields;
fields = new CArrayFixSeg<CData>(sizeof(CData)) ;

then you can call fields.SetReserveL(6) or something like it to reserve space for size CData items.  You can also just call the
AppendL function and add a CData item to the array.

According to the docs, the call to new does not allocate any memory to the array, so you dont have to worry about a memory allocation error, but the call to ReserveL or any other function that adds the item or saves space for the item may leave... as indicated by the L sufix.

"If we knew what it was we were doing, it would not be called research, would it?" - Albert Einstein

Fri, 2005-08-12 04:58
Joined: 2005-02-16
Forum posts: 70
Re: Dynamic arrays
Why not just use an RArray?
Fri, 2005-08-12 15:30
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Dynamic arrays
Quote from: Cryptik
I am a newbie... but I think the granularity is the size of each element.  Since it is a dynamic array, you would do something like the following:

CArrayFixSeg<CData>*   fields;
fields = new CArrayFixSeg<CData>(sizeof(CData)) ;

No, you're wrong: granularity specifies the number of elements by which the array will increase if you consumed the space you initially reserved for the dynamic array.

Example: let's say, your array holds TInts, which is 4-byte long. If the granularity is 5, then the initial size of the array will be 5*4=20 bytes regardless of how many elements are in the array (it can be even zero, 20 bytes will be reserved). If the array already contains 5 elements (that is, space is fully consumed) and you're about to add the 6th one, then the array needs to expand: new space has to be reserved and having granularity 5, it's gonna be another 20 bytes that will be reserved.

But anyway, my question is the same as Miranda's: why don't you use RArrays?

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

Sun, 2005-08-14 14:48
Joined: 2005-03-31
Forum posts: 173
Re: Dynamic arrays
Mon, 2007-07-23 03:29
Joined: 2007-07-23
Forum posts: 1
Re: Dynamic arrays

RArray vs CArray
if (CData` s is less than 640Bytes && Your application INFREQENTLY allocates memory)
{
RArray is probably a better choice
}
if (CData is fixed-size && Your application INFREQENTLY allocates memory)
{
Use CArrayFixFlat
}
if(CData is NOT fixed-size && Your application INFREQENTLY allocates memory)
{
Use CArrayVarFlat
}
if(CData is fixed-size && Your application FREQENTLY allocates memory)
{
Use CArrayFixSeg
}
if(CData is NOT fixed-size && Your application FREQENTLY allocates memory)
{
Use CArrayVarSeg
}

Mon, 2007-07-23 07:50
Joined: 2004-12-03
Forum posts: 276
Re: Dynamic arrays

>>But anyway, my question is the same as Miranda's: why don't you use RArrays?

I agree with tote and Miranda that we should use RArray instead of CArrayFix* ....

But I suggest not to use RArray if you want to store "C" type classes, such as CData. You should use RPointerArray. The reason is that RArray does not call copy-constructor of your class, but it will do a bit-wise copy. Therefore RArray is useful for "T" type classes. And for "C" type classes use RPointerArray.

Regards,
Dennis


Today is a gift by GOD, that's why it is called the present.

  • Login to reply to this topic.