Author Topic: Question about dynamic arrays  (Read 1988 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Hero Member
  • *****
  • Posts: 872
Question about dynamic arrays
« on: November 16, 2015, 01:42:05 AM »
Hi Charles,

may I ask for help? I refer to the demo \OxygenBasic\examples\WinGui\Toolbar1.o2bas in about line 147 and about line 187:

  ' bitmap for each button
  static tbBmp(7) as sys 'as BYTE
..
      for i = 0 to 6
..

I tried to replace 7 with a variable but this does not work. If I use a constant

  %count=7
  static tbBmp(count) as sys 'as BYTE
..
      for i = 0 to count-1

this will work and I see a difference if I change the value for count. So I tried:

int num=7
%count=num

this does fail. Is there a possibility to dimension arrays at runtime? I inspected Dynamic.inc but I do not know if I could use a function of this file? (I have not yet tried to test this)

Roland

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
Re: Question about dynamic arrays
« Reply #1 on: November 16, 2015, 03:59:05 AM »

Hi Roland,

Very briefly, this shows how to use a dynamic array:

Code: OxygenBasic
  1. indexbase 0
  2. sys i
  3. sys be=256
  4. byte b at getmemory be
  5.  
  6. for i=0 to be-1
  7. 'access as b[i]
  8. next
  9. print i
  10. ...
  11. freememory @b
  12.  

Arnold

  • Hero Member
  • *****
  • Posts: 872
Re: Question about dynamic arrays
« Reply #2 on: November 16, 2015, 06:59:46 AM »
Thanks Charles,

I now tried this:
Code: OxygenBasic
  1. ...
  2.  
  3.   function WndProc(hWnd, wMsg, wParam,lparam) as sys callback
  4.   ===========================================================
  5.    
  6.   static sys hdc, htools, hInstance
  7.  
  8.   static tbAddBmp as TBADDBITMAP
  9.   static tbb      as TBBUTTON
  10.   static iccx     as INITCOMMONCONTROLSEXtype
  11.  
  12. int num=7
  13. sys ie=num
  14.  
  15.   '' bitmap for each button
  16.  '  static tbBmp(7) as sys 'as BYTE
  17. static int tbBmp at getmemory ie
  18.  
  19. ...
  20.  
  21.       '' apply bitmap to toolbar buttons
  22.    '
  23.      dim i,k as LONG
  24.       for i = 0 to num-1
  25.         if i=3 then k=TBSTYLE_SEP else k=TBSTYLE_BUTTON
  26.         with tbb
  27.         .iBitmap   = tbBmp(i)
  28.         .fsState   = TBSTATE_ENABLED
  29.         .fsStyle   = k
  30.         .idCommand = -1
  31.         .dwData    = 0
  32.         end with
  33.         SendMessage( hTools, TB_ADDBUTTONS, 1, @tbb)
  34.       next
  35.     end if
  36. freememory @tbBmp
  37.  
  38.     case WM_DESTROY
  39.     '===============
  40. ...
  41.  

Now everthing works as expected. If I did everthing correctly then the dynamic short-cut for dimensioning / allocating will be helpful in other situations too.


Roland

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
Re: Question about dynamic arrays
« Reply #3 on: November 16, 2015, 07:23:00 AM »
When allocating space for int arrays you will need to scale up by 4:

Code: OxygenBasic
  1. static int tbBmp at getmemory ie*4 'sizeof int
  2.  

Arnold

  • Hero Member
  • *****
  • Posts: 872
Re: Question about dynamic arrays
« Reply #4 on: November 17, 2015, 01:51:01 AM »
Hi Charles,

out of curiosity I tried this:

int num=7

  ' bitmap for each button
   static bstring tbBmp = news num*sizeof(int)
...
     '' apply bitmap to toolbar buttons
    '
      dim i,k as LONG
      for i = 0 to num-1
...

freememory @tbBmp

This seems to work and if it is correct I would have finally understood the meaning of bstring (block of bytes, allocated in memory which must be freed at some time).

Roland

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
Re: Question about dynamic arrays
« Reply #5 on: November 17, 2015, 06:48:28 AM »

Hi Roland,

Bstrings contain the string pointer, and should be freed using one of these: (without an address operator)

frees tbBmp

freememory tbBmp

Arnold

  • Hero Member
  • *****
  • Posts: 872
Re: Question about dynamic arrays
« Reply #6 on: November 17, 2015, 08:47:08 AM »
Hi Charles,

thanks again. You explained the usage of frees and freememory clearly in the Help file but I overlooked that the @ operator was omitted. I assume that I will free something different when I use freememory / frees with an address operator but not the block itself?

Is it correct that bstring means a block of bytes? I must admit that at least in the past few years I associated string only with "Text" and was astonished when I looked in a dictionary today and there were many translations and synonyms but text was not mentioned. Somehow this was my personel blackout. In the meantime I remember that in fact I learned something different in school.

Roland

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4205
    • Oxygen Basic
Re: Question about dynamic arrays
« Reply #7 on: November 18, 2015, 07:57:47 AM »
Hi Roland,

Bstrings and Strings may contain any bytes including nulls, so they can be used for any binary data up to 2 gig in length.

Oxygen uses OleStrings/Bstr for all its dynamic memory allocations, in common with PowerBasic and thinBasic. (Under Linux it would have to be Malloc and Free).

When releasing dynamic objects and arrays, freememory requires the '@' or '&' operator, but not with individual bstrings. This is due to OxygenBasics implicit pointering.