Using a global C ++ object from application C crashes

This is my first post, I am new to this site, but I have been hiding for some time. I know C well and very limited knowledge of C ++. I suppose. I am on Windows (XPx64), VS2008.

I am trying to wrap a C ++ library, kdtree2 so that I can use it from C. The main issues are regarding access to kdtree2 and kdtree2_result_vector. Since the authors of the ftp server are not responding, I downloaded a copy of the original kdtree2 src distribution

Just some brief information about a kd tree (binary tree form), "data" are coordinates in n-dimensional Cartesian space and index. What it is used for is searching for the nearest neighbor, so after building a tree (which will not be changed), you can query the tree for various types of nn searches. The results in this case are returned in the vector object structs (c-like structs).

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

My imaginary solution is to have an array of kdtree2 objects (one per stream). For the kdtree2_result_vector class, I do not have a solution yet, since I do not get the first base. No need to directly access the kdtree2 class .

I only need to fill it with data and then use it (as an example of the second function below). For this, I defined:

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

To do this, using this tree, I defined:

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

, , , , - ( ) ++.

EDIT:

, 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;
}
+3
1

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.

+3

Source: https://habr.com/ru/post/1796640/


All Articles