Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some allocation issues (or possibly pointers)
#1
The following code is psuedo oop, but for some reason it crashes when I run it. I don't run into any problems using my allocated memory (done through a ptr and callocate) until I try passing it to a sub. Editing the variables outside a sub: ok, editing the same variables inside a sub: crashes. If anyone could shed light on this behaviour, I would be thankful.

Code:
#define pi 3.1415926/180
#define NewPt( _id, ele ) :_
    dim as PointCtrlType _id :_
    _id.p=callocate(ele*sizeof(PointType)) :_
    _id.PtSet=@PtSet :_
    _id.Move=@PtMove :_
    _id.Update=@PtUpdate :_
    _id.Draw=@PtDraw :_
    _id.Reset=@PtReset :_
    _id.Reset(@_id.p)

type PointType
    x as single
    xs as single
    y as single
    ys as single
    lu as single
end type

type PointCtrlType
    as PointType ptr p
    PtSet as sub(p as PointType ptr, x as single, y as single)
    Move as sub(p as PointType ptr, ang as single, mag as single)
    Update as sub(p as PointType ptr)
    Draw as sub(p as PointType ptr)
    Reset as sub(p as PointType ptr)
end type

sub PtSet(p as PointType ptr, x as single, y as single)
    p->x=x
    p->y=y
end sub

sub PtMove(p as PointType ptr, ang as single, mag as single)
    p->xs=cos(ang*pi)*mag
    p->ys=sin(ang*pi)*mag
end sub

sub PtUpdate(p as PointType ptr)
    p->lu=timer-p->lu
    p->x+=p->xs*p->lu
    p->y+=p->ys*p->lu
    p->lu=timer
end sub

sub PtDraw(p as PointType ptr)
    pset(p->x, p->y),15
end sub

sub PtReset(p as PointType ptr)
    p->x=0
    p->xs=0
    p->y=0
    p->ys=0
    p->lu=timer
end sub

screenres 320,240,8

NewPt(p,5)
'dim as PointCtrlType p
'p.p=callocate(sizeof(PointType) * 2)

p.p[0].x=98
p.PtSet(@p.p[0], 100,100)
sleep
end
[Image: freebasic.png]
Reply
#2
problem solved
see http://www.freebasic.net/forum/viewtopic.php?t=2213& if you're curious
[Image: freebasic.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)