dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 49

Thread: Hardcore String Mid$'ing

  1. #16
    Anthony Jones Guest

    Re: Hardcore String Mid$'ing

    Chris,

    Aren't you corrupting your string by poking a length value in the middle of
    a larger string?

    How do you ensure that the large string gets released properly?

    >>

    Aren't the characters "rst" stored right after "Fi" in memory? I was under
    the impression that strings were stored continously in memory.
    <<

    Yes. It's worrying that you asked.

    However, from what you have described so far I can't see a reason for your
    strange results. Perhaps if you share your definition of PokeLng and how
    your use it or better yet some example code.

    --
    Anthony Jones
    Nuesoft Ltd




  2. #17
    Anthony Jones Guest

    Re: Hardcore String Mid$'ing

    Chris,

    Aren't you corrupting your string by poking a length value in the middle of
    a larger string?

    How do you ensure that the large string gets released properly?

    >>

    Aren't the characters "rst" stored right after "Fi" in memory? I was under
    the impression that strings were stored continously in memory.
    <<

    Yes. It's worrying that you asked.

    However, from what you have described so far I can't see a reason for your
    strange results. Perhaps if you share your definition of PokeLng and how
    your use it or better yet some example code.

    --
    Anthony Jones
    Nuesoft Ltd




  3. #18
    Chris Lucas Guest

    Re: Hardcore String Mid$'ing


    >Aren't you corrupting your string by poking a length value in the middle

    of
    >a larger string?


    The addresses in memory that I'm poking values into should (and do) coincide
    with the BSTRs for my string array, they are not inside my longer string.
    Of course you were only getting all this second-hand so that's my fault.
    Below is the code and the contents of the immediate window. You can see
    that the string is fine and there is no memory leak as everything will go
    out of scope at the same time. This is rapidly becoming personal, I'd really
    like to get this to work. Thanks for the help.

    Chris

    ' Toss this into a module
    Option Explicit

    Public Declare Sub PeekLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    Dst As Long, _
    ByVal Addr As Long, _
    Optional ByVal nBytes As Long = 4)
    Public Declare Sub PokeLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    ByVal Addr As Long, _
    Value As Long, _
    Optional ByVal nBytes As Long = 4)

    Sub Main()

    Dim asFoo() As String
    Dim strTokens As String
    Dim i As Long
    Dim tmp As Long

    ReDim asFoo(2)

    strTokens = "First Second Third"

    asFoo(0) = Space$(5)
    asFoo(1) = Space$(6)
    asFoo(2) = Space$(5)


    ' Make asFoo(0) point at "F" in strTokens
    tmp = StrPtr(strTokens)
    PokeLng StrPtr(asFoo(0)), ByVal tmp

    ' Make asFoo(0) 5 characters long
    PokeLng StrPtr(asFoo(0)) - 4, 10

    ' Make asFoo(1) point at "S" in strTokens
    tmp = tmp + 12
    PokeLng StrPtr(asFoo(1)), ByVal tmp

    ' Make asFoo(1) 6 characters long
    PokeLng StrPtr(asFoo(1)) - 4, 12

    ' Make asFoo(2) point at "T" in strTokens
    tmp = tmp + 14
    PokeLng StrPtr(asFoo(2)), ByVal tmp

    ' Make asFoo(2) 5 characters long
    PokeLng StrPtr(asFoo(2)) - 4, 10

    ' Lets inspect our handy-work
    For i = 0 To 2
    Debug.Print asFoo(i) & "[End]"
    PeekLng tmp, StrPtr(asFoo(i)) - 4
    Debug.Print tmp
    Next i

    ' Now lets make sure the original string didn't get corrupted
    Debug.Print strTokens
    End Sub


    ' Contents of the immediate window
    Fi [End]
    10
    Se [End]
    12
    Th [End]
    10
    First Second Third

  4. #19
    Chris Lucas Guest

    Re: Hardcore String Mid$'ing


    >Aren't you corrupting your string by poking a length value in the middle

    of
    >a larger string?


    The addresses in memory that I'm poking values into should (and do) coincide
    with the BSTRs for my string array, they are not inside my longer string.
    Of course you were only getting all this second-hand so that's my fault.
    Below is the code and the contents of the immediate window. You can see
    that the string is fine and there is no memory leak as everything will go
    out of scope at the same time. This is rapidly becoming personal, I'd really
    like to get this to work. Thanks for the help.

    Chris

    ' Toss this into a module
    Option Explicit

    Public Declare Sub PeekLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    Dst As Long, _
    ByVal Addr As Long, _
    Optional ByVal nBytes As Long = 4)
    Public Declare Sub PokeLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    ByVal Addr As Long, _
    Value As Long, _
    Optional ByVal nBytes As Long = 4)

    Sub Main()

    Dim asFoo() As String
    Dim strTokens As String
    Dim i As Long
    Dim tmp As Long

    ReDim asFoo(2)

    strTokens = "First Second Third"

    asFoo(0) = Space$(5)
    asFoo(1) = Space$(6)
    asFoo(2) = Space$(5)


    ' Make asFoo(0) point at "F" in strTokens
    tmp = StrPtr(strTokens)
    PokeLng StrPtr(asFoo(0)), ByVal tmp

    ' Make asFoo(0) 5 characters long
    PokeLng StrPtr(asFoo(0)) - 4, 10

    ' Make asFoo(1) point at "S" in strTokens
    tmp = tmp + 12
    PokeLng StrPtr(asFoo(1)), ByVal tmp

    ' Make asFoo(1) 6 characters long
    PokeLng StrPtr(asFoo(1)) - 4, 12

    ' Make asFoo(2) point at "T" in strTokens
    tmp = tmp + 14
    PokeLng StrPtr(asFoo(2)), ByVal tmp

    ' Make asFoo(2) 5 characters long
    PokeLng StrPtr(asFoo(2)) - 4, 10

    ' Lets inspect our handy-work
    For i = 0 To 2
    Debug.Print asFoo(i) & "[End]"
    PeekLng tmp, StrPtr(asFoo(i)) - 4
    Debug.Print tmp
    Next i

    ' Now lets make sure the original string didn't get corrupted
    Debug.Print strTokens
    End Sub


    ' Contents of the immediate window
    Fi [End]
    10
    Se [End]
    12
    Th [End]
    10
    First Second Third

  5. #20
    Chris Lucas Guest

    Re: Hardcore String Mid$'ing


    Paul Marshall <pmarshal@vulcraft-al.com> wrote:
    >On 11 Dec 2001 11:49:58 GMT, "Chris Lucas"
    ><cdl1051@earthlink.net> wrote:
    >
    > Public Declare Sub PokeLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    > ByVal Addr As Long, _
    > Value As Long, _
    > Optional ByVal nBytes As Long = 4)
    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    >There's your problem.


    Excellent, thank you for seeing what I kept missing. This was in fact the
    problem. Thanks again to everyone, and especially Paul.

    Chris

  6. #21
    Chris Lucas Guest

    Re: Hardcore String Mid$'ing


    Paul Marshall <pmarshal@vulcraft-al.com> wrote:
    >On 11 Dec 2001 11:49:58 GMT, "Chris Lucas"
    ><cdl1051@earthlink.net> wrote:
    >
    > Public Declare Sub PokeLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    > ByVal Addr As Long, _
    > Value As Long, _
    > Optional ByVal nBytes As Long = 4)
    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    >There's your problem.


    Excellent, thank you for seeing what I kept missing. This was in fact the
    problem. Thanks again to everyone, and especially Paul.

    Chris

  7. #22
    Paul Marshall Guest

    Re: Hardcore String Mid$'ing

    On 11 Dec 2001 11:49:58 GMT, "Chris Lucas"
    <cdl1051@earthlink.net> wrote:

    Public Declare Sub PokeLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    ByVal Addr As Long, _
    Value As Long, _
    Optional ByVal nBytes As Long = 4)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    There's your problem.


    --
    Paul Marshall
    pmarshal@vulcraft-al.com

  8. #23
    Paul Marshall Guest

    Re: Hardcore String Mid$'ing

    On 11 Dec 2001 11:49:58 GMT, "Chris Lucas"
    <cdl1051@earthlink.net> wrote:

    Public Declare Sub PokeLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    ByVal Addr As Long, _
    Value As Long, _
    Optional ByVal nBytes As Long = 4)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    There's your problem.


    --
    Paul Marshall
    pmarshal@vulcraft-al.com

  9. #24
    Rob Teixeira Guest

    Re: Hardcore String Mid$'ing


    "Karl E. Peterson" <karl@mvps.org> wrote:
    >Yeah, the latter.
    >
    >I went back and reread the original question, and I'm not fully sure I understand
    >anyway, so perhaps my question is out of place? I guess if the OP is enlarging

    the
    >string elements, yeah, that'd be a problem. But if their contents is simply

    being
    >replaced...?


    From what i understand, he has a string like:

    "This is a string"

    Then, he is making an array of strings (which we know is really an array
    of pointers), so that (for example) each element is one word in the original
    string. Rather than create new string elements, he wants to simply point
    to a character within the original string. *BAD IDEA*

    The contents of a BSTR Looks like the following:

    [ptr] ----+
    |
    v
    [length] 'T' '' 'h' '' 'i' '' 's' '' [null terminator]

    You can't insert the length or the null terminator into the original string,
    into the middle of the other characters. Without this, some string operations
    will not work correctly.

    Furthermore, not only can you leak memory (as Michael points out), when VB
    automatically calls SysFreeString(), you will most likely corrupt memory.
    In addition, some other string functions also have the potential to corrupt
    memory in a similar fashion.

    Any performance gains would not be worth the potential problems - and wouldn't
    be that big to begin with. I'm not saying this isn't a clever attempt, it's
    just not worth it

    -Rob

  10. #25
    Rob Teixeira Guest

    Re: Hardcore String Mid$'ing


    "Karl E. Peterson" <karl@mvps.org> wrote:
    >Yeah, the latter.
    >
    >I went back and reread the original question, and I'm not fully sure I understand
    >anyway, so perhaps my question is out of place? I guess if the OP is enlarging

    the
    >string elements, yeah, that'd be a problem. But if their contents is simply

    being
    >replaced...?


    From what i understand, he has a string like:

    "This is a string"

    Then, he is making an array of strings (which we know is really an array
    of pointers), so that (for example) each element is one word in the original
    string. Rather than create new string elements, he wants to simply point
    to a character within the original string. *BAD IDEA*

    The contents of a BSTR Looks like the following:

    [ptr] ----+
    |
    v
    [length] 'T' '' 'h' '' 'i' '' 's' '' [null terminator]

    You can't insert the length or the null terminator into the original string,
    into the middle of the other characters. Without this, some string operations
    will not work correctly.

    Furthermore, not only can you leak memory (as Michael points out), when VB
    automatically calls SysFreeString(), you will most likely corrupt memory.
    In addition, some other string functions also have the potential to corrupt
    memory in a similar fashion.

    Any performance gains would not be worth the potential problems - and wouldn't
    be that big to begin with. I'm not saying this isn't a clever attempt, it's
    just not worth it

    -Rob

  11. #26
    Chris Lucas Guest

    Re: Hardcore String Mid$'ing


    "Michael \(michka\) Kaplan" <former_mvp@nospam.trigeminal.spamless.com> wrote:
    >DON'T DO IT.
    >
    >You are still leaking memory. This is bad. Please reconsider this
    >stupidty -- anyone smart enough to do this should be smart enough to know
    >*not* to do it!!!


    Michael,

    As I understand it a memory leak is a chunk of memory that continues
    to be thought of as used by windows even though it is not. So your saying
    when the string and the array go out of scope, because they are both pointing
    to the same spot in memory, this portion will not become free?

  12. #27
    Chris Lucas Guest

    Re: Hardcore String Mid$'ing


    "Michael \(michka\) Kaplan" <former_mvp@nospam.trigeminal.spamless.com> wrote:
    >DON'T DO IT.
    >
    >You are still leaking memory. This is bad. Please reconsider this
    >stupidty -- anyone smart enough to do this should be smart enough to know
    >*not* to do it!!!


    Michael,

    As I understand it a memory leak is a chunk of memory that continues
    to be thought of as used by windows even though it is not. So your saying
    when the string and the array go out of scope, because they are both pointing
    to the same spot in memory, this portion will not become free?

  13. #28
    Michael \(michka\) Kaplan Guest

    Re: Hardcore String Mid$'ing

    DON'T DO IT.

    You are still leaking memory. This is bad. Please reconsider this
    stupidty -- anyone smart enough to do this should be smart enough to know
    *not* to do it!!!


    --
    MichKa

    Michael Kaplan
    (principal developer of the MSLU)
    Trigeminal Software, Inc. -- http://www.trigeminal.com/
    the book -- http://www.i18nWithVB.com/

    "Chris Lucas" <cdl1051@earthlink.net> wrote in message
    news:3c1605c3$1@147.208.176.211...
    >
    > Paul Marshall <pmarshal@vulcraft-al.com> wrote:
    > >On 11 Dec 2001 11:49:58 GMT, "Chris Lucas"
    > ><cdl1051@earthlink.net> wrote:
    > >
    > > Public Declare Sub PokeLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    > > ByVal Addr As Long, _
    > > Value As Long, _
    > > Optional ByVal nBytes As Long = 4)
    > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > >
    > >There's your problem.

    >
    > Excellent, thank you for seeing what I kept missing. This was in fact the
    > problem. Thanks again to everyone, and especially Paul.
    >
    > Chris




  14. #29
    Michael \(michka\) Kaplan Guest

    Re: Hardcore String Mid$'ing

    DON'T DO IT.

    You are still leaking memory. This is bad. Please reconsider this
    stupidty -- anyone smart enough to do this should be smart enough to know
    *not* to do it!!!


    --
    MichKa

    Michael Kaplan
    (principal developer of the MSLU)
    Trigeminal Software, Inc. -- http://www.trigeminal.com/
    the book -- http://www.i18nWithVB.com/

    "Chris Lucas" <cdl1051@earthlink.net> wrote in message
    news:3c1605c3$1@147.208.176.211...
    >
    > Paul Marshall <pmarshal@vulcraft-al.com> wrote:
    > >On 11 Dec 2001 11:49:58 GMT, "Chris Lucas"
    > ><cdl1051@earthlink.net> wrote:
    > >
    > > Public Declare Sub PokeLng Lib "kernel32" Alias "RtlMoveMemory" ( _
    > > ByVal Addr As Long, _
    > > Value As Long, _
    > > Optional ByVal nBytes As Long = 4)
    > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > >
    > >There's your problem.

    >
    > Excellent, thank you for seeing what I kept missing. This was in fact the
    > problem. Thanks again to everyone, and especially Paul.
    >
    > Chris




  15. #30
    Chris Lucas Guest

    Re: Hardcore String Mid$'ing


    Well, this thread has gotten rather long eh? Anyway, I was poking my
    pointer into the wrong spot. What I originally thought was the solution
    was nothing more than a super low level mid. I wound up NOT allocating the
    string with the API. You don't have to. By changing 4 to 10 on the CopyMem
    call, windows really was copying "First" into asFoo(0). This is too much
    work, I was trying to get around actually moving memory. As best as I can
    tell, this is simply asFoo(0) = Mid$(strToken,1,5). Two copies of those
    bytes exist after the CopyMem call.
    The real solution was to poke the value of StrPtr(strToken) into VarPtr(asFoo(0)).
    Now the string element points to the spot in memory where strToken lives.
    Then I poke 10 into StrPtr(asFoo(0))-4. Now my question is, since I've
    not allocated any strings, do I still get a memory leak? I don't see how,
    but I'll leave it the the gurus to tell me.
    And by the way, I'm glad I've been upgraded from "stupid" to perhaps
    ill-advised. Thus is the price of fast string ops in VB.

    Chris

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