本文介绍了为什么这个代码中的sagemention错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int test_polygon(struct Polygon* poly, struct Pointf target)    //  mathematical algorithm to know whether the point is inside the polygon.
{
    int  i, j, c=0;           // new variables will be used in the next for loop.


    for (i =0,j=poly->nvert-1 ; i < poly->nvert ; j = i++)       // for (i=0,j=Number of vertices-1 ; i < Number of vertices ; j=i++)
    {
        if ((( poly->p[i].y > target.y) != (poly->p[j].y > target.y)) &&            //poly.p[i].y is y-coordinate of the polygon's vertices.
                (target.x < (poly->p[j].x - poly->p[i].x) * (target.y - poly->p[i].y) /     //poly.p[i].x is x-coordinate of the polygon's vertices.
                 (poly->p[j].y - poly->p[i].y) + poly->p[i].x))
            c = !c;
    }
    return c;
}





我的尝试:



i将结构更改为指针,但我有相同的sagemention错误。



What I have tried:

i changed the structure to pointers , but i have the same sagemention fault.

推荐答案

/* NOTE: NDEBUG must not be defined to execute assert statements */
#include <assert.h>

int test_polygon(struct Polygon* poly, struct Pointf target)
{
    assert(poly != NULL);
    assert(poly->p != NULL);
    assert(poly->nvert > 0);
    /* EDIT: After knowing the struct Polygon uses a fixed size p array */
    assert(poly->nvert < sizeof(poly->p) / sizeof(poly->p[0]));

    /* ... */
}



这篇关于为什么这个代码中的sagemention错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 05:26