Deprecated: Array and string offset access syntax with curly braces is deprecated in /homepages/21/d38531796/htdocs/jose/smfforum/Sources/Subs.php on line 3825
global variables

Author Topic: global variables  (Read 19803 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • User-Rate: +204/-0
Re: global variables
« Reply #30 on: September 24, 2007, 03:54:12 PM »
 
The "trick" is to use Automation compatible types, e.g. BSTR instead of ASCIIZ, safe arrays instead of other kind of arrays, object variables instead of pointers to custom allocated memory, etc. VariantClear knows how to manage them.

For a VT_BSTR variant, you allocate an string with SysAllocString and store the returned pointer in the structure. VariantClear will call SysFreeString to free the memory used by the string.

For a safe array, you allocate the memory and manage the elements using the API safe arrays functions. VariantClear will call SafeAerrayDestroy, that knows how to free the memory used by the array since a safe array includes information about the kind of data stored and the number of elements.

For an object variable, VariantClear will call the Release method of the object.

Now, if you want to use other kind of strings or arrays, then you will have to free the memory yourself, because VariantClear has no means to know how to do it.

For a perfect solution, just include another of my suggestions: native support for safe arrays.
« Last Edit: September 24, 2007, 05:21:26 PM by José Roca »

Online Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 683
  • User-Rate: +27/-1
    • Charles Pegge
Re: global variables
« Reply #31 on: September 24, 2007, 04:21:34 PM »

Oops! I missed your COM post Jose, this topic is moving too fast!

COM and variants do not prevail in the Linux world, but the Variant structure is generic enough to be an open standard, and a very comprehensive one. Do we know if Microsoft considers the Variant and COM system as being Microsoft property. I find myself using very similar structures in $ and R$ to handle different data types and could very well adopt at least some of the standard. It would certainly improve interoperability, for low-level coding.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • User-Rate: +204/-0
Re: global variables
« Reply #32 on: September 24, 2007, 04:40:01 PM »
 
You even can have structures containing variants, objects, etc., inside a variant. For that, you will need to learn some low-level COM programming and use the IRecordInfo interface. The possibilities are enormous...

Online Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 683
  • User-Rate: +27/-1
    • Charles Pegge
Re: global variables
« Reply #33 on: September 24, 2007, 05:07:23 PM »

Coming back to this global thing, most of my coding uses simple arrays of global strings. You can put dynamic structures inside strings and pass them anywhere by index. Then  memory allocation /  deallocation is never an issue. The string array can also be used as a stack, so that individual string elements are available for reuse.

The DIM .. AT .. when used in conjunction with strings is an especially useful feature of PowerBasic. - something I miss in FB.