I am writing a C++ library (but prefer C style interface functions) and one of the function returns array of floats. I am not sure if I should pass pointer or fixed array to get this data?
// in the library header file
#define MAX_ITEMS_SIZE 1000
int getData(float * data, int &size );
or
int getData(float (&data)[MAX_ITEMS_SIZE], int & size )
If I am passing the pointer, the client should allocate MAX_ITEMS_SIZE for it. Is that a reasonable and fair expectations? Passing it as array forces the client to pass the max memory required. Which one is better?
Some more background, the MAX_ITEMS_SIZE is the maximum number of floats that can be returned. In reality the array will contain less items which is filled in the size parameter. The function returns int which is error code where zero means no error.
My overall goal is how to return the data which is array of floats in easy to understand and reliable way. Even though I am using C++, I would like to stick with C interface functions so the library can be used more widely.
3
I am writing a C++ library
So why don’t you use C++ instead of C?
getData(std::vector<float> &data);
is most probably what you want. And even when programming in C, do yourself a favor and avoid to use fixed limits like MAX_ITEMS_SIZE
, that number will almost be too big (wasting resources) or too small (which means your program might not work as intended).
7
Typically you would see
size_t getDate(float* buffer, size_t bufferSize);
The return value is the amount of data returned and you pass in how large your buffer actually is.
EDIT: In your comments to want to have an error return code and remain C compatible, if so I suggest doing like Doc brown suggested:
int getDate(float* buffer, size_t bufferSize, size_t &dataSizeReturned);
16
My few cents… Since you want to maintain compatibility with C, I would say – neither. C does not have a “reference” concept and it also does not allow for passing array by reference. Furthermore, C++ has name mangling so unless the function is declared with C-linkage, it would not be possible to call it from a C code in a straightforward way.
Therefore, I’d say that for the purpose of compatibility with C, the signature should look like this:
#ifdef __cplusplus
extern "C" {
#endif
int getData(float *data, int* size)
#ifdef __cplusplus
}
#endif