String variables

B

Burnnie Holliday

I understand that a string variable size is 10 bytes + string length. Does
this mean that a a variable-length string which happens to have a length of 6
occupies the same amount of memory as a fixed-length string of the same size
(16 bytes), or does Excel assign a memory block capable of suiting a much
larger string when a variable-length string is declared?

I'm asking because I'm dealing with a macro that creates a string array
listing the types of products it has recorded data for, and it seems to be
occupying a surprising amount of processor time.
 
B

Burnnie Holliday

Okay... I don't really feel that was necessary to answer the question.

does:

Dim ThisVar as String
ThisVar = "ABCDEF"

take more memory than this:

Dim ThisVar as String * 6
ThisVar = "ABCDEF"

The entirety of the code in question is 221 lines long, and no, this is not
a direct excerpt. A direct excerpt isn't necessary to answer the question,
since the question is not about code, but how Excel handles variable
declaration.
 
R

Rick Rothstein

See inline comments...

Okay... I don't really feel that was necessary to answer the question.

Well, if you had any reason at all for including the last paragraph in your
original posting, then you are wrong... your actual code is quite material
to helping with that part of your message.

does:

Dim ThisVar as String
ThisVar = "ABCDEF"

take more memory than this:

Dim ThisVar as String * 6
ThisVar = "ABCDEF"

If you really think you need to see an answer to the above question, here is
how Microsoft describes it a this link...

http://msdn.microsoft.com/en-us/library/dd942824.aspx

Declare String Data Intelligently
========================================================
Visual Basic allocates stack and heap memory differently according to the
type of strings you create. By understanding how this works, you can write
more efficient string code. String variables in procedures that are
non-static use space on the computer's stack. Use the following information
to write code that minimizes stack memory usage.

Local fixed-length strings less than or equal to 64 characters use 2 bytes
for each character in the string. They don't use heap memory.

Local fixed-length strings longer than 64 characters use 4 bytes of stack
memory for a pointer to the variable in heap memory and 2 bytes of heap
memory for each character in the string.Local variable-length strings use 4
bytes of stack memory for a pointer to the variable in heap memory, and a
variable amount of heap memory according to the length of the string.

If your code used a large number of fixed-length strings of 64 characters or
less, you can reduce stack usage by changing the strings to local
variable-length strings or making them static fixed-length strings.

The entirety of the code in question is 221 lines long, and no, this is
not
a direct excerpt. A direct excerpt isn't necessary to answer the
question,
since the question is not about code, but how Excel handles variable
declaration.

Again, I have to wonder why you went out of your way to include the last
paragraph in your original posting. Variable memory usage is not where long
processing time comes from... it comes directly from the method of coding
used to process the strings (no matter whether they are fixed or variable in
length).
 

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