Re: Design question dealing with char* function
- From: "David Resnick" <lndresnick@xxxxxxxxx>
- Date: 28 Jul 2005 09:04:05 -0700
fernandez....@xxxxxxxxx wrote:
> My question is about if I should return char* or have a char* as an
> argument. My basic premise for this function is to return a char*
> buffer and the size of the buffer to the caller. I know that each of
> the following works but Stylistic which would be the better approach.
>
> Here are my two examples:
>
> char* GetBuffer(long* size);
>
> or
>
>
> void GetBuffer(char* buff, long* size);
>
They are not the same, the second doesn't let you return
the buffer. Perhaps you meant "char ** buff"?
Once you are returning multiple things, my inclination would
be more like this:
int GetBuffer(char **buf_out, size_t *buf_size_out)
Where the return int is for status reporting,
such as couldn't allocate a buffer/etc. I tend to name
parameters used to return values with _out, just my style,
as it is sometimes difficult to tell at a glance
in the header file which parameters are being used for
input to the function and which for output.
A reasonable alternative, leaning more towards use of
"objects", is to return a structure containing both the
buffer and its size. Some overhead for the struct, but
then you have the buffer and its size in a nice package
you can pass around...
But this is all style, you'll get lots of opinions...
-David
.
- References:
- Design question dealing with char* function
- From: fernandez . dan
- Design question dealing with char* function
- Prev by Date: Design question dealing with char* function
- Next by Date: Re: Hints on how to migrate from C++ to C
- Previous by thread: Design question dealing with char* function
- Next by thread: Re: Design question dealing with char* function
- Index(es):
Relevant Pages
|