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

Re: Structures with byte arrays using fromstring cause segfault


Well I think it is all me, fromstring is static, perhaps it only returns an array does not set an array.

I seem to have know that at some point in time.

Oh well, I guess I was wrong.

On 5/19/24 18:05, Jussi Lahtinen wrote:
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

--
~~~~ Brian

Attachment: OpenPGP_0x78BFB26402F48419.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


Follow-Ups:
Re: Structures with byte arrays using fromstring cause segfaultJussi Lahtinen <jussi.lahtinen@xxxxxxxxx>
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>
Re: Structures with byte arrays using fromstring cause segfaultJussi Lahtinen <jussi.lahtinen@xxxxxxxxx>