It is currently Sat Jan 23, 2021 6:51 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Clear Structures
PostPosted: Tue Aug 08, 2006 1:57 pm 
Offline
User
User
User avatar

Joined: Fri Oct 31, 2003 3:04 pm
Posts: 97
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:
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Aug 08, 2006 2:56 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Oct 22, 2003 2:51 am
Posts: 743
Location: Canada
Little bit more simple.

Regards
Guimauve

Code:
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Aug 08, 2006 4:55 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Sep 20, 2003 3:57 pm
Posts: 216
Location: Germany
And the string..?

_________________
irc://irc.freenode.org/#purebasic


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Aug 08, 2006 9:37 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8149
Location: Fort Nelson, BC, Canada
Quite right, the pointer to it is gone but the string is still there. This should zero the string as well:
Code:
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

_________________
BERISHEET


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 09, 2006 1:49 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Dec 20, 2003 6:19 pm
Posts: 468
Location: Switzerland
I think that's not what he meant :wink:

_________________
Athlon64 3700+, 1024MB Ram, Radeon X1600


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 09, 2006 5:09 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8149
Location: Fort Nelson, BC, Canada
Sorry - what did he mean then?

_________________
BERISHEET


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 10, 2006 1:57 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Sep 20, 2003 3:57 pm
Posts: 216
Location: Germany
You should free the memory of the string. If you zero the string the memory is still allocated.

_________________
irc://irc.freenode.org/#purebasic


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 10, 2006 3:44 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4655
Location: Spa, relaxing and thinking, and learning...
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 10, 2006 4:08 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Jun 28, 2003 12:01 am
Posts: 531
BUT! If you clear the string pointer in a structure, you produce a memory leak!

Test this:
Code:
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 10, 2006 4:33 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4655
Location: Spa, relaxing and thinking, and learning...
Of course, the "tip" or "trick" is very ugly, unelegant and bad done. :?

_________________
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye