Page 1 of 1

Clear Structures

Posted: Tue Aug 08, 2006 1:57 pm
by jqn
Code updated for 5.20+ (better to use ClearStructure(), especially when the structure contains strings, array, list or map which have been allocated internally by PureBasic)

Hi,
A little piece of code:

Code: Select all

Structure mystruc
  field1.s
  field2.l
  field3.q
  field4.f
  field5.d
EndStructure

Macro showit
  PrintN(Str(Len(mystruc\field1))+" : "+mystruc\field1)
  PrintN(Str(mystruc\field2))
  PrintN(Str(mystruc\field3))
  PrintN(StrF(mystruc\field4))
  PrintN(StrD(mystruc\field5))
  PrintN("size "+Str(SizeOf(mystruc)))
  PrintN("")
EndMacro

Global mystruc.mystruc

OpenConsole()
mystruc\field1 = "STRING DATA FIELD 1"
mystruc\field2 = 2006
mystruc\field3 = 123456789
mystruc\field4 = 987654321
mystruc\field5 = -987654321
PrintN("BEFORE")
showit

base = @mystruc
For n=0 To SizeOf(mystruc)-1
  PokeB(base+n,0)
Next
PrintN("AFTER")
showit

Input()
End

Posted: Tue Aug 08, 2006 2:56 pm
by Guimauve
Little bit more simple.

Regards
Guimauve

Code: Select all

Structure mystruc
   field1.s
   field2.l
   field3.q
   field4.f
   field5.d
EndStructure

Macro showit
   PrintN(Str(Len(mystruc\field1))+" : "+mystruc\field1)
   PrintN(Str(mystruc\field2))
   PrintN(StrQ(mystruc\field3))
   PrintN(StrF(mystruc\field4))
   PrintN(StrD(mystruc\field5))
   PrintN("size "+Str(SizeOf(mystruc)))
   PrintN("")
EndMacro

Global mystruc.mystruc

OpenConsole()
mystruc\field1 = "STRING DATA FIELD 1"
mystruc\field2 = 2006
mystruc\field3 = 123456789
mystruc\field4 = 987654321
mystruc\field5 = -987654321
PrintN("BEFORE")
showit

; base = @mystruc
; For n=0 To SizeOf(mystruc)-1
   ; PokeB(base+n,0)
; Next

RtlZeroMemory_(mystruc, SizeOf(mystruc))

PrintN("AFTER")
showit

Input()
End 

Posted: Tue Aug 08, 2006 4:55 pm
by Deeem2031
And the string..?

Posted: Tue Aug 08, 2006 9:37 pm
by netmaestro
Quite right, the pointer to it is gone but the string is still there. This should zero the string as well:

Code: Select all

Structure mystruc 
   field1.s 
   field2.l 
   field3.q 
   field4.f 
   field5.d 
EndStructure 

Macro showit 
   PrintN(Str(Len(mystruc\field1))+" : "+mystruc\field1) 
   PrintN(Str(mystruc\field2)) 
   PrintN(StrQ(mystruc\field3)) 
   PrintN(StrF(mystruc\field4)) 
   PrintN(StrD(mystruc\field5)) 
   PrintN("size "+Str(SizeOf(mystruc))) 
   PrintN("") 
EndMacro 

Global mystruc.mystruc 

OpenConsole() 
mystruc\field1 = "STRING DATA FIELD 1" 
mystruc\field2 = 2006 
mystruc\field3 = 123456789 
mystruc\field4 = 987654321 
mystruc\field5 = -987654321 
PrintN("BEFORE") 
showit 

RtlZeroMemory_(@mystruc\field1, Len(mystruc\field1))
RtlZeroMemory_(@mystruc, SizeOf(mystruc)) 

PrintN("AFTER") 
showit 

Input() 
End 

Posted: Wed Aug 09, 2006 1:49 pm
by remi_meier
I think that's not what he meant :wink:

Posted: Wed Aug 09, 2006 5:09 pm
by netmaestro
Sorry - what did he mean then?

Posted: Thu Aug 10, 2006 1:57 pm
by Deeem2031
You should free the memory of the string. If you zero the string the memory is still allocated.

Posted: Thu Aug 10, 2006 3:44 pm
by Psychophanta
Deeem2031 wrote:You should free the memory of the string. If you zero the string the memory is still allocated.
The topic said "clear", but not to free the structure.
And BTW, about this thread: is this a trick or a sort of a "Hello world!" example?

Posted: Thu Aug 10, 2006 4:08 pm
by helpy
BUT! If you clear the string pointer in a structure, you produce a memory leak!

Test this:

Code: Select all

Macro Pause
	MessageRequester("PAUSE", "Check memory usage in task manager")
EndMacro

Structure test
	l.l
	StructureUnion
		s.s
		ps.l
	EndStructureUnion
EndStructure

#TestLoop = 1024 * 1024

Pause
For i = 1 To #TestLoop
	a.test\s = Space(32)
	a\ps = #Null
Next i
Pause

Posted: Thu Aug 10, 2006 4:33 pm
by Psychophanta
Of course, the "tip" or "trick" is very ugly, unelegant and bad done. :?