[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Structures with byte arrays using fromstring cause segfault


I think fromstring() is just incompatible with normal arrays.

Jussi

On Mon, May 20, 2024 at 4:02 AM Jussi Lahtinen <jussi.lahtinen@xxxxxxxxx>
wrote:

> Here is your code corrected:
>
> Public Struct demo
>      tag[100] As Byte
> End Struct
>
> Public dtag As Struct Demo
> Public theString As String = "This is a test string tag"
>
> Public Sub main()
>
>      dtag.tag.fromstring(theString)
>
>      Print "Tag="; dtag.tag.tostring()
>
>      For i As Integer = 0 To theString.len - 1
>         dtag.tag[i] = Asc(theString[i])
>      Next
>      dtag.tag[i + 1] = 0
>
>      Print "Tag="; dtag.tag.tostring()
>
> End
>
> However, the fromstring() function still does nothing.
>
>
> Jussi
>
>
> On Sun, May 19, 2024 at 11:36 PM Brian G <brian@xxxxxxxxxxxxxxxx> wrote:
>
>> When Working with structures with byte arrays, the copy of a string to
>> the byte array
>> using fromstring() appears to corrupt memory
>>
>> But using the tostring() seems to work correctly.
>>
>> I have included  a simple script to demonstrate the issue.
>> The first line does a fromstring() and initially appears to do nothing.
>>
>> But the last line that frees the allocated memory causes a segfault
>> sometimes.
>>
>> Removing the line doing the fromstring() makes everything ok on the free.
>>
>> Maybe I am using this in an incorrect way?
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> #!/usr/bin/env gbs3
>>
>> public struct demo
>>      tag[100] as byte
>> end struct
>>
>> public dtag as demo = alloc(400)
>> public theString as string = "This is a test string tag"
>>
>> public sub main()
>>      dtag.tag.fromstring(theString)
>>
>>      print "Tag=";dtag.tag.tostring() ' prints nothing and in fact the
>> content is not changed.
>>
>>      for i as integer = 0 to theString.len-1
>>         dtag.tag[i] = asc(theString[i])
>>      next
>>      dtag.tag[i+1]=0
>>
>>      print "Tag=";dtag.tag.tostring()
>>
>>      free(varptr(dtag))
>> end
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
>> --
>> ~~~~ Brian
>>
>>

Follow-Ups:
Re: Structures with byte arrays using fromstring cause segfaultBrian G <brian@xxxxxxxxxxxxxxxx>
References:
Structures with byte arrays using fromstring cause segfaultBrian G <brian@xxxxxxxxxxxxxxxx>
Re: Structures with byte arrays using fromstring cause segfaultJussi Lahtinen <jussi.lahtinen@xxxxxxxxx>