Share your advanced PureBasic knowledge/code with the community.
jqn
User
Posts: 97 Joined: Fri Oct 31, 2003 3:04 pm
Post
by jqn » Tue Aug 08, 2006 1:57 pm
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
Guimauve
Enthusiast
Posts: 742 Joined: Wed Oct 22, 2003 2:51 am
Location: Canada
Post
by Guimauve » Tue Aug 08, 2006 2:56 pm
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
Deeem2031
Enthusiast
Posts: 216 Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:
Post
by Deeem2031 » Tue Aug 08, 2006 4:55 pm
And the string..?
irc://irc.freenode.org/#purebasic
netmaestro
PureBasic Bullfrog
Posts: 8433 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Tue Aug 08, 2006 9:37 pm
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
Posts: 468 Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland
Post
by remi_meier » Wed Aug 09, 2006 1:49 pm
I think that's not what he meant
Athlon64 3700+, 1024MB Ram, Radeon X1600
netmaestro
PureBasic Bullfrog
Posts: 8433 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Wed Aug 09, 2006 5:09 pm
Sorry - what did he mean then?
BERESHEIT
Deeem2031
Enthusiast
Posts: 216 Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:
Post
by Deeem2031 » Thu Aug 10, 2006 1:57 pm
You should free the memory of the string. If you zero the string the memory is still allocated.
irc://irc.freenode.org/#purebasic
Psychophanta
Addict
Posts: 4996 Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:
Post
by Psychophanta » Thu Aug 10, 2006 3:44 pm
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?
helpy
Enthusiast
Posts: 552 Joined: Sat Jun 28, 2003 12:01 am
Post
by helpy » Thu Aug 10, 2006 4:08 pm
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
Psychophanta
Addict
Posts: 4996 Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:
Post
by Psychophanta » Thu Aug 10, 2006 4:33 pm
Of course, the "tip" or "trick" is very ugly, unelegant and bad done.