April 28, 2024, 01:22:13 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Dynamic Array Binding (how?)

Started by Ficko, February 11, 2007, 04:16:51 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ficko

I have this program here which works fine if it is NOT in a SUB but freeze badly in a SUB.

I am wondering why?  ???

What I am trying to accomplish to bind Variables with size of 6 or less from a DB but I want to be efficient with memory usage so I do want create dynamic array elements with tailor-made size.

But I just can’t find a way that would work in a SUB.  :'(

Is there a â€Ã...“trickâ€Ã, to do it?

Thanks for any hint! :D
Csaba

OPENCONSOLE

PILOT()

DO:UNTIL INKEY$<>""
CLOSECONSOLE



SUB Pilot
   DEF H AS POINTER
   H = NEW(CHAR,70)
   FOR A = 0 TO 9
      H[A*7,A] ="123456"
      'BindVariable(hstmt,A+1,H[A*7,A]
   NEXT A
   
   PRINT #<STRING>H[0,0]
   PRINT #<STRING>H[0,9]
   DELETE H
ENDSUB

Ficko

OK, I had worked it out myself with ALLOCMEM.

2 Things to it because the user guide didn't mention it or erroneous.

1.)   In a SUB ALLOCMEM do NOT initialized to zero!
2.)            The exemplar there should be:
                â€Ã...“IF ALLOCMEM(mymem,100,10)=0â€Ã...“ due to the return value is 0 OR -1.

I still don't know why my first exemplar freeze but this works:


DECLARE IMPORT, _RtlZeroMemory ALIAS RtlZeroMemory(dest AS POINTER,numBytes AS INT)
OPENCONSOLE

Pilot()

DO:UNTIL INKEY$<>""
CLOSECONSOLE

SUB Pilot
DEF MyMem as MEMORY
DEF Index AS POINTER
IF ALLOCMEM(MyMem,10,7)=0
   _RtlZeroMemory(MyMEM,70)
   Index = MyMEM
    PRINT "Allocated 70 bytes of memory"
   FOR A = 0 to 9
      #<STRING>Index = "123456"
      'dbBindVariable(hstmt,A+1,#<STRING>Index)
      Index += 7
   NEXT A
   Index = MyMEM
   FOR A =0 TO 9
      PRINT #<STRING>Index
      Index += 7
   NEXT A   
    FREEMEM MyMem
ENDIF
ENDSUB

Ionic Wind Support Team

ALLOCMEM does initialize to zero.

Your first attempt failed because you were trying to access allocated memory as a multi-dimensional array without dereferencing it.

The memory returned by NEW and ALLOCMEM are identical, in fact they both call the API function GlobalAlloc.  The only difference is ALLOMEM uses a MEMORY variable type wich supports the READMEM and WRITEMEM statements. 
Ionic Wind Support Team

Ficko

You right Paul ALOCMEM do initialize with zeros it was my confusion sorry about that!

But I do not understand what you mean with â€Ã...“dereferencingâ€Ã, the array?

What that has to do with the program running as main or as a SUB.

This program running fine if it is not in a SUB but freezing in a SUB?

OPENCONSOLE

DEF H AS POINTER
H = NEW(CHAR,70)
FOR A = 0 TO 9
   H[A*7,A] ="123456"
NEXT A

PRINT #<STRING>H[0,0]
PRINT #<STRING>H[0,9]
DELETE H

DO:UNTIL INKEY$<>""
CLOSECONSOLE

Csaba

Parker

If it's a pointer, you need to dereference it. This is different from C where an array is a pointer so the [] does the dereference.

For example,

'' Doesn't need a dereference because it is just an array
dim stuff[10] as char
...
print stuff[5]

''---------
'' Needs a dereference (*) or (#) because it is a pointer
dim stuff as pointer
stuff = new(char, 10)
...
print *stuff[5]


Whether you use * or # is personal preference I think. I use * because I use C a lot.

Ionic Wind Support Team

You can't do this:
POINTER H
H = NEW(CHAR,70)
FOR A = 0 TO 9
   H[A*7,A] ="123456"
NEXT A

The array reference would access the pointer in 4 byte pointer elements because the pointer is untyped.  And it is not a two dimensional array, so you would be tromping on memory.

The syntax itself is valid because you are allowed to access an array of pointers, you're just not using it correctly.

The correct way to do what you want, since you want an dynamic array of 10*7 is this:


pointer mem
mem = new(char,70)
for x = 0 to 6
*<string>(mem + x*7) = "123456"
next x

for x = 0 to 6
print *<string>(mem + x*7)
next x

do:until inkey$ <>""


You have to be careful not to overwrite memory.

Paul.
Ionic Wind Support Team

Ficko