You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
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.
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()
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.
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.
[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
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment