Question about memory allocation

C

cyberdude

Hi,

I defined A as an array in this way:

Dim A() as string

It is an array with unspecified array size.

Somewhere in the programme, I define the array size like this:

ReDim A(10)

When I redim, does it mean some memory is immediately allocated for
this array or does memory is allocated for this array ONLY when I
define the elements as well, like, for example,

ReDim A(10)
A(1)="apple"
A(2)="orange"
.....

?

Thanks.

Mike
 
T

Tony Jollans

Without going into a lot of detail, *some* memory is allocated (for the
array structure, if you like) when you do the ReDim (some (4 bytes) has
already been allocated by the original Dim) but more will be allocated when
values are assigned to the strings because it is only at that point that the
system knows how much is needed.

What kind of processing are you doing for this to be significant?
 
C

cyberdude

Hi Tony,

Thank you for the reply.

My process is a ReDim process which requires a lot of computer memory
as the array sizes are big. I have three arrays: array A, array B,
array C that hold strings. Array A and array B contain no null
elements. Array C contains a lot of null elements (that is, for
example, C(1)=""). I worried if a lot of null elements occupy a lot
of computer memory. This may slow down the computation as my computer
has very limited memory.

Mike
 
T

Tony Jollans

If it's really significant (which, to be honest, I doubt) you could save a
little by assigning your empty strings to be vbNullString - but I think
that's what they'll be by default anyway unless you are explicitly assigning
empty strings to them.

--
Enjoy,
Tony

Hi Tony,

Thank you for the reply.

My process is a ReDim process which requires a lot of computer memory
as the array sizes are big. I have three arrays: array A, array B,
array C that hold strings. Array A and array B contain no null
elements. Array C contains a lot of null elements (that is, for
example, C(1)=""). I worried if a lot of null elements occupy a lot
of computer memory. This may slow down the computation as my computer
has very limited memory.

Mike
 
K

Karl E. Peterson

Tony said:
If it's really significant (which, to be honest, I doubt) you could save a
little by assigning your empty strings to be vbNullString - but I think
that's what they'll be by default anyway unless you are explicitly assigning
empty strings to them.

Yeah, new, unassigned Strings are, by definition.

This can be tested for with:

If StrPtr(X$) = 0 Then

In this particular case, I would hazard a guess that each empty/unassigned array
element is still occupying four bytes for the null pointer, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top