ANOTHER question: Erasing a UDT

General FreeBASIC programming questions.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Nov 19, 2008 23:27

For simple UDTs, I would recommend something less intrusive, like placement operator New:

Code: Select all

type SimpleUDT
    as integer a, b
end type

' allocate/construct object.
dim x as SimpleUDT

' ...

' reconstruct object.
var ignore_address = new(@x) SimpleUDT


For complex UDTs, I would recommend calling the destructor explicitly to properly destroy the object, then calling placement operator New as for simple types:

Code: Select all

type ComplexUDT
    as string c, d
end type

' allocate/construct object.
dim y as ComplexUDT

' ...

' destroy and reconstruct the object.
y.destructor
var tmp = new(@y) ComplexUDT


This not only does this do the right thing for both simple and complex types (even those with other simple/complex member data), but it also allows to construct objects which have constructors declared properly (by calling their constructors).

McLovin, the types of member data is only part of the story. Most obviously, if the object has sole ownership of any resources -- whether through a pointer or an opaque "handle" -- those resources must must be freed -- either by manual memory management or perhaps through a call to a resource-manager API.

Dinosaur, in my experience, it's best to look at objects as black boxes and try not to be concerned with their internal structure. It seems to me that it might be helpful to rethink where the object is declared. If you need it to be initialized at the start of a loop, then it might make sense to declare it there, where it will automatically be initialized.
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 20, 2008 1:14

Hi all

Stylin, thank you for your input.
I am getting an error when I put the destructor statement anywhere.
For example

Code: Select all

Dim Y As Task      
Y.destructor
Var tmp = New(@Y) Task
For Xq = 1 To 10
    If Queue.MC_Nbr(Xq)   = Caller Then Exit For   
    If Queue.MC_Nbr(Xq)   = 0 Then
        Queue.MC_Nbr(Xq)  = Caller                  '
        Queue.ReqTime(Xq) = Times.MSec
        Queue.Task(Xq)    = Y         
        Queue.Entries += 1
        Exit For
    EndIf
Next
Y.destructor
This creates an error
Error: symbol `.Lt_049E' is already defined
As soon as I rem out "Y.destructor" there is no error.
Dinosaur, in my experience, it's best to look at objects as black boxes and try not to be concerned with their internal structure. It seems to me that it might be helpful to rethink where the object is declared. If you need it to be initialized at the start of a loop, then it might make sense to declare it there, where it will automatically be initialized.

I declare the individual udt's in a .bi file, but when it is time to use a blank complext udt, I declare it right where it is needed(as in the example above) Using "Clear" in a similar manner seems to work, but there is doubt about the use of that.(as pointed above)
If I declare a copy of a complex udt just before I need it as per:

Code: Select all

Dim Y As Task
and yet leaving out the New & destructor statements, will that make Y a blank copy of the Task udt, or will it inherit the values ? (or will it be undecided)

Regards
srvaldez
Posts: 2527
Joined: Sep 25, 2005 21:54

Postby srvaldez » Nov 20, 2008 1:35

could you please explain the line

Code: Select all

var tmp = New(@y) ComplexUDT

what does it do?
it looks like it's the equivalent of

Code: Select all

dim tmp as ComplexUDT ptr
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 20, 2008 2:20

Hi all

As I am not the author of the line, and have never used the New function, my interpretation is that the Function "New" returns a value "tmp" when it creates a copy of ComplexUDT called y.

Your equivalent actually makes tmp a pointer to the existing ComplexUDT

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

Postby Loe » Nov 20, 2008 4:29

@Dinosaur
But it is therefore possible that at startup the udt will have garbage in it.


what happen when we dim myvar as myudt?
is it callocate or allocate???
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 20, 2008 5:45

Hi all

Loe, I cant answer that question.(dont know)
But I can see where you are going with that.
If it was callocate, it cleares the memory.(so no need to clear the udt)

Would be nice if that was the case.

Regards
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Nov 20, 2008 18:12

Loe, as far as I understand it, the memory for variables declared with Dim are allocated on the stack, and are initialized when they are declared (at run-time). If no initializer is given, then this means that the memory is initialized to zeros (for simple types) or initialized by calling the default constructor (for complex types).

The memory for variables declared with Dim Shared (without an initializer) is allocated in the .bss section of the executable, which the linker/loader initializes with zeros before execution (before static object constructors are called, implicit main, etc.). The same is true for variables declared with Static and Static Shared without an initializer.

So, the following program will print all zeros:

Code: Select all

for i as integer = 1 to 5
    ' n is allocated once on the stack,
    ' and initialized to zero each iteration.
    dim n as integer
    print n
    n += 1
next
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Postby bcohio2001 » Dec 05, 2008 15:39

Loe wrote: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.


Seeing this.... can you read/write a file.
[Example]

Code: Select all

type Gen
  A as short
  B as long
  C as string
end type
dim shared as Gen Array(1 to 30)

dim as byte fh,l
fh = freefile
open "dummy.dat" for input as #fh
for l = 1 to 30
  input #fh, Array(l)
next
close


And writing would be similar.
Loe
Posts: 323
Joined: Apr 30, 2006 14:49

Postby Loe » Dec 06, 2008 1:27

@bcohio2001
yupz, your method could use to reset/store initial value of an/array of UDT, by read/write its initial value to/from the file.

but as the topic is erasing an UDT, what I want to say is an easiest way, by assigning erasing UDT with unused UDT with the same type.
and if the UDT contain allocated pointer, we should deallocated pointer first to avoid memory leak.

Code: Select all

Type nonallocated
    a as integer
    b as integer
    c as integer
End type

Type allocated
    a as integer
    b as integer
    c as integer ptr
End type

dim as nonallocated VN1,VN0
dim as allocated V1,V0

'assign non allocated
VN1.a=1:VN1.b=2:VN1.c=3

'assign allocated
V1.a=1:V1.b=2:V1.c=allocate(sizeof(Integer))
*V1.c=3

'to erasing VN1 we just have assign it with unused VN0
VN1=VN0

'but we cant erase V1 with assign it with unused V0, because there will be a  memory leak, we should deallocate V1.c first, then assign it with V0

deallocate(V1.c)
V1=V0
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Dec 06, 2008 5:40

Hi all

but as the topic is erasing an UDT, what I want to say is an easiest way, by assigning erasing UDT with unused UDT with the same type.


And that is exactly what I have done, and as I dont use pointers in my udt's, it is easy to do.

Regards
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Postby yetifoot » Dec 06, 2008 10:48

AFAIK, in FB, Clear is just an alias to memset, same as Allocate is an alias to malloc, and Deallocate is an alias to free.
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Postby bcohio2001 » Dec 07, 2008 2:20

@Loe

I understood that the "main" topic was "Erasing UDT's" quickly. But I did "kind of" stay with the topic with my question. I just didn't want to start a whole new topic just for my question. I have seen many times on here where they completely get off topic and you completely loose the flow of the topic.

Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests