Re: Expanding buffer - response to "Determine the size of malloc" query
- From: "Malcolm McLean" <regniztar@xxxxxxxxxxxxxx>
- Date: Sat, 31 May 2008 10:05:24 +0100
"James Harris" <james.harris.1@xxxxxxxxxxxxxx> wrote in message
Initial issue: read in an arbitrary-length piece of text.Firstly, don't worry about the actual code bodies at this stage. Any reasonably competent C programmer should be able to provide those.
Perceived issue: handle variable-length data
The code below is a suggestion for implementing a variable length
buffer that could be used to read text or handle arrays of arbitrary
length. I don't have the expertise in C of many folks here so I feel
like I'm offering a small furry animal for sacrifice to a big armour
plated one... but will offer it anyway. Please do suggest improvements
or challenge the premise. It would be great if it could be improved to
become a generally useful piece of code.
The thing is the interfaces.
The first problem is that if we use char *, the functions will only work on character arrays. If we use void *s, this problem disappears, but there might be issues about too many casts to access the actual data.
The second issue is whether to use a structure for the buffer, or, as you have done, pass in several parameters to represent size and capacity. There's a nasty C stitch-up if we use void *s with option 2.
ebuf_full(void **buf ...)
char *buffer;
/* this is illegal */
ebuf_full(&buffer)
buffer has to be assigned to a dummy void *first. Which makes the function unusable.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
.
- References:
- Expanding buffer - response to "Determine the size of malloc" query
- From: James Harris
- Expanding buffer - response to "Determine the size of malloc" query
- Prev by Date: Re: malloc()/realloc() - have I got this right?
- Next by Date: Re: malloc()/realloc() - have I got this right?
- Previous by thread: Re: Expanding buffer - response to "Determine the size of malloc" query
- Next by thread: passing copy of a pointer to a variable vs passing the copy
- Index(es):
Relevant Pages
|