ANOTHER question: Erasing a UDT

General FreeBASIC programming questions.
Dinosaur
Posts: 1478
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

ANOTHER question: Erasing a UDT

Post by Dinosaur »

Hi all

Hope I am keeping you all on your toes with the bombardment of questions.

In the QB days, there was no solution for this. I kept files with a blank UDT in it, and read the file if I wanted to erase the UDT.
But this is 2008, and we have FreeBasic.

Unfortunately, my searching cant find anything.
Because I have a udt's within udt's it would be long winded to zero every field. And even at startup, I cant rely on the udt being blank.

Erase UDT doesnt work.(shame)
Making a udt like

Code: Select all

Type Blank
    Fill(1 to Len(Myudt)) as Byte
End Type
and then trying to load it into the other wont work either.
Somebody out there has an easy solution, I am sure.

Regards
Loe
Posts: 323
Joined: Apr 30, 2006 14:49

Post by Loe »

dont know is this what you mean,
variant is udt right?

Code: Select all

dim as variant var0, var1
var1.vt=vt_i2
var1.ival=10

'then make var1 as variant nothing
var1=var0
as long as UDT dont have any allocated pointer, I just assign it to "unused" UDT. if it has allocated pointer then I should deallocate it first.
wolfstar
Posts: 96
Joined: Nov 07, 2006 12:42

Post by wolfstar »

how about this

Code: Select all

type myudt
    as byte x
end type

dim as myudt one , nullify

one.x = 5
? one.x
one = nullify
? one.x
sleep
beat me to it loe
Loe
Posts: 323
Joined: Apr 30, 2006 14:49

Post by Loe »

wolfstar:
beat me to it loe
depend on Dinosaur ^_^
Dinosaur
Posts: 1478
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Post by Dinosaur »

Hi all

wolfstar, I cant find any reference to "nullify".
Your suggestion certainly appears to work.I tested quickly with 3 layer nested udt's and it cleared the values.

Any idea where I can find details on "nullify" ??
Many thanks.

Regards
wolfstar
Posts: 96
Joined: Nov 07, 2006 12:42

Post by wolfstar »

It's just a variable name I chose to use. Maybe I should've used "two" to make it clearer. Just as long as your blank UDT has the same elements as the one you're using, you can just copy the blank over the other.

same as a normal variable

Code: Select all

dim as integer x , blank  'or dim as mytype x , blank
x =5
blank= 0
x = blank
McLovin
Posts: 82
Joined: Oct 21, 2008 1:15
Contact:

Post by McLovin »

Having a way to reinitialize a TYPE sounds like a good feature to have built into the compiler. The function would need to take into account field padding as well I guess. Something like TYPECLEAR or TYPEINIT or TYPERESET..... ?
wolfstar
Posts: 96
Joined: Nov 07, 2006 12:42

Post by wolfstar »

there's also Clear, I just noticed..

Code: Select all

declare sub print_type_elems

type atype
    as integer z
end type

type utype
    as integer x
    as byte f
    as atype a
end type

dim shared as utype udt

udt.x = 2147483647
udt.f = -1 
udt.a.z = -2147483647

print_type_elems

clear udt , , sizeof(udt)

print_type_elems

sleep

sub print_type_elems
    ? udt.x
    ? udt.f
    ? udt.a.z
end sub
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

McLovin wrote:Having a way to reinitialize a TYPE sounds like a good feature to have built into the compiler. The function would need to take into account field padding as well I guess. Something like TYPECLEAR or TYPEINIT or TYPERESET..... ?
FB already has this...its called Erase.

Code: Select all

Type _MyUDT
   foo as string
   bar as integer
EndType

Dim MyUDT as _MyUDT

'Put stuff in the UDT
'...

'now erase it
Erase MyUDT

'All fields are now cleared!
-Vince
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Erase only works on arrays, not variables.
(Heh, "erase arrays"...)
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

counting_pine wrote:Erase only works on arrays, not variables.
(Heh, "erase arrays"...)
Ooops. Sorry. My VB days are showing!

Could you use this?

Code: Select all

Clear @MyUDT,,SizeOf(MyUDT)
-Vince
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

Post by Antoni »

Using CLEAR on UDT's will work correctly if they are fixed length. If one or more of the fields is a string you will erase the string descriptor but will not reclaim the memory used by the string.
To correctly erase an udt just add to it a member function setting the fields to zero or null string.
Dinosaur
Posts: 1478
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Post by Dinosaur »

Hi all

Wolfstar , I didnt recognise that nullify was just a name. I Normally use the same name,(Common Shared Myudt As Myudt) thats why it threw me.
But it is therefore possible that at startup the udt will have garbage in it. I also had not tested it on strings.

Clear is an instruction that I associated with clearing memory at startup in QB. "Clear ,,,20000"
But still it works both ways (like vdecampo suggested).
Using CLEAR on UDT's will work correctly if they are fixed length. If one or more of the fields is a string you will erase the string descriptor but will not reclaim the memory used by the string.
Testing that, shows that both types of strings clear. The Len of the variable length string reports as 0 after Clear. But I will combine the two methods to avoid losing memory , as I may be clearing udt's hundreds of times in a day.
So, Clear a copy of the udt, and keep it to clear the source udt when needed.

Now all I have to do, is verify it will clear nested udt's

Many thanks for all the help, I guess there is a clear instruction for udt's after all.

Regards
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Dinosaur wrote:Testing that, shows that both types of strings clear. The Len of the variable length string reports as 0 after Clear.
Both will leave you with blank strings, but Clearing a var-len string descriptor will zero out its data pointer, leaving you with no way to deallocate the string data it was pointing to.

The Clear method shouldn't be used for complex UDTs, because Clear can't clean up anything tied up in the current UDT's state.
My recommend is to give your UDT a dedicated clearing method that does all the necessary zeroing/cleaning up.
Even if it just calls Clear, it means you don't have to worry about what it does.

Code: Select all

type simple_udt
    as integer a, b
    declare sub clearme()
end type

sub simple_udt.clearme()
    clear this, , sizeof(this)
end sub


type complex_udt
    a as integer
    b as string
    c as simple_udt
    declare sub clearme()
end type

sub complex_udt.clearme()
    a = 0
    b = ""
    c.clearme()
end sub

dim foo as complex_udt

foo.a = 25
foo.b = "bar"
foo.c = type( 42, 255 )

print foo.a, foo.b, foo.c.a, foo.c.b

foo.clearme()

print foo.a, foo.b, foo.c.a, foo.c.b
By the way, in case you haven't realised, Clear's meaning in FB is completely separate from its QB meaning.
Neither usage works in the other language.
http://www.freebasic.net/wiki/KeyPgClear
McLovin
Posts: 82
Joined: Oct 21, 2008 1:15
Contact:

Post by McLovin »

If your Type doesn't contain any variable strings then a simple method to clear it would be to use the C function MemSet.

Code: Select all

#include "crt\string.bi"

type MyType1
   st1 as string * 50
   i1  as integer
   d1  as double
end type

type MyType2
   st2 as string * 50
   i2  as integer
   d2  as double
   MyT1 as MyType1
end type


dim t as MyType2
t.st2 = "My String1"
t.i2  = 1
t.d2  = 2
t.MyT1.st1 = "My String2"
t.MyT1.i1  = 11
t.MyT1.d1  = 22


print t.st2; t.i2; t.d2; t.MyT1.st1; t.MyT1.i1; t.MyT1.d1
sleep

MemSet @t, 0, len(t)

print t.st2; t.i2; t.d2; t.MyT1.st1; t.MyT1.i1; t.MyT1.d1
sleep

----
Fogell
Post Reply