*** glibc detected *** ... free(): corrupted unsorted chunks
*** glibc detected *** ... malloc(): memory corruption
etc.
Endlesss errooooors.., success finally, compiled ok.Not yet routine, only example to unifying.
(My mind was mystified with 'Cast/Cptr' usage, looks like).
Example looks now (non-OOP version with pointers)
Code: Select all
#include "fbgfx.bi"
screen 20,32
'UDT for 4 user datafile formats and parameters
type fileTypeBuffer
indexFR as integer
notes as string
bufferType as string
bufferVariable as string
bufferPointer as any Ptr
bufferLEN as ubyte
bufferSize as byte
end type
'4 custom buffer datatype here - UDTs for RANDOM file reading of user data
type udtA
as byte a
end type
type udtB
as ushort b
as string*3 cs
end type
type udtC
as single a
as ubyte c
as integer d
as string*8 fs
end type
type udtD
as long dw
as single a
as ubyte c
as integer d
as string*10 ws
end type
const bufferTypeSsum=4
dim shared fileReadParm(1 to bufferTypeSsum) as fileTypeBuffer
dim shared recN as integer 'record number
dim shared as integer fx(1 to 4) 'freefile
'define global buffers for reading files
Static shared buffA as udtA
Static shared buffB as udtB
Static shared buffC as udtC
Static shared buffD as udtD
Static shared buffR as udtD
'pointers to types
Static Shared As udtA Ptr pA
Static Shared As udtB Ptr pB
Static Shared As udtC Ptr pC
Static Shared As udtD Ptr pD
Static Shared As udtD Ptr pR
pA=0
pB=0
pC=0
pD=0
'array of I/O parameters
'fill values for file format + buffer
with fileReadParm(1)
.indexFR=1 'just info
.bufferPointer=pA 'address const
.bufferLEN=LEN(buffA) 'lenght of variable
.bufferSize=Sizeof(udtA) 'size of type
end with
with fileReadParm(2)
.indexFR=2 'just info
.bufferPointer=pB 'address const
.bufferLEN=LEN(buffB) 'lenght of variable
.bufferSize=Sizeof(udtB) 'size of type
end with
with fileReadParm(3)
.indexFR=3 'just info
.bufferPointer=pC 'address const
.bufferLEN=LEN(buffC) 'lenght of variable
.bufferSize=Sizeof(udtC) 'size of type
end with
with fileReadParm(4)
.indexFR=4 'just info
.bufferPointer=pR 'address const
'.bufferPointer=pD 'address const
.bufferLEN=LEN(buffD) 'lenght of variable
.bufferSize=Sizeof(udtD) 'size of type
end with
'define global index of buffer type selected
dim shared iB as integer
'testin - print buffer members
sub printBufferMembers(byval id as integer,byval p as any ptr)
? " Buffer members ... buffer type id= ";id
select case id
case 1
? "->a "; cast(Typeof(buffA) ptr, p)->a
case 2
? "->b "; cast(Typeof(buffB) ptr, p)->b
? "->cs "; cast(Typeof(buffB) ptr, p)->cs
case 3
? "->a "; cast(Typeof(buffC) ptr, p)->a
? "->c "; cast(Typeof(buffC) ptr, p)->c
? "->d "; cast(Typeof(buffC) ptr, p)->d
? "->fs "; cast(Typeof(buffC) ptr, p)->fs
case 4
? "->dw "; cast(Typeof(buffD) ptr, p)->dw
? "->a "; cast(Typeof(buffD) ptr, p)->a
? "->c "; cast(Typeof(buffD) ptr, p)->c
? "->d "; cast(Typeof(buffD) ptr, p)->d
? "->fs "; cast(Typeof(buffD) ptr, p)->ws
end select
end sub
'simulation starts here
var aqa=1
var bbq=1
dim as any Ptr p=0
'open files
var aqar=10 'dummy
for i as integer=1 to 4
fx(i)=freefile()
aqar=fileReadParm(i).bufferSize
open str(i)+"datatype.dat" for random as #fx(i) Len=aqar
next i
'uncomment to create files
/'
'fill some buffer variable
scope
'A
buffA.a=26
'B
buffB.b=12345
buffB.cs="XYZ"
'C
buffC.a=999.5
buffC.c=99
buffC.d=321000
buffC.fs="87654321"
'D
buffD.dw=123000456
buffD.a=23.4
buffD.c=101
buffD.d=23000
buffD.ws="0123456789"
'R
buffR.dw=1111111111
buffR.a=4321.0
buffR.c=55
buffR.d=20002
buffR.ws="0022433789"
? "pR ";pR ',*pr ',pR[0]
end scope
'create files of one record
put #fx(1),,buffA
put #fx(2),,buffB
put #fx(3),,buffC
put #fx(4),,buffD
'/
'target to do
Sub randomReadRecord3(byval id as integer, byref p as any Ptr)
'get #ff,recN,*s,1
end sub
'simulated flow
var sizeX=0 'bufferSize empty
recN=1 'first record only
'used pointer to its datatype works ok
iB=1
sizeX=fileReadParm(iB).bufferSize
? "sizeX ";sizeX,:? " pA = ";pA,
pA = Allocate(sizeX)
get #fx(iB),recN,*pA,1
? "2- pA = ";pA
printBufferMembers(iB,pA) 'testin - print buffer members
?
iB=3
sizeX=fileReadParm(iB).bufferSize
? "sizeX ";sizeX,:? " pC = ";pC,
pC = Allocate(sizeX)
get #fx(iB),recN,*pC,1
? "2- pC = ";pC
printBufferMembers(iB,pC) 'testin - print buffer members
?
iB=4
sizeX=fileReadParm(iB).bufferSize
? "sizeX ";sizeX,:? " pD = ";pD,
pD = Allocate(sizeX)
get #fx(iB),recN,*pD,1
? "2- pD = ";pD
printBufferMembers(iB,pD) 'testin - print buffer members
?
deallocate (pA): pA=0
'now used pointer pA to all datatypes works too
iB=1
sizeX=fileReadParm(iB).bufferSize
? "fx(iB),recN,sizeX ";fx(iB),recN,sizeX,:? " pA = ";pA,
pA = Allocate(sizeX)
'get #fx(iB),recN,*Cptr(udtD ptr, pA),1
get #fx(iB),recN,*Cast(udtA ptr, pA),1 ',sizeX
? "2- pA = ";pA
printBufferMembers(iB,pA) 'testin - print buffer members
deallocate (pA): pA=0
?
iB=2
sizeX=fileReadParm(iB).bufferSize
? "fx(iB),recN,sizeX ";fx(iB),recN,sizeX,:? " pA = ";pA,
pA = Allocate(sizeX)
get #fx(iB),recN,*Cast(udtB ptr, pA),1 ',sizeX
? "2- pA = ";pA
printBufferMembers(iB,pA) 'testin - print buffer members
deallocate (pA): pA=0
?
iB=3
sizeX=fileReadParm(iB).bufferSize
? "fx(iB),recN,sizeX ";fx(iB),recN,sizeX,:? " pA = ";pA,
pA = Allocate(sizeX)
get #fx(iB),recN,*Cast(udtC ptr, pA),1 ',sizeX
? "2- pA = ";pA
printBufferMembers(iB,pA) 'testin - print buffer members
deallocate (pA): pA=0
?
close
? "ended, press key..."
sleep
end
compiled + works ok
Now I want to change/replace this part
Code: Select all
"Cast(udtB ptr," 'from line " get #fx(iB),recN,*Cast(udtB ptr, pA),1 "
by used index "iB"
with keyword 'Typeof(*somePointer(iB))', assuming, to get needed routine.