Announcement

Collapse
No announcement yet.

Commands for Pointers

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

  • Commands for Pointers

    Hi all,

    Is that possible in the next versions for managing
    pointers new commands for Create, Destroy,... pointers?

    Greetings,
    stephane


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

  • #2
    Create = DIM
    Destroy = NOT DIM, which is not possible.

    ..unless you use dynamic memory allocation, as shown in my December, 1998 "BASICally Speaking" article, "Waste Not, Want Not".

    Info at www.infoms.com, follow the links to BASICally Speaking.

    MCM
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      One of the weaknesses in PB's pointer facility is that there are no procedures for explicitly declaring and destroying memory using the pointers. Here is one solution:

      malloc(size): use pbstralloc (built-in function) to alloacte size+2 bytes. Then copy the resulting string handle (a 16-bit value) into the first two bytes of the space you just allocated. Then return a pointer to the space that's left.

      free(pointer): This will crash the program if you try to free something that wasn't allocated by malloc. Anyway, this routine looks behind the ponter two bytes for the string handle, and then calls pbStrRel (or whatever it is) to release the memory.

      I have actually coded up these two subroutines using the built-in assembler. I'll post them to the source code forum later today.

      Michael


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

      Comment


      • #4
        Originally posted by Michael Torrie:
        One of the weaknesses in PB's pointer facility is that there are no procedures for explicitly declaring and destroying memory using the pointers. Here is one solution:

        malloc(size): use pbstralloc (built-in function) to alloacte size+2 bytes. Then copy the resulting string handle (a 16-bit value) into the first two bytes of the space you just allocated. Then return a pointer to the space that's left.

        free(pointer): This will crash the program if you try to free something that wasn't allocated by malloc. Anyway, this routine looks behind the ponter two bytes for the string handle, and then calls pbStrRel (or whatever it is) to release the memory.

        I have actually coded up these two subroutines using the built-in assembler. I'll post them to the source code forum later today.

        Michael



        Hi Micheal,

        Can you send the routines for managing pointers like
        - Create pointers
        - Destroy pointers
        - Alloc Memory for Pointers
        - Dealloc Memory for Pointers

        Can you please for me routines like C for using PowerBASIC DOS/Windows???
        - Printf()
        - Scanf()
        of
        - Write() & WriteLn()
        ---> writing all the datatypes and formats in PB
        - Read() & ReadLn()
        ---> reading all the datatypes (digits and string not strings only) and formats in PB

        ---> I don't like INPUT and PRINT [USING] is to premetif !!!!!!!!

        Is beter that you can:

        PBWrite("Give the number from 1 to 10")
        PBRead(Number)

        PBWrite("The number is %f3.2",number)
        ==> 45.12
        or
        PBWrite("The number is :2:3",number)

        New Commands is then in an PBL library for I/0
        - PBRead()
        - PBReadLn()

        - PBWrite()
        - PBWriteLn()

        'Ln' ==> CR/LF

        Greetings,
        stephane



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

        Comment


        • #5
          Why not just write these functions yourself? You do know how
          to write a function don't you?

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

          Comment


          • #6
            Here is the malloc/free routines. From what you say about creating and destroying pointers, it doens't appear that you understand totally how to use them. Pointers are simply variables that a 32-bits long. They point to something. You create them by declaring a variable of some type ptr (ie dim x a s integer ptr). PB will destroy all variables when they go out of scope (but not the data they point too). Thus all you need to worry about is malloc and free. I've not touched these in a couple of years, so hopefully they work. If you get weird crashes though, scrutinize this code and it's use carefully.

            Please note that I'm placing this code in the publich domain, so you can do what you want with it.

            Code:
            '$dim all
            declare function getStrAlloc (size as integer)   as integer
            declare function rlsStrAlloc (handle as integer) as integer
            declare function getStrLoc   (handle as integer) as dword
            
            declare function malloc (byval sizeOfType as integer) as dword
            declare function free (typePtr as dword)            as integer
            
            function malloc(byval sizeOfType as integer) public as dword
                 ! push es            ;I'm not sure if this is necessary.  Precautionary
                 ! mov  ax,sizeOfType ;retrieve the size of structure into ax
                 ! add  ax,2          ;add 2 so there's room for the string handle
                 ! push ax            ;push size onto stack
                 ! call getStrAlloc   ;call string alloc function
                 ! mov  si,ax         ;store string handle for future use
                 ! push ax            ;push string handle as a parameter for getStrLoc
                 ! call getStrLoc     ;get str loc into dx:ax
                 ! cmp  dx,0          ;if address.Seg<>0
                 ! jz   endIf1        ;then
            
                 ! mov  es,dx         ;  set es:bx=dx:ax
                 ! mov  bx,ax         ;
                 ! mov  es:[bx],si    ;  save the str handle in first part of buffer
                 ! add  dx,2          ;  move the pointer ahead of the str handle
            
            endIf1:                   'end if
                 ! pop  es
                 ! mov  sp,bp         ;this avoids having to explicitly set function=
                 ! pop  bp            ;since ax:dx are already set.  So we bypass normal
                 ! retf 2             ;function epilogue.
            end function
            
            function free(typePtr as dword) public as integer
                 ! les  bx,typePtr
                 ! mov  ax,es:[bx]   ;peel off the segment portion of the pointer
                 ! mov  es,ax        ;set es to segment
                 ! mov  bx,es:[bx+2] ;peel off the ofset portion
                 ! sub  bx,2         ;subtract two bytes so we can access string handle
                 ! mov  ax,es:[bx]   ;retrieve string handle into AX
                 ! push ax           ;place string handle on stack
                 ! call rlsStrAlloc  ;releases string, ax=true if worked
                 ! mov  sp,bp        ;since ax is already set, we don't want it reset
                 ! pop  bp           ;when PB sets ax=function
                 ! retf 4            ;so we bypass normal epilogue
            end function
            As for the other functions you request, I'm afraid I don't have time to reinvent the wheels. There are a variety of input and output routines in places such as All-Basic Code's web site and other places. Or you could code it yourself. If I come across a printf/scanf thing, I'll pass it on. If you like pascal, consider trying out Free Pascal, a DOS/Windows/Linux -based compiler compatible with Borland's Pascal 7. A web search will find it for you.

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

            Comment


            • #7
              Oops. I goofed a little bit. the keyword "free" is reserved in pb (I knew there was a reason I chose delete), so rename that subroutine to something else, like freemem (is that a keyword too) or delete or something.


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

              Comment


              • #8
                [QUOTE]Originally posted by Michael Torrie:
                [B]Oops. I goofed a little bit. the keyword "free" is reserved in pb (I knew there was a reason I chose delete), so rename that subroutine to something else, like freemem (is that a keyword too) or delete or something.

                Hi Michael,
                Thank you for this routines!
                Can you give you me your email adres?
                Can you please writing me advanced input and output routine but
                PRINT and INPUT is to premitive.

                I wil read to the keyboard al the datatypes en write all the
                datatypes with new line or not in two commands like Pascal.

                Two commands for reading the keyboard:
                PBRead & PBReadLn

                Attension : Ln = CR + LF

                Two commands for writing strings, variabeles with format
                %d = integer
                %f = single
                %...
                %f3.2
                PBWrite & PBWriteLn

                Can you programming in assembly!!!!

                Thanks for writing this codes



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

                Comment

                Working...
                X