Re: Expanding buffer - response to "Determine the size of malloc" query




"James Harris" <james.harris.1@xxxxxxxxxxxxxx> wrote in message
Initial issue: read in an arbitrary-length piece of text.
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.

Firstly, don't worry about the actual code bodies at this stage. Any reasonably competent C programmer should be able to provide those.

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


.



Relevant Pages

  • Re: How to develop a random number generation device
    ... a buffer (e.g. by making some portion of its input larger than the buffer ... will use a segment for the larger array and leave the character arrays as ... The realistic approach to eliminating buffer overruns is not to write word ...
    (sci.electronics.design)
  • Re: [] arrays, again
    ... or (baseaddr index)? ... The convention introduced by Julian Noble for 1 and 2-D arrays in ... Or copy the data into a hardware buffer for transmission... ... the -1-st cell contains something so useful. ...
    (comp.lang.forth)
  • Re: speed it up
    ... Gernot Frisch wrote: ... I'm transfering my back buffer to the display memory here. ... You may also get more performance by declaring the arrays the same ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.cpp)
  • Re: [] arrays, again
    ... Scientific Library (FSL). ... The problem is that you do not always have arrays. ... Or copy the data into a hardware buffer for transmission... ... I do this all the time by defining "sizeof" constants ...
    (comp.lang.forth)
  • Re: Understanding ByteBuffer
    ... One clue is the limit method of Buffer. ... and arrays and collections all with the exact same syntax to process ... I have written a little essay on what I have learned about ByteBuffer. ... It turns out it is not nearly as clever as thought it was. ...
    (comp.lang.java.programmer)