Re: malloc()/realloc() - have I got this right?



Richard Heathfield wrote:
CBFalconer said:
Dave wrote:

I'm teaching myself C by working my way through Steve Summit's
tutorial (http://www.eskimo.com/~scs/cclass/cclass.html). In one
of the questions (assignment 6, exercise 7), you have to write a
function to read lines of arbitrary length from the command line,
using malloc() and realloc() to allocate the necessary memory to
hold the lines. I came up with this:

Instead of all that just get ggets.zip from:

<http://cbfalconer.home.att.net/download/ggets.zip>

Be aware, if you do so, that the above routine has what some people
consider to be serious design flaws. And of course you'll learn
more by writing your own than you will by pinching someone else's.

Here, without the documentation, .h file, test routines, etc. is
the actual (standard C) code for ggets.c. Instead of listening to
the mewling of people who don't like to see malloc called within a
routine, read the code, and make your own decision. ggets is a
macro in ggets.h that uses the FILE stdin.

#include <stdio.h>
#include <stdlib.h>
#include "ggets.h"

#define INITSIZE 112 /* power of 2 minus 16, helps malloc */
#define DELTASIZE (INITSIZE + 16)

enum {OK = 0, NOMEM};

int fggets(char* *ln, FILE *f)
{
int cursize, ch, ix;
char *buffer, *temp;

*ln = NULL; /* default */
if (NULL == (buffer = malloc(INITSIZE))) return NOMEM;
cursize = INITSIZE;

ix = 0;
while ((EOF != (ch = getc(f))) && ('\n' != ch)) {
if (ix >= (cursize - 1)) { /* extend buffer */
cursize += DELTASIZE;
if (NULL == (temp = realloc(buffer, (size_t)cursize))) {
/* ran out of memory, return partial line */
buffer[ix] = '\0';
*ln = buffer;
return NOMEM;
}
buffer = temp;
}
buffer[ix++] = ch;
}
if ((EOF == ch) && (0 == ix)) {
free(buffer);
return EOF;
}

buffer[ix] = '\0';
if (NULL == (temp = realloc(buffer, (size_t)ix + 1))) {
*ln = buffer; /* without reducing it */
}
else *ln = temp;
return OK;
} /* fggets */
/* End of ggets.c */

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
.



Relevant Pages

  • Re: input word
    ... int cursize, ch, ix; ... char *buffer, *temp; ...
    (comp.lang.c)
  • Re: malloc()/realloc() - have I got this right?
    ... enum {OK = 0, NOMEM}; ... int cursize, ch, ix; ... char *buffer, *temp; ...
    (comp.lang.c)
  • Re: Reading a string of unknown size
    ... I think the heart code of ggets is somewhat simpler. ... int cursize, ch, ix; ... char *buffer, *temp; ...
    (comp.lang.c)
  • Re: postscript
    ... >> bytes past the allocated size of the buffer? ... >> routine, which is the interesting piece of code, does not. ... The amount of additional memory required is ... commandline argument they want to retrieve, ...
    (alt.lang.asm)
  • Re: Faster HexToBuffer Routines
    ... routine that is so amazingly fast. ... I am not here for writing your absurd Library, clown. ... comparisons in between the various Assemblers Speeds, ... did you noticed that you "Integer to Buffer" shit ...
    (alt.lang.asm)