Qbasicnews.com
Pointer Bug? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Pointer Bug? (/thread-6195.html)



Pointer Bug? - lkd85 - 02-22-2005

The following indexing code crashes (can't read memory error):
Code:
Type Vert
     x As Single
     y As Single
     z As Single
End Type

Type Tri
     verts(2) As Vert
End Type

Type Object
     numoftris As Integer
     tris As Tri Ptr
End Type

Dim obj As Object

With obj
     .numoftris = 7
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris
        For v = 0 To 2
            .tris[i].verts(v).x = Rnd * 23
            .tris[i].verts(v).y = Rnd * 75
            .tris[i].verts(v).z = Rnd * 51
        Next v
     Next i
End With

Print obj.numoftris, obj.tris[0].verts(0).y '<- Error here


Sleep

But doesn't with a minor change to print the values as they're being set:

Code:
Type Vert
     x As Single
     y As Single
     z As Single
End Type

Type Tri
     verts(2) As Vert
End Type

Type Object
     numoftris As Integer
     tris As Tri Ptr
End Type

Dim obj As Object

With obj
     .numoftris = 7
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris
        For v = 0 To 2
            .tris[i].verts(v).x = Rnd * 23
            .tris[i].verts(v).y = Rnd * 75
            .tris[i].verts(v).z = Rnd * 51
            Print .tris[i].verts(v).x  '<- Change
        Next v
     Next i
End With

Print obj.numoftris, obj.tris[0].verts(0).y '<- Reads fine with print in loop


Sleep

What's wrong? :o


Pointer Bug? - steven_basic - 02-22-2005

I don't get an error on either one.

WinXP Pro SP2


Pointer Bug? - v3cz0r - 02-22-2005

Yeah, there was a bug with 0.11 when calculating the len of types that have array fields (reported by Dr_D), it was fixed 2 weeks ago, the patch will be on the next release.

That code worked fine with version 0.12.

Btw, it should be: for i = 0 To .numoftris-1


Pointer Bug? - lkd85 - 02-22-2005

Got it. Thanks.


Pointer Bug? - lkd85 - 02-23-2005

One more thing, there also seems to be another bug in assigning variables to pointer indexes. This time the compiler crashes.:

Code:
Type Vert
     x As Single
     y As Single
     z As Single
End Type

Type Tri
     verts(2) As Vert
End Type

Type Object
     numoftris As Integer
     tris As Tri Ptr
End Type

Dim obj As Object

With obj
     .numoftris = 7
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris - 1
        For v = 0 To 2
            .tris[i].verts(v).x = Rnd * 23
            .tris[i].verts(v).y = Rnd * 75
            .tris[i].verts(v).z = Rnd * 51
        Next v
     Next i
End With

newfile = FreeFile

Open "data.obj" For Binary As #newfile

Put #newfile,,obj.numoftris

For i = 0 To obj.numoftris - 1
    Put #newfile,,obj.tris[i].verts()
Next i

Close #newfile

Deallocate(obj.tris)

Dim newobj As Object, temptri As Tri

readfile = FreeFile
Open "data.obj" For Binary As #readfile

Get #readfile,,newobj.numoftris
Print newobj.numoftris
newobj.tris = Allocate(newobj.numoftris * Len(Tri))
For i = 0 To newobj.numoftris - 1
    Get #readfile,,newobj.tris[i].verts() '<- Crashes here, compiles when commented out.
    Print "X: ", newobj.tris[i].verts(0), "Y: ", newobj.tris[i].verts(1), "Z: ", newobj.tris[i].verts(2)
Next i
Close #readfile

Sleep

Even using a temp variable to read in a triangle (temptri), assigning it to the pointer index fails.


Pointer Bug? - v3cz0r - 02-23-2005

Problem is at Print "X: ", newobj.tris[i].verts(0), "Y: ", newobj.tris[i].verts(1), "Z: ", newobj.tris

TYPE's can't be printed, but PRINT and WRITE are not checking for that, new check added..


Pointer Bug? - lkd85 - 02-23-2005

LOL, are you serious? :oops: I really, really need to proofread my code more, Thanks.

The test works now:

Code:
With newobj
     Get #readfile,,.numoftris
     Print "Number of triangles: "; .numoftris
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris - 1
         Get #readfile,,.tris[i].verts()
         Color i + 10
         Print "Triangle: ";i
         Print "X: "; .tris[i].verts(0).x; " Y: "; .tris[i].verts(0).y; " Z: "; .tris[i].verts(0).z
         Print "X: "; .tris[i].verts(1).x; " Y: "; .tris[i].verts(1).y; " Z: "; .tris[i].verts(1).z
         Print "X: "; .tris[i].verts(2).x; " Y: "; .tris[i].verts(2).y; " Z: "; .tris[i].verts(2).z
     Next i
     Close #readfile
End With