Mixed datatype

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Postby duke4e » Mar 16, 2008 3:21

cha0s wrote:If you want to chat using your native languages...

Lachie and I speak in Croatian, while Krcko speaks in Serbian. Well, that makes us speak in unversal language which is not english.

Just kidding, Croatian and Serbian and 97.6% similar languages :)

Well, it seams that it's time to move this chat somewhere else...
krcko
Posts: 163
Joined: Jul 30, 2006 0:34
Location: Serbia
Contact:

Postby krcko » Mar 16, 2008 4:11

where did you just found that percent? :)

well, cha0s is rigth we should speak on english, and we will from now on, but it was great speaking on our native language for a while :)
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Postby duke4e » Mar 16, 2008 16:57

97.6% is a random number. Well, it was a joke.
krcko
Posts: 163
Joined: Jul 30, 2006 0:34
Location: Serbia
Contact:

Postby krcko » Mar 16, 2008 17:05

i know it was a joke :)
srvaldez
Posts: 2578
Joined: Sep 25, 2005 21:54

Re: Mixed datatype

Postby srvaldez » Jun 16, 2020 14:53

would anyone take a look at first post on this thread and try to make the code 64-bit clean?
as it is, it crashes if compiled to 64-bit
if I comment-out all the code dealing with fbInteger and Integer then it compiles and runs OK in 64-bit, the problem for me is that the code is too complex for me to understand and to fix
fxm
Posts: 10046
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Mixed datatype

Postby fxm » Jun 16, 2020 15:24

In fact it's a general bug (not specific to 64-bit compilation):
When memory is allocated for a string descriptor, this memory must also be cleared before using the string.

Line #158 of mixed.bi:
change
This.StoredData = Allocate(SizeOf(String))
with
This.StoredData = Callocate(SizeOf(String))

I have for long time noticed that a 64-bit compilation is more susceptible (by crashing) to coding bugs than a 32-bit compilation.
srvaldez
Posts: 2578
Joined: Sep 25, 2005 21:54

Re: Mixed datatype

Postby srvaldez » Jun 16, 2020 15:49

@fxm
thank you very much, I made the change and no more crashes, thank you
can you expand on this?
why is it important for the memory to be cleared?
MrSwiss
Posts: 3657
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Mixed datatype

Postby MrSwiss » Jun 16, 2020 15:58

srvaldez wrote:why is it important for the memory to be cleared?
To assure proper termination of the string.
Unless you want to manually add a Chr(0) after the string-data.
MrSwiss
Posts: 3657
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Mixed datatype

Postby MrSwiss » Jun 16, 2020 16:06

fxm wrote:I have for long time noticed that a 64-bit compilation is more susceptible (by crashing) to coding bugs than a 32-bit compilation.
From my point of view, another advantage, that speaks for FBC 64.
IMO, FBC 32 should also be crashing, when 'buggy' code is compiled.
srvaldez
Posts: 2578
Joined: Sep 25, 2005 21:54

Re: Mixed datatype

Postby srvaldez » Jun 16, 2020 16:07

thanks MrSwiss
it makes sense, but would be great if the compiler would give a warning
fxm
Posts: 10046
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Mixed datatype

Postby fxm » Jun 16, 2020 16:14

MrSwiss wrote:
srvaldez wrote:why is it important for the memory to be cleared?
To assure proper termination of the string.
Unless you want to manually add a Chr(0) after the string-data.

Not exactly.
This resetting to 0 concerns the string descriptor and not the string character data which do not yet exist.
A null string descriptor correspond to a string empty which is the right starting point before any string assignment.

For more explanation, see the ALLOCATE documentation page.
srvaldez
Posts: 2578
Joined: Sep 25, 2005 21:54

Re: Mixed datatype

Postby srvaldez » Jun 16, 2020 16:25

thank you again fxm, you are my hero :-)
seriously, I very much appreciate all you do to help and to keep the documentation up-to-date
MrSwiss
Posts: 3657
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Mixed datatype

Postby MrSwiss » Jun 16, 2020 16:27

Thanks fxm. I was already 1 step ahead (string-data memory allocation).
This should also use: CAllocate (preferred over Allocate).
(for the previously stated reasons)
fxm
Posts: 10046
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Mixed datatype

Postby fxm » Jun 16, 2020 16:38

Other change to avoid memory leak:
Assign a null string to the used string before deallocating its descriptor.

Code: Select all

.....
   Destructor Mixed
      If This.StoredType <> fbEmpty And this.StoredType <> fbAnyPointer Then
         If This.StoredType = fbString Then
            *Cast(String Pointer, This.StoredData) = ""
         EndIf
         DeAllocate This.StoredData
      EndIf
   End Destructor
.....
   Operator Mixed.Let(Value As String)
      If This.StoredType <> fbEmpty And This.StoredType <> fbAnyPointer Then
         If This.StoredType = fbString Then
            *Cast(String Pointer, This.StoredData) = ""
         EndIf
         DeAllocate This.StoredData
      EndIf
      This.StoredType = fbString
      This.StoredData = CAllocate(SizeOf(String))
      *Cast(String Pointer, This.StoredData) = value
   End Operator
.....
fxm
Posts: 10046
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Mixed datatype

Postby fxm » Jun 16, 2020 16:44

MrSwiss wrote:I was already 1 step ahead (string-data memory allocation).
This should also use: CAllocate (preferred over Allocate).
(for the previously stated reasons)

Yes, but when allocating memory for a Zstring for example.
For a var-len string the allocation for string character data is internally handled by the different string operators.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 4 guests