Clear Structures

Share your advanced PureBasic knowledge/code with the community.
User avatar
jqn
User
User
Posts: 97
Joined: Fri Oct 31, 2003 3:04 pm

Clear Structures

Post 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
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Post 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 
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Post by Deeem2031 »

And the string..?
irc://irc.freenode.org/#purebasic
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
BERESHEIT
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

I think that's not what he meant :wink:
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Sorry - what did he mean then?
BERESHEIT
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Post by Deeem2031 »

You should free the memory of the string. If you zero the string the memory is still allocated.
irc://irc.freenode.org/#purebasic
User avatar
Psychophanta
Addict
Addict
Posts: 4976
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Post 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?
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post 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
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
Psychophanta
Addict
Addict
Posts: 4976
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Post by Psychophanta »

Of course, the "tip" or "trick" is very ugly, unelegant and bad done. :?
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply