使用来自C的全局C ++对象崩溃应用程序

时间:2011-03-06 14:07:05

标签: c++ c data-structures

这是我的第一篇文章,我是这个网站的新手,但我现在已经潜伏了一段时间。我对C语言有很好的了解,对C ++的知识非常有限。我猜。我在Windows(XPx64),VS2008。

我正在尝试包装一个C ++库kdtree2,以便我可以在C中使用它。主要问题与访问kdtree2和kdtree2_result_vector类有关。由于作者ftp服务器没有回应我已经上传了原始发行版kdtree2 src

的副本

关于kd-tree(二叉树的一种形式)的一些快速信息,“'数据'是n维笛卡尔空间中的坐标和索引。它用于最近邻搜索,所以之后构造树(不会被修改),可以查询树的各种类型的nn搜索。在这种情况下的结果是在结构的矢量对象(类似c的结构)中返回的。

struct kdtree2_result {
  // 
  // the search routines return a (wrapped) vector
  // of these. 
  //
public:
  float dis;  // its square Euclidean distance
  int idx;    // which neighbor was found
}; 

我想象的解决方案是拥有一个kdtree2对象数组(每个线程一个)。对于kdtree2_result_vector类,我还没有得到解决方案,因为我没有超越一垒。 无需直接访问kdtree2类

我只需要填充数据然后使用它(因为下面的第二个函数是一个例子)。为此我已定义:

kdtree2 *global_kdtree2;

extern "C" void new_kdtree2 ( float **data, const int n, const int dim, bool arrange ) {

    multi_array_ref<float,2> kdtree2_data ( ( float * ) &data [ 0 ][ 0 ], extents [ n ][ dim ], c_storage_order ( ) );

    global_kdtree2 = new kdtree2 ( kdtree2_data, arrange );
}

然后使用那棵树,我定义了:

extern "C" void n_nearest_around_point_kdtree2 ( int idxin, int correltime, int nn ) { 

    kdtree2_result_vector result;

    global_kdtree2->n_nearest_around_point ( idxin, correltime, nn, result );
}

kdtree2_result_vector派生自矢量类。这个编译没有错误,结果库可以链接,它的C函数可以从C。

访问

问题是调用n_nearest_around_point_kdtree2会使程序崩溃。我怀疑在设置树和在第二个函数调用中使用它之间,树以某种方式被释放/销毁。调用c-test程序发布在下面:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "kdtree2.h"

#define MALLOC_2D(type,x,y) ((type**)malloc_2D_kdtree2((x),(y),sizeof(type)))

void **malloc_2D_kdtree2 ( const int x, const int y, const int type_size ) {

    const int y_type_size = y * type_size;

    void** x_idx = ( void ** ) malloc ( x * ( sizeof ( void ** ) + y_type_size ) );

    if ( x_idx == NULL )
        return NULL;

    char* y_idx = ( char * ) ( x_idx + x );

    for ( int i = 0; i < x; i++ )
        x_idx [ i ] = y_idx + i * y_type_size;

    return x_idx;
}

int main ( void ) {

    float **data = MALLOC_2D ( float, 100, 3 );

    for ( int i = 0; i < 100; i++ )
        for ( int j = 0; j < 3; j++ ) 
            data [ i ][ j ] = ( float ) ( 3 * i + j );

    // this works fine
    tnrp ( data, 100, 3, false );

    new_kdtree2 ( data, 100, 3, false );
    // this crashes the program
    n_nearest_around_point_kdtree2 ( 9, 3, 6 );

    delete_kdtree2 ( );

    free ( data );

    return 0;
}

据我所知,在互联网上搜索它应该有效,但我显然错过了勇敢(对我来说)C ++新世界的重要内容。

编辑:

决议,感谢larsmans。我已经定义了以下类(源自之前发布的larsmans):

class kdtree {

private:   

    float **data;
    multi_array_ref<float,2> data_ref;
    kdtree2 tree;

public:

    kdtree2_result_vector result;

    kdtree ( float **data, int n, int dim, bool arrange ) :

        data_ref ( ( float * ) &data [ 0 ][ 0 ], extents [ n ][ dim ], c_storage_order ( ) ),
        tree ( data_ref, arrange )
        {
        }

    void n_nearest_brute_force ( std::vector<float>& qv ) {
        tree.n_nearest_brute_force ( qv, result ); }

    void n_nearest ( std::vector<float>& qv, int nn ) {
        tree.n_nearest ( qv, nn, result ); }

    void n_nearest_around_point ( int idxin, int correltime, int nn ) {
        tree.n_nearest_around_point ( idxin, correltime, nn, result ); }

    void r_nearest ( std::vector<float>& qv, float r2 ) {
        tree.r_nearest ( qv, r2, result ); }

    void r_nearest_around_point ( int idxin, int correltime, float r2 ) {
        tree.r_nearest_around_point ( idxin, correltime, r2, result ); }

    int r_count ( std::vector<float>& qv, float r2 ) {
        return tree.r_count ( qv, r2 ); }

    int r_count_around_point ( int idxin, int correltime, float r2 ) {
        return tree.r_count_around_point ( idxin, correltime, r2 ); }
};

从C:

调用这些函数的代码
kdtree* global_kdtree2 [ 8 ];


extern "C" void new_kdtree2 ( const int thread_id, float **data, const int n, const int dim, bool arrange ) {

    global_kdtree2 [ thread_id ] = new kdtree ( data, n, dim, arrange );
}


extern "C" void delete_kdtree2 ( const int thread_id ) {

    delete global_kdtree2 [ thread_id ];
}


extern "C" void n_nearest_around_point_kdtree2 ( const int thread_id, int idxin, int correltime, int nn, struct kdtree2_result **result ) { 

    global_kdtree2 [ thread_id ]->n_nearest_around_point ( idxin, correltime, nn );

    *result = &( global_kdtree2 [ thread_id ]->result.front ( ) );
}

最终C程序开始全部使用:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "kdtree2.h"


int main ( void ) {

    float **data = MALLOC_2D ( float, 100, 3 );

    for ( int i = 0; i < 100; i++ )
        for ( int j = 0; j < 3; j++ ) 
            data [ i ][ j ] = ( float ) ( 3 * i + j );

    int thread_id = 0;

    new_kdtree2 ( thread_id, data, 100, 3, false );

    struct kdtree2_result *result;

    n_nearest_around_point_kdtree2 ( thread_id, 28, 3, 9, &result );

    for ( int i = 0; i < 9; i++ )
        printf ( "result[%d]= (%d,%f)\n", i , result [ i ].idx, result [ i ].dis );

    printf ( "\n" );

    n_nearest_around_point_kdtree2 ( thread_id, 9, 3, 6, &result );

    for ( int i = 0; i < 6; i++ )
        printf ( "result[%d]= (%d,%f)\n", i , result [ i ].idx, result [ i ].dis );

    delete_kdtree2 ( thread_id );

    free ( data );

    return 0;
}

1 个答案:

答案 0 :(得分:3)

参考文件中的API文档相当薄弱,作者的FTP服务器没有响应,所以我无法确定,但我的预感是

multi_array_ref<float,2> kdtree2_data((float *)&data[0][0], extents[n][dim],
                                      c_storage_order( ));

global_kdtree2 = new kdtree2(kdtree2_data, arrange);

通过在kdtree2对象中存储对kdtree2_data的引用来构造global_kdtree2,而不是制作完整副本。由于kdtree2_data是一个局部变量,因此在new_kdtree2返回时会被销毁。在n_nearest_around_point_kdtree2完成之前,你必须保持活着。