Announcement

Collapse
No announcement yet.

StrPtr

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • StrPtr

    How are you guys doing?

    Don't kill me for this but I don't get it!

    The following lines drive me nuts!
    It might be discussed already but I can't
    remember.
    '---------------------------------------
    #COMPILE EXE
    #INCLUDE "tlxdebug.inc"

    FUNCTION PBMAIN
    DIM a$
    DIM i AS LONG



    a$ = a$ + SPACE$(524219)
    debugprint str$(STRPTR(a$))
    NEXT i


    END FUNCTION
    '---------------------------------------

    RESULT = 1.192757E+7

    If I just write a$ = Space$(524219) the world is ok and the size
    can be as big as it wants.

    Another step:
    For i = 1 to 524219
    a$ = a$ + " "
    Next i
    This will perform the same overflow

    Everything is fine as long the string size is not bigger than 524218
    if I expand an existing string.

    Ok, what is that?
    I mean it looks like a bug to me.

    Oh, I forgot: Ram size is 64MB. Language is PBDLL 6.0
    and tlxdebug is not needed. The Msgbox will do the same.

    Regards
    --- Tom.




    ------------------

  • #2
    Tom --
    Lance already explained this (stack size).
    Use Static/Global String (Asciiz) instead of Local.


    ------------------
    E-MAIL: [email protected]

    Comment


    • #3
      It doesn't work with global strings either.

      Regards
      ---Tom


      ------------------

      Comment


      • #4
        Tom --
        Doesn't work ? What ?
        a$ = a$ + " " one million times ?
        It's clear that such operations
        1) in any case take a lot of time
        2) if you operate with dynamic strings I also expect serious troubles, because each time you ask bigger memory block.
        Meanwhile in some OSes there are problems with memory management.

        Try this
        Code:
          #Compile Exe
          #Register None
          Function PbMain
             %n = 5000000
             %m = 3000000
             Global a As Asciiz * (%n + 1), i As Long
             ReDim b(1 To %n) As Byte At VarPtr(a)
             For i = 1 To %m
                b(i) = 32 ' instead of a = a + " "
             Next
             MsgBox Str$(Len(a$))
          End Function
        ------------------
        E-MAIL: [email protected]

        Comment


        • #5
          Semen--

          I do well understand that it makes no sense to add one char
          1000000 times but thanks for clarifying. ;-)

          My question was just another:
          If I allocate memory like that a$ = SPACE$(2^20) then I get a
          Strinpointer by using STRPTR()

          If I allocate memory like that a$ = "" : a$ = a$ + SPACE$(2^20)
          Then I get an Overflow. If I use a predifined String instead of
          Space$() then it is not gonna work either.

          This technic is not working also with Global Strings.

          I just want to know why?
          What is going on internally when I do a$ = a$ + BigString
          How is the memory allocated
          What is different to just: a$ = anything


          Ok.
          ---Tom




          ------------------

          Comment


          • #6
            Tom --
            I really do not understand about what you are talking.
            BTW, about local stack. I remembered that Lance told about local pre-allocated strings (asciiz).
            Here I agree - in your case Global/Static/Local has no relation to your problem.

            Well, you wrote
            "If I allocate memory like that a$ = SPACE$(2^20) then I get a
            Strinpointer by using STRPTR()
            If I allocate memory like that a$ = "" : a$ = a$ + SPACE$(2^20)
            Then I get an Overflow."

            You need to give full sample, which doesn't work, because on my PC similar code works under Win2000 and 95 too.
            Code:
               #Compile Exe
               Function PbMain
                  Dim a As String
                  a$ = a$ + Space$(2^25)
                  #Debug Print Str$(StrPtr(a$))
                  MsgBox Str$(StrPtr(a$))
               End Function
            I don't think that there is difference between a$ = a$ + BigString
            and a$ = BigString, if initial a$ = "".

            Another question that statements similar For i = 1 To x: a$ = a$ + " ": Next are able to touch any PC.

            ------------------
            E-MAIL: [email protected]

            Comment


            • #7
              Semen --

              It looks like that it is a problem on my machine. Thats the only
              explanation I'm having.

              Every value which is bigger than approx. 1/2 Meg makes this trouble.

              But you know whats funny the length of the string is exactly the
              length I've allocated. All the regular string operations are working
              pretty well but not STRPTR() there I'm getting an overflow.

              Regards.
              ---Tom


              ------------------

              Comment


              • #8
                Semen --

                Now I found out that when I' doing this:
                ================================
                DIM strp AS STRING PTR

                strp = VARPTR(a$)
                ================================

                that everything is ok and I can live with that.

                But I still don't understand that STRPTR() is not working
                correctly on my machine but on yours.

                Regards.
                --- Tom.


                ------------------

                Comment


                • #9
                  Tom,

                  I ran the following code and apart from it taking a very long time to
                  complete, it ran OK. Over 20 minutes on a PIII 600 with half gig ram.
                  Code:
                      LOCAL a$
                      LOCAL i AS LONG
                  
                      For i = 1 to 524219
                      a$ = a$ + " "                           ' very slow operation
                      SetWindowText hWin,ByCopy str$(len(a$)) ' nearly as slow
                      Next i
                  
                      MsgBox "Done"
                  A basic dynamic string is not dependent on the stack size at all, if you
                  make a LOCAL variable of the type string, it takes 4 bytes for the string
                  handle so dynamic string is not your problem here. Basic string uses the
                  OLE string pool so it has no dependency on local or global memory.

                  The STRPTR() function gives you the address of the start of a dynamic
                  string. Where you have to be careful is in changing the string with normal
                  basic string functions as it may change the address of the dynamic string
                  after the initial address was obtained.

                  If you can give us a clear definition of the problem you have had, we may
                  be able to do something with it.

                  Regards,

                  [email protected]

                  ------------------
                  hutch at movsd dot com
                  The MASM Forum

                  www.masm32.com

                  Comment


                  • #10
                    Hi Steve,

                    I'll try to clarify and here is the not working snippet first:
                    '==============================================================
                    #COMPILE EXE

                    GLOBAL a$

                    FUNCTION PBMAIN
                    a$ = ""
                    a$ = a$ + SPACE$(1000000)
                    MSGBOX STR$(STRPTR(a$))
                    END FUNCTION
                    '==============================================================
                    Result here is something like: 1.192757E+7
                    Which is an overflow isn't it?

                    Here is the working version:
                    '==============================================================
                    #COMPILE EXE

                    GLOBAL a$

                    FUNCTION PBMAIN
                    a$ = SPACE$(1000000)
                    MSGBOX STR$(STRPTR(a$))
                    END FUNCTION
                    '==============================================================
                    Result here is a pointer to the data.

                    Is this just on my machine or has somebody else ever had a problem
                    like that. The value of 1000000 may vary on other machines.

                    It is not that just STRPTR() gives me that result
                    this one here CVL( PEEK$( VARPTR(a$), 4)) comes out with the same.

                    And the funny thing is that the string *IS* allocated. I can do
                    operations with it but I need the pointer to the data.

                    Any ideas?

                    Regards
                    ---Tom

                    [email protected]


                    ------------------

                    Comment


                    • #11
                      Tom --
                      > Result here is something like: 1.192757E+7
                      > Which is an overflow isn't it?
                      No, it is not. This is normal address in virtual memory.
                      Instead of Str$ better to use Hex$.

                      > STRPTR() gives me that result CVL(PEEK$(VARPTR(a$), 4))

                      It's correct for dynamic strings.
                      At address VarPtr is located a POINTER (StrPtr) to string data.
                      See PB manual (assembler's samples).

                      Below is simple sample of direct usage StrPtr
                      Code:
                         #Compile Exe
                         Global a$
                         Function PbMain
                            a$ = "": a$ = a$ + Space$(1000000)
                            ReDim b(1 : 1) As String * 1 At StrPtr(a$)
                            b(24) = "A": b(25) = "B": b(26) = "C"
                            MsgBox "/" + Mid$(a$, 24, 3) + "/",, Hex$(StrPtr(a$))
                         End Function


                      ------------------
                      E-MAIL: [email protected]

                      Comment


                      • #12
                        Alright Semen --

                        Now we finally got it.
                        This is what I want to know!

                        You see me like on that overflow thing.

                        Thanks a lot.

                        Regards.
                        ---Tom.


                        ------------------
                        [email protected]

                        Comment

                        Working...
                        X