C++ – Should I pass array or pointer

cdesignlibraries

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.

Best Answer

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).

Related Topic