ClearStructure(..., ..., #PB_Structure_ClearNested)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

ClearStructure(..., ..., #PB_Structure_ClearNested)

Post by srod »

Hi,

just updating some code to use some of the new features (PB 4.5), in particular discarding some code for clearing dynamic structures in favour of the new ClearStructure() etc.

Came across some nested structure pointers in which it would be nice ( :) ) if ClearStructure() could be persuaded to do it all for me in one swoop!

Take the following structure for example :

Code: Select all

Structure test
  a$
  *ptrString.STRING
EndStructure
And the following code which allocates two chunks of memory, one for a 'test' structure and one for a STRING structure :

Code: Select all

*ptr.test = AllocateMemory(SizeOf(test))
If *ptr
  *ptr\a$ = "Hello matey!"
  *ptr\ptrString = AllocateMemory(SizeOf(STRING))
  If *ptr\ptrString
    *ptr\ptrString\s = "Hello again!"
  EndIf
EndIf
Now as things stand, the only way of tidying this memory up is via two calls to ClearStructure() as follows :

Code: Select all

ClearStructure(*ptr\ptrString, STRING)
ClearStructure(*ptr, test)
No great hardship I admit. :)

I was just wondering, however, if it would be viable to add some optional parameter to ClearStructure() so that it could run through and clear both structures in one go? I would then be able to replace my two previous calls to ClearStructure() with something like :

Code: Select all

ClearStructure(*ptr, test, #PB_Structure_ClearNested)
With this optional parameter specified, all pointers to structures (of explicit types) could be tidied up by PB in some recursive manner. Course this would have to be used with great care and would only be useable if all such structure pointers pointed to some dynamically allocated memory rather than some global/static structure etc, but in my case that is invariably the case anyhow! :)

Thanks.
I may look like a mule, but I'm not a complete ass.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: ClearStructure(..., ..., #PB_Structure_ClearNested)

Post by helpy »

Hi srod,

and who will free the allocated memory of the STRING structure? To free all of the structure this should be done:

Code: Select all

ClearStructure( *ptr\ptrString, STRING )
FreeMemory( *ptr\ptrString )
ClearStructure( *ptr, test )
FreeMemory( *ptr )
But with ...

Code: Select all

ClearStructure( *ptr, test, #PB_Structure_ClearNested )
... *ptr/ptrString would be set to #Null ... so you would have to store the memory pointer in a temporary variable, therfore you can free the allocated memory!

See:

Code: Select all

Structure test
  a$
  *ptrString.STRING
EndStructure

*ptr.test = AllocateMemory(SizeOf(test))
If *ptr
  *ptr\a$ = "Hello matey!"
  *ptr\ptrString = AllocateMemory(SizeOf(STRING))
  If *ptr\ptrString
    *ptr\ptrString\s = "Hello again!"
  EndIf
EndIf

ClearStructure( *ptr, test )
Debug *ptr\ptrString   ; ==> Result is #Null !!!
cu,
guido
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: ClearStructure(..., ..., #PB_Structure_ClearNested)

Post by srod »

Good point, I knew there was something I was overlooking! Something was nagging away at me! :)

Oh well, teach me to be so lazy! :wink:
I may look like a mule, but I'm not a complete ass.
Post Reply