Skip to main content

Timeline for Should I pass array or pointer?

Current License: CC BY-SA 3.0

18 events
when toggle format what by license comment
Mar 28, 2014 at 19:08 comment added maple_shaft Please do not use comments for chatting unless it is about clarification or elaboration on a question or answer. If you would like to continue this conversation then I recommend visiting the chat room. Thank you.
Mar 28, 2014 at 16:58 comment added Doc Brown @zadane: I would probably prefer to use a separate function for querying the size (if you want to avoid the library to do the malloc for the client.) You can also return a specific error code is the provided buffer size is too small, pass the required size in "dataSizeReturned" and let the client make a second try with an increased buffer size.
Mar 28, 2014 at 16:15 history edited ratchet freak CC BY-SA 3.0
added 220 characters in body
Mar 28, 2014 at 16:11 comment added zadane since we are at it, I always thought a dry run of the same function can be used to return the required size but I have never seen this anywhere and don't know if I want to be the first :) For example getData(NULL, bfrSize) where the pointer is NULL, the function knows just to return the required size in bfrSize. Would that be too bad?
Mar 28, 2014 at 16:03 comment added ratchet freak @zadane documentation or a helper function, another option is to allow partial returns so the next call will return the data portion that couldn't be returned in the last call
Mar 28, 2014 at 16:01 comment added zadane @DocBrown but how does a client know how much buffer size should they allocate?
Mar 28, 2014 at 15:59 comment added Doc Brown @zadane: please, don't use fixed values like MAX_ITEMS_SIZE, take this recommendation seriously, this will help you to avoid much trouble,
Mar 28, 2014 at 15:58 comment added Doc Brown @ratchetfreak: throwing an exception is not a good idea when the API shall be kept C compatible, as the OP wrote in another comment.
Mar 28, 2014 at 15:26 comment added ratchet freak @zadane looks like a new error; besides the error code return you can also throw an exception to signal failure
Mar 28, 2014 at 15:25 comment added zadane What I am not sure about is the client is passing the buffer pointer and size, it doesn't seem to recognize how much buffer size the function actually needs which is defined by #define in header file. I guess first thing my function has to to do is check if the buffer size is at least as big as MAX_ITEMS_SIZE
Mar 28, 2014 at 15:21 comment added Doc Brown @zadane: its pretty obvious how to change this answer to what you want: int getDate(float* buffer, size_t bufferSize, size_t &dataSizeReturned); where the return code is your error code.
Mar 28, 2014 at 15:21 comment added ratchet freak @zadane make all error code negative and return the size, test for error is int read = getData(buff, buffsize);if(read<0){/*ERROR*/}
Mar 28, 2014 at 15:18 comment added zadane 0 return means no error occurred but any other int indicates a particular error.
Mar 28, 2014 at 15:17 comment added ratchet freak @kevincline you'd need to return an int then
Mar 28, 2014 at 15:07 comment added kevin cline return a negative number for error
Mar 28, 2014 at 15:06 comment added ratchet freak @zadane you can still return 0 to say nothing was returned, though no complex error types
Mar 28, 2014 at 15:06 comment added zadane my return type is int where zero means success and non-zero indicates error. Sorry I missed that in my post, will update.
Mar 28, 2014 at 15:03 history answered ratchet freak CC BY-SA 3.0