Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"segmentation violation" signal?
#1
When I try to compile my program, fbc gives the following output:

Quote:Aborting due to runtime error 11 ("segmentation violation" signal)

What would cause this? Here's the code:

Code:
type vector
    x as single
    y as single
end type

type particle
    x as single     'position
    y as single
    v as vector     'velocity
    r as single     'radius
    p as single     '(phase) angle of rotation
    w as single     'angular velocity
    q as integer    'charge
    s as integer    'spin
    m as single     'mass
end type

declare function dot_product(a as vector, b as vector) as single
declare function make_vector(p as single) as vector
declare function normalize_vector(v as vector) as vector
declare sub handle_particles
declare sub draw_particles

const pi = 3.14159
const force_coefficient = 1
const particle_color = 15
const max_particles = 9
dim shared num_particles, par(max_particles) as particle
num_particles=1
for i = 0 to num_particles
    par(i).x=cx-j\2+j*i
    par(i).y=cy
    par(i).q=-1
    par(i).w=0
    par(i).s=1
    par(i).m=1
    par(i).r=10
    par(i).v.x=0
    par(i).v.y=0
next i






sub draw_particles
    for i=0 to num_particles
        x1=par(i).x-par(i).r*cos(par(i).p)
        y1=par(i).y+par(i).r*sin(par(i).p)
        x2=par(i).x+par(i).r*cos(par(i).p)
        y2=par(i).y-par(i).r*sin(par(i).p)
        line (x1,y1)-(x2,y2),particle_color
    next i
end sub

sub handle_particles
    dim as vector f, dv, a
    dim as single t, fm, d
    for i=0 to num_particles
        a.x=0
        a.y=0
        aa=0
        for j=0 to num_particles
            if i<>j then
                d=sqr((par(j).x-par(i).x)^2+(par(j).y-par(i).y)^2)
                fm=force_coefficient*par(i).s*par(j).s*par(i).q*par(j).q* _
                  dot_product(make_vector(par(i).p),make_vector(par(j).p))/(d*d)
                dv.x=par(j).x-par(i).x
                dv.y=par(j).y-par(i).y
                dv = normalize_vector(dv)                                'vector points at other particle
                a.x+=fm*dv.x/par(i).m                                                   'linear acceleration
                a.y+=fm*dv.y/par(i).m
                t=dot_product(make_vector(par(i).p),dv)*fm*par(i).r                'torque
                aa+=t/par(i).m                                                          'angular acceleration
            end if
        next j
        par(i).v.x+=a.x
        par(i).v.y+=a.y
        par(i).x+=par(i).v.x
        par(i).y+=par(i).v.y
        par(i).w+=aa
        par(i).p+=par(i).w
    next i
end sub

function dot_product(a as vector, b as vector) as single
    dot_product = a.x*b.x+a.y*b.y
end function

function make_vector(p as single) as vector
    make_vector.x=cos(p)
    make_vector.y=sin(p)
end function

function normalize_vector(v as vector) as vector
    m=sqr(v.x^2+v.y^2)
    normalize_vector.x=v.x/m
    normalize_vector.y=v.y/m
end function
Reply
#2
In this line:

par(i).x=cx-j\2+j*i

us there a specific reason for the integer division you're doing considering that your par(i).x is declared as a single?
hen they say it can't be done, THAT's when they call me ;-).

[Image: kaffee.gif]
[Image: mystikshadows.png]

need hosting: http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
Reply
#3
Quote:In this line:

par(i).x=cx-j\2+j*i

us there a specific reason for the integer division you're doing considering that your par(i).x is declared as a single?

I was just trying to speed things up. A habit from the old days, I guess...
I tried commenting the line, but it still gives the same error.
Reply
#4
I think I know :-)

Code:
function make_vector(p as single) as vector
    make_vector.x=cos(p)
    make_vector.y=sin(p)
end function

function normalize_vector(v as vector) as vector
    m=sqr(v.x^2+v.y^2)
    normalize_vector.x=v.x/m
    normalize_vector.y=v.y/m
end function

You should try using a work vector instead of assigning each element like this.

Code:
function make_vector(p as single) as vector

    dim work_vector as vector

    work_vector.x=cos(p)
    work_vector.y=sin(p)
    make_vector = work_vector

end function

function normalize_vector(v as vector) as vector
    
    dim work_vector as vector

    m=sqr(v.x^2+v.y^2)
    work_vector.x=v.x/m
    work_vector.y=v.y/m
    normalize_vector = work_vector
  
end function

Your functions will exit at the first assignment you make to make_vector and normalize_vector.
hen they say it can't be done, THAT's when they call me ;-).

[Image: kaffee.gif]
[Image: mystikshadows.png]

need hosting: http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
Reply
#5
I just tried that & it still gives the same error. Sad
Reply
#6
The latest CVS version gives the following error:

Code:
test_sigsegv.bas(68) : error 90: Cannot pass an UDT result by reference, at parameter 1 of DOT_PRODUCT()

                  dot_product(make_vector(par(i).p),make_vector(par(j).p))/(d*d)

                                                                         ^

Storing the result of make_vector into a temporary variable first works and compiles without a SIGSEGV.

Regards,
Mark
Reply
#7
Thanks, but that really sucks. It works, though. I think the FB developers should fix this problem right away. :-?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)