Re: style question,itoa
- From: Syren Baran <sbaran@xxxxxxxxxxxxxxx>
- Date: Fri, 25 Jan 2008 02:50:40 +0100
moi schrieb:
On Thu, 24 Jan 2008 21:02:12 +0100, Syren Baran wrote:vs.
Frank Cusack schrieb:I learned b) the hard way a long time ago. Thats why i document theGood to know you know that i will have an easier time reading my codean all too common error to assume
when using functions i wrote that behave as i wanted to. I didnt know
that yet.
a) no one else will ever read your code. b) you will remember how any
function behaves.
functions. That way they will not only "behave as i wanted to", but also
as i document them.
admittedly in this case it's rather trivial.Yupp, but anyway that file will contain the notice "All functions below
that return a char* require the caller to free the char*."
-frank
For this kind of formatting-functions the snprintf() kind of interface is
perfect: *) it cannot overrun the buffer (once coded correctly)
*) if it would: it returns a negative value.
*) if it succeeds, it returns the amount written,
*) it cannot overrun the buffer (it malloc's a sufficient buffer)
*) it wont fail if the memory can be alloc'd. [1]
*) it succeeds and returns the char*
such that buffer[return_value] == '\0'; which makes is easy to fill the buffer in multiple calls, and without excess copying.Oehm, why?
Utility functions, returning malloc()ed chunks need return value testing by the caller.
The snprintf() method does so also, but (on failure) it does *not* need to free() the preceding chunks, which means it is shorten and less error prone.Ok, but what do i do if i check the error condition and figure out i need a larger buffer anyway? Allocate a new buffer? In that case i can just as well have the utility function allocate the buffer for me, right?
YMMV,
AvK
[1] Checking every small malloc is the logical inverse of calling the lottery service every day asking if i have won even though i didnt get a ticket. Sure somebody may have accidently written my name on the ticket and won, but its not likely.
.
- Follow-Ups:
- Re: style question,itoa
- From: Frank Cusack
- Re: style question,itoa
- References:
- style question,itoa
- From: Syren Baran
- Re: style question,itoa
- From: David Schwartz
- Re: style question,itoa
- From: Syren Baran
- Re: style question,itoa
- From: Robert Latest
- Re: style question,itoa
- From: Syren Baran
- Re: style question,itoa
- From: Frank Cusack
- Re: style question,itoa
- From: Syren Baran
- Re: style question,itoa
- From: Frank Cusack
- Re: style question,itoa
- From: Syren Baran
- Re: style question,itoa
- From: moi
- style question,itoa
- Prev by Date: Re: style question,itoa
- Next by Date: Re: SCTP reference implementation
- Previous by thread: Re: style question,itoa
- Next by thread: Re: style question,itoa
- Index(es):
Relevant Pages
|