c++ - VS2015 x64 三角库

标签 c++ visual-studio-2015 out-of-memory

三角形库可以在 VS2015 x64 上运行吗?

我在 Linux 上工作,并致力于将 VS2015 x64 转换为 Windows,但由于 poolalloc 函数中的内存问题,我遇到了错误。

但我不明白问题出在哪里。

void OptimizeMatches(MatrixXf& feat, MatrixXf& feat_ref, MatrixXi& match_idx, MatrixXf& match_dist, std::vector<Matrix3f>& trans_mat, std::vector<Matrix3f>& inv_mat, std::vector<Matrix3f>& trans_mat_ref, std::vector<Matrix3f>& inv_mat_ref,
    MatrixXf* belief_ptr, MatrixXi* label_ptr, std::vector<match_list>* match_ptr, std::vector<triangle_segment>* triangle_ptr ) {

    int num_nodes = (int)feat.cols();
    int num_matches = (int)match_idx.rows();

    // build graph using delaunay triangulation
    std::vector<support_pt> p_support;
    p_support.resize(num_nodes);
    for(int i = 0; i < num_nodes; i++){
        p_support[i].x = feat(0,i); 
        p_support[i].y = feat(1,i);
    }
    std::vector<triangle_segment>& T = *triangle_ptr;
    computeDelaunayTriangulation(p_support, &T);

    ............

}

在 trangle.cpp 用户函数中

void computeDelaunayTriangulation (vector<support_pt> p_support, vector<triangle_segment>* tri_ptr){
// input/output structure for triangulation    
    struct triangulateio in, out;
    int k;

    // inputs
    in.numberofpoints = p_support.size();
    in.pointlist = (float*)malloc(in.numberofpoints*2*sizeof(float));
    k=0;
    for (int i=0; i< p_support.size(); i++){
        in.pointlist[k++] = p_support[i].x;
        in.pointlist[k++] = p_support[i].y;
    }

    in.numberofpointattributes = 0;
    in.pointattributelist      = NULL;
    in.pointmarkerlist         = NULL;
    in.numberofsegments        = 0;
    in.numberofholes           = 0;
    in.numberofregions         = 0;
    in.regionlist              = NULL;

    // outputs
    out.pointlist              = NULL;
    out.pointattributelist     = NULL;
    out.pointmarkerlist        = NULL;
    out.trianglelist           = NULL;
    out.triangleattributelist  = NULL;
    out.neighborlist           = NULL;
    out.segmentlist            = NULL;
    out.segmentmarkerlist      = NULL;
    out.edgelist               = NULL;
    out.edgemarkerlist         = NULL;


    // do triangulation (z=zero-based, n=neighbors, Q=quiet, B=no boundary markers)
    char parameters[] = "zQB";
    //printf("triangulate\n");
    triangulate(parameters, &in, &out, NULL);

    .....
}

triangle.cpp 中的三角形主函数

void triangulate(char *triswitches, struct triangulateio *in,
             struct triangulateio *out, struct triangulateio *vorout)
{
  struct mesh m;
    struct behavior b;
    float *holearray;               /* Array of holes. */
    float *regionarray;   /* Array of regional attributes and area constraints. */

    triangleinit(&m);
    parsecommandline(1, &triswitches, &b);
    m.steinerleft = b.steiner;

    transfernodes(&m, &b, in->pointlist, in->pointattributelist,
                in->pointmarkerlist, in->numberofpoints,
                in->numberofpointattributes);
    .....
}

在这个函数的最后,poolalloc 函数为顶点循环分配了一个内存地址。此内存地址无效,发生访问冲突。

void transfernodes(struct mesh *m, struct behavior *b, float *pointlist,
               float *pointattriblist, int *pointmarkerlist,
               int numberofpoints, int numberofpointattribs)
{
    vertex vertexloop;
    float x, y;
    int i, j;
    int coordindex;
    int attribindex;

    m->invertices = numberofpoints;
    m->mesh_dim = 2;
    m->nextras = numberofpointattribs;
    m->readnodefile = 0;
    if (m->invertices < 3) {
      printf("Error:  Input must have at least three input vertices.\n");
      triexit(1);
    }
    if (m->nextras == 0) {
      b->weighted = 0;
    }

    initializevertexpool(m, b);

    /* Read the vertices. */
    coordindex = 0;
    attribindex = 0;
    for (i = 0; i < m->invertices; i++) {
      vertexloop = (vertex) poolalloc(&m->vertices);
      //vertexloop = (vertex) &m->vertices;
      /* Read the vertex coordinates. */
      x = vertexloop[0] = pointlist[coordindex++];
      y = vertexloop[1] = pointlist[coordindex++];


  ....
}

这是 poolalloc 函数

int *poolalloc(struct memorypool *pool)
{
    int *newitem;
    int **newblock;
    unsigned long alignptr;

    /* First check the linked list of dead items.  If the list is not   */
    /*   empty, allocate an item from the list rather than a fresh one. */
    if (pool->deaditemstack != (int *)NULL) {
        newitem = pool->deaditemstack;               /* Take first item in list. */
        pool->deaditemstack = *(int **)pool->deaditemstack;
    }
    else {
        /* Check if there are any free items left in the current block. */
        if (pool->unallocateditems == 0) {
            /* Check if another block must be allocated. */
            if (*(pool->nowblock) == (int *)NULL) {
                /* Allocate a new block of items, pointed to by the previous block. */
                newblock = (int **)trimalloc(pool->itemsperblock * pool->itembytes +
                    (int) sizeof(int *) +
                    pool->alignbytes);
                *(pool->nowblock) = (int *)newblock;
                /* The next block pointer is NULL. */
                *newblock = (int *)NULL;
            }

            /* Move to the new block. */
            pool->nowblock = (int **) *(pool->nowblock);
            /* Find the first item in the block.    */
            /*   Increment by the size of (int *). */
            alignptr = (unsigned long) (pool->nowblock + 1);
            /* Align the item on an `alignbytes'-byte boundary. */
            pool->nextitem = (int *)
                (alignptr + (unsigned long) pool->alignbytes -
                (alignptr % (unsigned long) pool->alignbytes));
            /* There are lots of unallocated items left in this block. */
            pool->unallocateditems = pool->itemsperblock;
        }

        /* Allocate a new item. */
        newitem = pool->nextitem;
        /* Advance `nextitem' pointer to next free item in block. */
        pool->nextitem = (int *)((char *)pool->nextitem + pool->itembytes);
        pool->unallocateditems--;
        pool->maxitems++;
    }
    pool->items++;
    return newitem;
}

最佳答案

好的,我已经解决了同样的问题。

triangle.c 中的代码假定 sizeof(long) 等于 8,并使用 unsigned long 作为指针类型。但是 sizeof(long) 在 VS 中等于 4,所以 unsigned long 不能是 x64 中指针的类型。

我只是将 triangle.c 中的所有“long”替换为“__int64”,代码就可以工作了。

关于c++ - VS2015 x64 三角库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44558793/

相关文章:

c++ - gen~ phasor() 是做什么的? (将 Max/MSP gen 转换为 C++)

c# - 是否有内置方法来序列化数组配置值?

java - 清理登录数据

c++ - 如何选择合适的倍数?

c++ - struct within-struct 初始化成员的统一初始化

clr - asp.net mvc 6如何知道 Controller 不继承自Controller类时要添加哪些资源?

angular - ng 命令抛出内存不足错误 [致命进程 OOM 内存不足,无法创建隔离]

java - HashMap 和 ArrayList 的 Android OutOfMemoryError

C++ 菱形接口(interface)继承

visual-studio-2015 - Visual Studio 2015 社区诊断工具 - 没有内存使用工具?