2017-06-15 121 views
1

三角形庫是否適用VS2015 x64?三角形庫VS2015 x64

我正在使用Linux並正在轉換爲VS2015 x64的windows,但由於poolalloc func中的內存問題,我得到錯誤。

但我不明白問題是什麼。

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用戶FUNC

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); 
    ..... 
} 

主FUNC在這個函數結束時,函數poolalloc分配的存儲器地址的vertexloop。此內存地址無效,併發生訪問衝突。

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 FUNC

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; 
} 

回答

1

OK,我已經解決了這個同樣的問題。

triangle.c中的代碼假定sizeof(long)等於8,並使用unsigned long作爲指針的類型。但是sizeof(long)在VS中等於4,所以unsigned long不能是x64中指針的類型。

我只是用triangle.c中的「__int64」替換所有「long」,並且代碼有效。