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



Hello,

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:

char *getline(char *line)

{

int line_len = 0;

int max_len = 32; /* initial guess */

char *start; /* pointer to start of string is the return value */

char *count;

int c;



line = malloc(max_len * sizeof(char));

if (line == NULL)

{

printf("Out of memory.\n");

exit(1);

}

start = line;

count = line;



while ((c = getchar()) != EOF)

{

if (c == '\n')

break;

if (line_len == max_len - 1)

{

max_len *= 2;

line = realloc(line, max_len * sizeof(char));

if (line == NULL)

{

printf("Out of memory.\n");

exit(1);

}

}



*count++ = c;

line_len++;

}

*count = '\0';

return start;

}

And I'm using this to test it:

int main()

{

char *line;

int i;

for (i = 0; i < 5; i++)

{

line = getline(line);

printf("%s\n%d\n", line, strlen(line));

}

return 0;

}

On Linux it seems to work with lines of any length. On Windows (using the
MinGW compiler) it fails if the line is longer than 1024 characters (just
prints out a few random characters and reports the length as 0). I'm
hoping someone can tell me if I've got this function basically right -
have I introduced some bug which is causing the crash, or is it a Windows
thing that I don't need to concern myself with?

Also, is it better in principle to make more calls to realloc() to ensure
that you get exactly the memory you need and no more, or to make fewer
calls and just malloc() more than you think you'll need at the beginning?

Thanks for any help.
.



Relevant Pages

  • Re: Segmentation fault on 64 bit
    ... using a HP-UX C compiler on PA-RISC system. ... malloc() and friends are defined. ... int main(int argc, char **argv) ... Obviously a pointer can't be stored in an int on the system ...
    (comp.lang.c)
  • Re: DRAM data persistence
    ... int main(int argc, char **argv) ... char *ptr; ... However, in both cases (malloc and automatic variables), ...
    (sci.electronics.design)
  • Re: why still use C?
    ... int main(int arc, char* argv) { ... If there's a prototype in scope, the value returned by malloc() ... >> returned by mallocis of type int, and, in the presence of a cast, ... I can't even get it to compile with the compiler I use most ...
    (comp.lang.c)
  • Re: Array and Pointer Tutorial
    ... A char is _always_ a byte. ... int main ... Never cast malloc(). ... can result in garbage being assigned to your pointer. ...
    (comp.lang.c)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)