0

I'm coding a C wrapper and need to get data from a vector of struct.

struct and functio to wrap

struct foo {
    string name;
    time_t datetime; 
    string uid;
    int number_id;
    string store;
    string city;

    invoice():number_uid{INT_MAX}{}
};

struct bar {
    int foo;
    string name;
}

int cpp_function(vector<struct foo> &some_foo);

I redefined foo struct in C, but maybe is not necesary if I should use an opca type as handle. Header wrapper.h

#ifdef __cplusplus
extern "C" {
#endif

struct foo {
    char *name;
    time_t datetime; 
    char *uid;
    int number_id;
    char *store;
    char *city;
};
typedef struct foo_wrapper foo_wrapper;

struct bar_wrapper {
    int foo;
    char *name;
}
typedef struct bar_wrapper bar_wrapper;

bar_wrapper *c_function_create(foo_wrapper **foo_wrapper, int *number_foo);
#ifdef __cplusplus
}

#endif

wrapper.cpp

extern "C" struct bar_wrapper {
    struct bar instance;
};
bar_wrapper *c_function_create(foo_wrapper **foo_wrapper, int *number_foo)
{
    bar_wrapper *bar_wrapper = new bar_wrapper;
    struct bar& bar = bar_wrapper->instance; //<-- note the reference

    vector<struct foo> some_foo;
    int ret = cpp_function(&some_foo);

    *foo_wrapper = (foo_wrapper*)malloc(sizeof(**foo_wrapper));
    *number_foo = some_foo.size();
    // How to write a some_foo.size() structs into the foo_wrapper pointer to structs?
    // Below code is not correct
    for (i=0; i<some_foo.size(); i++)
    {
        (*foo_wrapper + (i * sizeof(foo_wrapper)))->name = some_foo[i].name;
        (*foo_wrapper + (i * sizeof(foo_wrapper)))->datetime = some_foo[i].datetime;
        (*foo_wrapper + (i * sizeof(foo_wrapper)))->uid = some_foo[i].uid;
        (*foo_wrapper + (i * sizeof(foo_wrapper)))->number_id = some_foo[i].number_id;
        (*foo_wrapper + (i * sizeof(foo_wrapper)))->store = some_foo[i].store;
        (*foo_wrapper + (i * sizeof(foo_wrapper)))->city = some_foo[i].city;
    }

    return bar_wrapper;
}

main.c

foo_wrapper *foo_wrapper;
int number_foo = 0;
int ret = c_function_create(&foo_wrapper, &number_foo);

With this code I'm getting runtime pointer errors, in main.c when I read a foo_wrapper pointer. munmap_chunk(): invalid pointer

crossmax
  • 346
  • 3
  • 20
  • your data is stored in `some_foo` which goes out of scope at the end of the function so all the pointers in `foo_wrapper` are dangling. – kmdreko Jun 10 '19 at 22:55
  • @kmdreko, yes, but I need another way to pass vector from wrapper.cpp to main.c. Is opaque handle a good colution for that? – crossmax Jun 10 '19 at 23:00
  • yes, an opaque type would be more appropriate i'd think – kmdreko Jun 10 '19 at 23:06
  • `string` and `char*` are fundamentally different things. – L. F. Jun 11 '19 at 03:37
  • @L.F. of course, but I need wrapping it in some way. If there is another way to return the vector to a called from C I happy to hear that. I'm not able to convert this to an opaque handler – crossmax Jun 11 '19 at 07:51
  • @kmdreko I'm thinking about use opaque type handle but, if I don't understood wrong, with opaque handle I will not be able to read the struct data from C code, isn't it? – crossmax Jun 11 '19 at 08:40
  • You'll need a function like `char* get_name(opaque_type*)` to get the name from it – kmdreko Jun 11 '19 at 10:15

0 Answers0