Efficient use of strings (Dim, Redim, and all that) 
Author Message
 Efficient use of strings (Dim, Redim, and all that)

On Wed, 26 Jul 1995 in article <Efficient use of strings (Dim, Redim, and

Quote:
>' Example 1

>Dim s As String        ' Unallocated String buffer
>Do
>s = s & Comm1.Input
>Loop Until s = Chr$(0)

This will allocate the string in VB's DSegment I think it's
called. It's the global string space that's limited to 32k.

Quote:
>' Example 2

>Dim s As String * 128  ' Fixed-length String buffer
>Do
>s = s & Comm1.Input
>Loop Until s = Chr$(0)

This will allocate the string to it's own memory space and
will be limited to 128 bytes.

Quote:
>' Example 3

>Dim s As String
>s = String$(128, 0)    ' Allocated String buffer
>Do
>s = s & Comm1.Input
>Loop Until s = Chr$(0)

Actually, this does not set the length of the string. All it
does is set the first 128 characters to null (chr$(0)).

Quote:
>In the first example, s is declared and appended with each new char
received.
>The size of s is never declared. The second example is the same, but
declares
>s as a fixed-length string. The third example allocates s to be 128 bytes
in  
>length and all bytes set to 0.
>Which "s = s & Comm1.Input" statement is more efficient (i.e. has less  
>overhead)?  

Hmmm, I believe they will both run at practicaly the same speed
since we are talking about pure memory here. Any difference in
speed will not be noticable by a human (no matter how many times
you try to run that loop).

Quote:
>What happens if I overrun s' 128 byte limit in examples 2 and 3?  

Example 2 will have a string overrun error (don't remember ACTUAL
vb error, but it's something like that). The third example is the
same as the first example, in essence. Setting the first 128 bytes
to something doesn't set the size of the variable. The first and
third example can take up to 32k of characters (depending upon
how many other strings are currently allocated).

Quote:
>And what happens to the size of s in each example if I use the statement:

>s = ""

>to clear the contents of s?

If examples 1 and 3, s will equal "". In example 2, s will equal
128 spaces.

--
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

--------------------------------------------------------------------------
Programmer/Analyst - Dow Jones & Co., Inc.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Mon, 12 Jan 1998 03:00:00 GMT  
 Efficient use of strings (Dim, Redim, and all that)

Quote:

> ' Example 3

>     Dim s As String
>     s = String$(128, 0)    ' Allocated String buffer
>     Do
>         s = s & Comm1.Input

Here, s will read
"00000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000" + whatever you get
from the comm-port. That wasn't your intention, was it?

Jens



Mon, 12 Jan 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Newbie Question: Dim A() as String /Dim A as String()

2. dim string to dim form

3. Efficient converting from byte-string to word-string

4. Dim and ReDim?

5. Dim and ReDim

6. Dim and ReDim

7. dim and redim allocate memory?

8. ReDim a 2 dim. array

9. Efficient way of string manipulation

10. Efficient string concatination

11. Efficient string concatenation

12. Efficient handling of strings from serial port device?

 

 
Powered by phpBB® Forum Software