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


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.

  • If you want to remain C compatible you should update your question to reflect that fact.
    – Ramhound
    Commented Mar 28, 2014 at 17:32
  • @Ramhound done, just updated the question.
    – zadane
    Commented Mar 28, 2014 at 18:07
  • 1
    References are C++. If you need extern "C" compatibility, make sure it's all clean C.
    – david.pfx
    Commented Mar 29, 2014 at 14:04

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

  • or std::vector<float> getData() and throw an exception on error. Commented Mar 28, 2014 at 15:09
  • I thought passing STL containers is not safe across different compilers when it comes to exposing them from a library?
    – zadane
    Commented Mar 28, 2014 at 15:10
  • @zadane: are you really going to distribute your lib across different compilers in binary form?
    – Doc Brown
    Commented Mar 28, 2014 at 15:12
  • I will only provide .lib and header file and I would expect it would work with at least windows compilers who knows from VC6 to VS2013 and gcc. I don't want to worry about that. Also even though I am using C++, I would like interface to be in C functions so they can be used more widely.
    – zadane
    Commented Mar 28, 2014 at 15:14
  • @zadane: you did not mention anything of that in your question. But if that's the case, I suggest you use ratchedfreak's answer.
    – Doc Brown
    Commented Mar 28, 2014 at 15:17

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);
  • my return type is int where zero means success and non-zero indicates error. Sorry I missed that in my post, will update.
    – zadane
    Commented Mar 28, 2014 at 15:06
  • @zadane you can still return 0 to say nothing was returned, though no complex error types Commented Mar 28, 2014 at 15:06
  • return a negative number for error Commented Mar 28, 2014 at 15:07
  • @kevincline you'd need to return an int then Commented Mar 28, 2014 at 15:17
  • 0 return means no error occurred but any other int indicates a particular error.
    – zadane
    Commented Mar 28, 2014 at 15:18

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" {

int getData(float *data, int* size)

#ifdef __cplusplus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.