Announcement

Collapse
No announcement yet.

what the!?

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

  • what the!?

    Hello,


    Can sombody explain this to me. How come SIZEOF returns 80 inside
    PBMAIN but returns 0 in the test function. This really has me boggled!
    I was under the impression that SIZEOF would always return the size
    of the ASCIIZ.


    Code:
    #compile exe
    
    
    function Test(Buffer as asciiz) as long
        msgbox format$(sizeof(Buffer))
    end function
    
    
    function pbmain as long
        local mark as asciiz * 80
        
        
        mark = "mark"
        msgbox format$(sizeof(mark))    
        test mark
    end function
    ------------------
    Cheers

  • #2
    Asciiz strings haven't the descriptors.
    That's why it possible to understand SizeOf from declaration only.
    If to use Function Test(Buffer as asciiz * x) SizeOf(Buffer) will return x, not depends of real value.
    Len is another question. Because Asciiz is 0 terninated, PB is able to calculate.

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

    Comment


    • #3
      The variable MARK is an ASCIIZ * 80, which is a buffer of 80 characters. The variable BUFFER is an ASCIIZ, which is a pointer to a nul-terminated buffer. So, I'd probably expect SIZEOF to return four (the size of a pointer), not zero. I'll have to take that up with Development.


      ------------------
      Tom Hanlin
      PowerBASIC Staff

      Comment


      • #4
        The word on this is, as a BASIC type, ASCIIZ without a length specification is considered as an ASCIIZ string of undefined length. So, SIZEOF is not actually meaningful here, and returns zero as an arbitrary result.

        ------------------
        Tom Hanlin
        PowerBASIC Staff

        Comment


        • #5
          One "solution" is to pass just a pointer to the ASCIIZ data and manipulate it thus:
          (Note this is PB/DOS code as I don't have PB/DLL installed on the PC I'm using today, so you'll need to adapt the technique as you find necessary)

          Code:
          %AZsize = 80
          function Test(byval buffer as dword) as long
            dim x as asciiz ptr * %AZsize
            x = buffer
            print sizeof(@x), len(@x)
          end function
           
          dim mark as asciiz * %AZsize
           
          mark = "mark"
          print sizeof(mark), len(mark)
          test varptr32(mark)
          end
          
          Results:
          80  4
          80  4
          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment


          • #6
            Thanks Guy's!


            The original idea was that I was going to make some "macros" so
            that I could get and put any powerbasic type to and from an edit
            box.


            Code:
            GetItemByte
            GetItemWord
            GetItemDword
            GetItemInteger
            GetItemLong
            GetItemQuad
            GetItemSingle
            GetItemDouble
            GetItemString
            GetItemAsciiz
            
            PutItemByte
            PutItemWord
            PutItemDword
            PutItemInteger
            PutItemLong
            PutItemQuad
            PutItemSingle
            PutItemDouble
            PutItemString
            PutItemAsciiz
            For the string versions of these wrappers I needed to know the
            full allocated length of the string. That way I could make sure
            that I dont retrieve more string data then there is string space.


            I solved the problem by just adding another variable to the string
            functions...

            Code:
            function GetItemAsciiz(byval hDlg as long, byval ID as long, Buffer as asciiz, byval Length as long) as long
                function = SendMessage(GetDlgItem(hDlg,ID), %WM_GETTEXT, Length, varptr(Buffer))  
            end function
            
            function PutItemAsciiz(byval hDlg as long, byval ID as long, Buffer as asciiz)
                function = SendMessage(GetDlgItem(hDlg,ID), %WM_SETTEXT, 0, varptr(Buffer))
            end function



            ------------------
            Cheers

            [This message has been edited by mark smit (edited November 29, 2000).]

            Comment

            Working...
            X