Announcement

Collapse
No announcement yet.

Small question. What is this code doing?

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

  • Small question. What is this code doing?

    I found this in a post. What is this doing? Why does it show all 20 characters when it (supposedly) only allocated 12 characters?


    Code:
    #COMPILE EXE
    #DIM ALL
    
    '------------------------------------------------------------------------------
     FUNCTION GetMem(BYVAL memSize AS LONG) AS LONG
        LOCAL memHandle AS LONG
        LOCAL memString AS STRING PTR
    
        memString = VARPTR(memHandle)                 ' the string pointer points to a NULL pointer to the data (which is allowed for OLE strings)
        @memString = STRING$(memSize, CHR$(0))        ' allocate memory ... memHandle now points to the memory data
    
        FUNCTION = memHandle                          ' memHandle = handle to memory, pointer to the data
     END FUNCTION
    '------------------------------------------------------------------------------
    
     SUB ReleaseMem(BYVAL memHandle AS LONG)
        LOCAL memString AS STRING PTR
    
        memString = VARPTR(memHandle)                 ' let memString be a pointer to the string which handle is memHandle
        @memString = ""                               ' release the memory ... -> memHandle = 0
    
     END SUB
    
    '------------------------------------------------------------------------------
    
    FUNCTION PBMAIN () AS LONG
    
    
        LOCAL lpBuffer AS ASCIIZ PTR
    
        lpBuffer = GetMem(12)
        @lpBuffer = "12345678901234567890"
    
    
        MSGBOX @lpBuffer
    
        ReleaseMem lpBuffer
    
    END FUNCTION 

  • #2
    This line stores 20 characters at the pointer.
    @lpBuffer = "12345678901234567890"
    This line returns the handle to the string generated in GetMem.
    lpBuffer = GetMem(12)
    If you use the following you'll see that lpbuffer is just that.
    MSGBOX FORMAT$(lpBuffer)

    As well,, well, there are other issues. Where did you get the code?
    Rod
    "To every unsung hero in the universe
    To those who roam the skies and those who roam the earth
    To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

    Comment


    • #3
      Thanks Rodney - But where does the 12 fit in? When I added MSGBOX FORMAT$(lpBuffer) it gave me the memory location, right? No mater what I do the the number called as part of GetMem() nothing changes. What is the purpose of passing a number?

      Code:
      FUNCTION PBMAIN () AS LONG
      
      
          LOCAL lpBuffer AS ASCIIZ PTR
      
          lpBuffer = GetMem(1)
          @lpBuffer = "12345678901234567890"
      
      
          MSGBOX @lpBuffer
          MSGBOX FORMAT$(lpBuffer)
      
          ReleaseMem lpBuffer
      
      END FUNCTION

      Comment


      • #4
        The GetMem function allocates space to the string memstring. In the context of just the code shown it does nothing. You allocate the string to 12 characters and then reallocate a different string to 20 and fill it with numbers.

        The problem I see is memstring is a local string to the GetMem function which is deallocated once the function terminates.

        Comment


        • #5
          AH! Thanks Frank.

          Comment


          • #6
            Unless I am completely missing something PBMain is using lpBuffer, but GetMem is using memstring and memhandle which have nothing to do with lpBuffer. After GetMem terminates memstring and memhandle are not available anymore but returns a handle to memstring which was never assigned anything. All that ReleaseMem does is make lpBuffer equal nothing. Some pretty strange code..

            Comment


            • #7
              Well Frank, mostly correct but memstring was assigned something, "memstring=VARPTR(memHandle)", just not a string, which was just as well as it is a string pointer.
              Perhaps this was intended as a stepping stone to a more useful concept.
              Rod
              "To every unsung hero in the universe
              To those who roam the skies and those who roam the earth
              To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

              Comment


              • #8
                Dave,

                There are various ways to allocate memory, you can use BASIC string memory and use STRPTR to get its address or you can use either GlobalAlloc() or HeapAlloc(), they all work OK. Once you have a pointer to memory, all you need to do is make sure you don't go past the end of the allocated memory or you get a memory page fault.
                hutch at movsd dot com
                The MASM Forum

                www.masm32.com

                Comment


                • #9
                  Thanks all!

                  Comment


                  • #10
                    This is where I found the original code.
                    https://forum.powerbasic.com/forum/u...for-pbwin-9-02

                    Comment

                    Working...
                    X