I'm implementing a C API that exposes some data collected from a systemd service. The header of the API contains two structs:
typedef struct Info {
int latest;
int prev;
int cur;
} Info;
typedef struct InfoArray {
Info *info;
} InfoArray;
The implementation of this C header is written in C++ and I plan to offer a create(size_t size) function
to let the user create a new struct with an array of the user entered size. This returned array can then later be passed by reference to another function to fill it with data until the array has reached the size defined by the user.
How would I achieve this?
Edit: I'm looking for a way to return the allocated array of size n, however I tried the following:
InfoArray *create(size_t size) {
InfoArray *array = (InfoArray *)malloc(size * sizeof(Info));
return array;
}
which should return a allocated array, but when trying to delete the array on the client side by calling delete info
I get an error that I cannot free this handle, so how would i let the consumer of the API manage the deletion of the memory?
Since the implementation is written in a .cpp file (C++) i figured there must be a way to return it by using the new operator, but I've failed on implementing it like this.