If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Strings and memory management

Hi,

when one combines strings
e.g.,

s="a" & " B" & "C"

are temporary strings made for each addition
i.e., "a B" and then "a BC", or does VB look at the lengths of
all parts, and reserve enough space to copy all into the final
string such that temporaries are not required?

Re: Strings and memory management

In P-Code I'm pretty sure that vb does each as a seperate action, however I'm not sure how that gets optimized after it's compiled,
I'll have to compile, put debug break and take a look. I'll get back to you on that later. If you however do that with variables,
I.E.:

.......
A = "a"
B = " B"
C = "C"

Str = a + b + c

it will always do it in three seperate operations.

Oren

"Brian Leung" <bleung@zoo.cam.ac.uk> wrote in message news:3965ded2$1@news.devx.com...
>
> Hi,
>
> when one combines strings
> e.g.,
>
> s="a" & " B" & "C"
>
> are temporary strings made for each addition
> i.e., "a B" and then "a BC", or does VB look at the lengths of
> all parts, and reserve enough space to copy all into the final
> string such that temporaries are not required?
>
>
> Brian
>

Re: Strings and memory management

In P-Code I'm pretty sure that vb does each as a seperate action, however I'm not sure how that gets optimized after it's compiled,
I'll have to compile, put debug break and take a look. I'll get back to you on that later. If you however do that with variables,
I.E.:

.......
A = "a"
B = " B"
C = "C"

Str = a + b + c

it will always do it in three seperate operations.

Oren

"Brian Leung" <bleung@zoo.cam.ac.uk> wrote in message news:3965ded2$1@news.devx.com...
>
> Hi,
>
> when one combines strings
> e.g.,
>
> s="a" & " B" & "C"
>
> are temporary strings made for each addition
> i.e., "a B" and then "a BC", or does VB look at the lengths of
> all parts, and reserve enough space to copy all into the final
> string such that temporaries are not required?
>
>
> Brian
>

Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
news:3965ded2$1@news.devx.com...
>
> Hi,
>
> when one combines strings
> e.g.,
>
> s="a" & " B" & "C"
>
> are temporary strings made for each addition
> i.e., "a B" and then "a BC", or does VB look at the lengths of
> all parts, and reserve enough space to copy all into the final
> string such that temporaries are not required?
>
>
> Brian
>

Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
news:3965ded2$1@news.devx.com...
>
> Hi,
>
> when one combines strings
> e.g.,
>
> s="a" & " B" & "C"
>
> are temporary strings made for each addition
> i.e., "a B" and then "a BC", or does VB look at the lengths of
> all parts, and reserve enough space to copy all into the final
> string such that temporaries are not required?
>
>
> Brian
>

Re: Strings and memory management

Hi,

I don't mind appending to the same string...unless by doing so, VB must recopy
the entire
string to allow the increasing size of the string. If that is true, is there
a way around it...such as
finding the total length of all strings to be appended and reserving a fixed
length string of appropriate size.
I assume recopying is more expensive than finding the length of strings.
Are there other methods to reserve adequate space (i.e., ones that retain
the ability to dynamically resize)?

Thanks,

Brian

"Jonathan Wood" <jwood@softcircuits.com> wrote:
>As a general rule, VB makes temporary strings like crazy.
>
>However, know what API functions are available for working with BSTRs, if
I
>had to hazzard a guess, I'd say VB does the following in this case.
>
>+Copies "a" to a new string
>+Appends " B" to that same string
>+Appends "C" to that same string
>+Assigns that string to s
>
>--
>Jonathan Wood
>SoftCircuits Programming
>http://www.softcircuits.com
>
>Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
>news:3965ded2$1@news.devx.com...
>>
>> Hi,
>>
>> when one combines strings
>> e.g.,
>>
>> s="a" & " B" & "C"
>>
>> are temporary strings made for each addition
>> i.e., "a B" and then "a BC", or does VB look at the lengths of
>> all parts, and reserve enough space to copy all into the final
>> string such that temporaries are not required?
>>
>>
>> Brian
>>
>
>

Re: Strings and memory management

Hi,

I don't mind appending to the same string...unless by doing so, VB must recopy
the entire
string to allow the increasing size of the string. If that is true, is there
a way around it...such as
finding the total length of all strings to be appended and reserving a fixed
length string of appropriate size.
I assume recopying is more expensive than finding the length of strings.
Are there other methods to reserve adequate space (i.e., ones that retain
the ability to dynamically resize)?

Thanks,

Brian

"Jonathan Wood" <jwood@softcircuits.com> wrote:
>As a general rule, VB makes temporary strings like crazy.
>
>However, know what API functions are available for working with BSTRs, if
I
>had to hazzard a guess, I'd say VB does the following in this case.
>
>+Copies "a" to a new string
>+Appends " B" to that same string
>+Appends "C" to that same string
>+Assigns that string to s
>
>--
>Jonathan Wood
>SoftCircuits Programming
>http://www.softcircuits.com
>
>Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
>news:3965ded2$1@news.devx.com...
>>
>> Hi,
>>
>> when one combines strings
>> e.g.,
>>
>> s="a" & " B" & "C"
>>
>> are temporary strings made for each addition
>> i.e., "a B" and then "a BC", or does VB look at the lengths of
>> all parts, and reserve enough space to copy all into the final
>> string such that temporaries are not required?
>>
>>
>> Brian
>>
>
>

Re: Strings and memory management

Hi Brian,

Well that's what VB does. The alternartive is to pre-allocate a string the
size of the sum of the lengths of the substrings and then use the Mid$
statement to assign the sub strings to the appropiate places in the string.

Bill.

"Brian Leung" <bleung@zoo.cam.ac.uk> wrote in message
news:39668f21$1@news.devx.com...
>
> Hi,
>
> I don't mind appending to the same string...unless by doing so, VB must
recopy
> the entire
> string to allow the increasing size of the string. If that is true, is
there
> a way around it...such as
> finding the total length of all strings to be appended and reserving a
fixed
> length string of appropriate size.
> I assume recopying is more expensive than finding the length of strings.
> Are there other methods to reserve adequate space (i.e., ones that retain
> the ability to dynamically resize)?
>
> Thanks,
>
> Brian
>
>
>
> "Jonathan Wood" <jwood@softcircuits.com> wrote:
> >As a general rule, VB makes temporary strings like crazy.
> >
> >However, know what API functions are available for working with BSTRs, if
> I
> >had to hazzard a guess, I'd say VB does the following in this case.
> >
> >+Copies "a" to a new string
> >+Appends " B" to that same string
> >+Appends "C" to that same string
> >+Assigns that string to s
> >
> >--
> >Jonathan Wood
> >SoftCircuits Programming
> >http://www.softcircuits.com
> >
> >Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
> >news:3965ded2$1@news.devx.com...
> >>
> >> Hi,
> >>
> >> when one combines strings
> >> e.g.,
> >>
> >> s="a" & " B" & "C"
> >>
> >> are temporary strings made for each addition
> >> i.e., "a B" and then "a BC", or does VB look at the lengths of
> >> all parts, and reserve enough space to copy all into the final
> >> string such that temporaries are not required?
> >>
> >>
> >> Brian
> >>
> >
> >
>

Re: Strings and memory management

Hi Brian,

Well that's what VB does. The alternartive is to pre-allocate a string the
size of the sum of the lengths of the substrings and then use the Mid$
statement to assign the sub strings to the appropiate places in the string.

Bill.

"Brian Leung" <bleung@zoo.cam.ac.uk> wrote in message
news:39668f21$1@news.devx.com...
>
> Hi,
>
> I don't mind appending to the same string...unless by doing so, VB must
recopy
> the entire
> string to allow the increasing size of the string. If that is true, is
there
> a way around it...such as
> finding the total length of all strings to be appended and reserving a
fixed
> length string of appropriate size.
> I assume recopying is more expensive than finding the length of strings.
> Are there other methods to reserve adequate space (i.e., ones that retain
> the ability to dynamically resize)?
>
> Thanks,
>
> Brian
>
>
>
> "Jonathan Wood" <jwood@softcircuits.com> wrote:
> >As a general rule, VB makes temporary strings like crazy.
> >
> >However, know what API functions are available for working with BSTRs, if
> I
> >had to hazzard a guess, I'd say VB does the following in this case.
> >
> >+Copies "a" to a new string
> >+Appends " B" to that same string
> >+Appends "C" to that same string
> >+Assigns that string to s
> >
> >--
> >Jonathan Wood
> >SoftCircuits Programming
> >http://www.softcircuits.com
> >
> >Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
> >news:3965ded2$1@news.devx.com...
> >>
> >> Hi,
> >>
> >> when one combines strings
> >> e.g.,
> >>
> >> s="a" & " B" & "C"
> >>
> >> are temporary strings made for each addition
> >> i.e., "a B" and then "a BC", or does VB look at the lengths of
> >> all parts, and reserve enough space to copy all into the final
> >> string such that temporaries are not required?
> >>
> >>
> >> Brian
> >>
> >
> >
>

Re: Strings and memory management

"Jonathan Wood" <jwood@softcircuits.com> wrote:
>It would take a lot of work to determine exactly which methods cause VB
to
>be the most efficient, and that might change on future versions of VB.
>
>If you are really concerned about optimizations at this level, I'd recommend
>looking into C.
>
>Or, if you don't mind paying, someone such as myself could write you a
>routine in C or assembler to do what you want.
>
>--
>Jonathan Wood
>SoftCircuits Programming
>http://www.softcircuits.com
>Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
>news:39668f21$1@news.devx.com...

Hi Jonathan,

I program in C++ as well as VB and considered writing a function to do this
in C++.
I envisioned passing Unicode strings to C++ and having to convert them to
ANSI and then back
again as well as having to copy them into a C++ string before casting it
into a BSTR and passing
it back. Alternatively, I suppose I could use byte arrays as strings and
the API copyMemory into a
fixed length string after determining the total length of the final string.

Oh well…I think I'll see whether the performance of the program I playing
with is reasonable before
I try these things. I suppose the slowest step would probably be writing
these strings to file rather than
memory management of even large strings.

Cheers,

Brian

>>
>> Hi,
>>
>> I don't mind appending to the same string...unless by doing so, VB must
>recopy
>> the entire
>> string to allow the increasing size of the string. If that is true, is
>there
>> a way around it...such as
>> finding the total length of all strings to be appended and reserving a
>fixed
>> length string of appropriate size.
>> I assume recopying is more expensive than finding the length of strings.
>> Are there other methods to reserve adequate space (i.e., ones that retain
>> the ability to dynamically resize)?
>>
>> Thanks,
>>
>> Brian
>>
>>
>>
>> "Jonathan Wood" <jwood@softcircuits.com> wrote:
>> >As a general rule, VB makes temporary strings like crazy.
>> >
>> >However, know what API functions are available for working with BSTRs,
if
>> I
>> >had to hazzard a guess, I'd say VB does the following in this case.
>> >
>> >+Copies "a" to a new string
>> >+Appends " B" to that same string
>> >+Appends "C" to that same string
>> >+Assigns that string to s
>> >
>> >--
>> >Jonathan Wood
>> >SoftCircuits Programming
>> >http://www.softcircuits.com
>> >
>> >Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
>> >news:3965ded2$1@news.devx.com...
>> >>
>> >> Hi,
>> >>
>> >> when one combines strings
>> >> e.g.,
>> >>
>> >> s="a" & " B" & "C"
>> >>
>> >> are temporary strings made for each addition
>> >> i.e., "a B" and then "a BC", or does VB look at the lengths of
>> >> all parts, and reserve enough space to copy all into the final
>> >> string such that temporaries are not required?
>> >>
>> >>
>> >> Brian
>> >>
>> >
>> >
>>
>
>

Re: Strings and memory management

"Jonathan Wood" <jwood@softcircuits.com> wrote:
>It would take a lot of work to determine exactly which methods cause VB
to
>be the most efficient, and that might change on future versions of VB.
>
>If you are really concerned about optimizations at this level, I'd recommend
>looking into C.
>
>Or, if you don't mind paying, someone such as myself could write you a
>routine in C or assembler to do what you want.
>
>--
>Jonathan Wood
>SoftCircuits Programming
>http://www.softcircuits.com
>Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
>news:39668f21$1@news.devx.com...

Hi Jonathan,

I program in C++ as well as VB and considered writing a function to do this
in C++.
I envisioned passing Unicode strings to C++ and having to convert them to
ANSI and then back
again as well as having to copy them into a C++ string before casting it
into a BSTR and passing
it back. Alternatively, I suppose I could use byte arrays as strings and
the API copyMemory into a
fixed length string after determining the total length of the final string.

Oh well…I think I'll see whether the performance of the program I playing
with is reasonable before
I try these things. I suppose the slowest step would probably be writing
these strings to file rather than
memory management of even large strings.

Cheers,

Brian

>>
>> Hi,
>>
>> I don't mind appending to the same string...unless by doing so, VB must
>recopy
>> the entire
>> string to allow the increasing size of the string. If that is true, is
>there
>> a way around it...such as
>> finding the total length of all strings to be appended and reserving a
>fixed
>> length string of appropriate size.
>> I assume recopying is more expensive than finding the length of strings.
>> Are there other methods to reserve adequate space (i.e., ones that retain
>> the ability to dynamically resize)?
>>
>> Thanks,
>>
>> Brian
>>
>>
>>
>> "Jonathan Wood" <jwood@softcircuits.com> wrote:
>> >As a general rule, VB makes temporary strings like crazy.
>> >
>> >However, know what API functions are available for working with BSTRs,
if
>> I
>> >had to hazzard a guess, I'd say VB does the following in this case.
>> >
>> >+Copies "a" to a new string
>> >+Appends " B" to that same string
>> >+Appends "C" to that same string
>> >+Assigns that string to s
>> >
>> >--
>> >Jonathan Wood
>> >SoftCircuits Programming
>> >http://www.softcircuits.com
>> >
>> >Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
>> >news:3965ded2$1@news.devx.com...
>> >>
>> >> Hi,
>> >>
>> >> when one combines strings
>> >> e.g.,
>> >>
>> >> s="a" & " B" & "C"
>> >>
>> >> are temporary strings made for each addition
>> >> i.e., "a B" and then "a BC", or does VB look at the lengths of
>> >> all parts, and reserve enough space to copy all into the final
>> >> string such that temporaries are not required?
>> >>
>> >>
>> >> Brian
>> >>
>> >
>> >
>>
>
>

Re: Strings and memory management

First of all, usually strings have to do with UI, the user is the slowest part of a computer, ), therefore if you must do "a" + "
b" + "c", they most likely won't notice the less than a milisecond difference, )

Second of all, if you create an AX dll there is no unicode/ansi conversion. You can cheat the conversion via type libraries.

Re: Strings and memory management

First of all, usually strings have to do with UI, the user is the slowest part of a computer, ), therefore if you must do "a" + "
b" + "c", they most likely won't notice the less than a milisecond difference, )

Second of all, if you create an AX dll there is no unicode/ansi conversion. You can cheat the conversion via type libraries.