Strings and memory management


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 37

Thread: Strings and memory management

Hybrid View

  1. #1
    Brian Leung Guest

    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?


    Brian


  2. #2
    Oren Guest

    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
    >




  3. #3
    Jonathan Wood Guest

    Re: Strings and memory management

    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
    >




  4. #4
    Brian Leung Guest

    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
    >>

    >
    >



  5. #5
    Jonathan Wood Guest

    Re: Strings and memory management

    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,
    >
    > 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
    > >>

    > >
    > >

    >




  6. #6
    Bill McCarthy Guest

    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
    > >>

    > >
    > >

    >




  7. #7
    Brian Leung Guest

    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
    >> >>
    >> >
    >> >

    >>

    >
    >



  8. #8
    Oren Guest

    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.


    Oren



  9. #9
    Michael \(michka\) Kaplan Guest

    Re: Strings and memory management

    I think you would be amazed how often VB converts those strings, anyway....

    --
    MichKa
    "Cause it's a bittersweet symphony, thats life..." -- The Verve

    random junk of dubious value, at the multilingual,
    no scripts required, http://www.trigeminal.com/

    "Oren" <CompuMight@erols.com> wrote in message
    news:39674bd7$1@news.devx.com...
    > 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.
    >
    >
    > Oren
    >
    >




  10. #10
    Oren Guest

    Re: Strings and memory management

    I know how many times, I've done many a dumps, )

    Oren



  11. #11
    Jonathan Wood Guest

    Re: Strings and memory management

    Brian,

    > 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.

    Huh? If you are trying to optmize this process, why would you convert
    Unicode to ANSI and then back?

    If you use a typelib, VB can pass the Unicode strings unchanged. I would
    think you'd then want to work with and return Unicode strings.

    Also, be careful with CopyMemory (RtlMoveMemory) because it will convert the
    strings to ANSI and back unless you use something like StrPtr to pass the
    address of the Unicode data.

    > I suppose the slowest step would probably be writing
    > these strings to file rather than
    > memory management of even large strings.


    Amen to that!

    Jonathan




  12. #12
    Jonathan Wood Guest

    Re: Strings and memory management

    Oren,

    > [...] I've done many a dumps, )


    Please keep your bathroom habits private.

    Jonathan




  13. #13
    Brian Leung Guest

    Re: Strings and memory management


    Hi Oren,

    Sounds like you're looking at what VB does behind the scenes.
    I don't know how to do this, but would like to find out.
    Can you point me to any sites or books?

    Thanks,

    Brian


    "Oren" <CompuMight@erols.com> wrote:
    >I know how many times, I've done many a dumps, )
    >
    > Oren
    >
    >



  14. #14
    Brian Leung Guest

    Re: Strings and memory management


    Hi Jonathan,
    >
    >Huh? If you are trying to optmize this process, why would you convert
    >Unicode to ANSI and then back?


    I wouldn't want to, which is why I didn't like this idea.

    >
    >If you use a typelib, VB can pass the Unicode strings unchanged. I would
    >think you'd then want to work with and return Unicode strings.


    Yes. that sounds like a better option. I should probably learn
    how to do that, but until now have only been programming with
    standard C++ features.

    >
    >Also, be careful with CopyMemory (RtlMoveMemory) because it will convert

    the
    >strings to ANSI and back unless you use something like StrPtr to pass the
    >address of the Unicode data.


    I hadn't considered that.

    >
    >> I suppose the slowest step would probably be writing
    >> these strings to file rather than
    >> memory management of even large strings.

    >
    >Amen to that!


    Any suggestions on how best to write to file? i.e., is it
    better to concatenate a large string and write that rather than
    writing separate small string?? Is there a optimal string
    length to write at a time?

    Cheers,

    Brian
    >
    >Jonathan
    >
    >
    >



  15. #15
    Jonathan Wood Guest

    Re: Strings and memory management

    Brian,

    > Yes. that sounds like a better option. I should probably learn
    > how to do that, but until now have only been programming with
    > standard C++ features.


    Like I said, we do this type of thing all the type for ourselves and various
    clients.

    > Any suggestions on how best to write to file? i.e., is it
    > better to concatenate a large string and write that rather than
    > writing separate small string?? Is there a optimal string
    > length to write at a time?


    Disk access is always slow. For the most part, any code that must be as fast
    as possible will not include disk access. There are times where the data is
    organized in such a way that it can be written in a large chunk without a
    bunch of conversions. But for many cases, the benefits do not justify
    worrying about it.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center