Re: How to take in a string of any size?

From: William L. Bahn (william_at_toomuchspam.net)
Date: 08/23/04


Date: Sun, 22 Aug 2004 16:00:30 -0600


"SM Ryan" <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org>
wrote in message news:10ihe9v94togo26@corp.supernews.com...
> zalzon <zalzonishappy@zalll.com> wrote:
> # On Sun, 22 Aug 2004 02:07:17 -0600, William L. Bahn wrote:
> #
> # > You could write it to a file, counting the characters as
you go,
> # > and then allocate the memory and read it back in.
> #
> # Except you have to read the string in first before you write
it to a file
> # stream... which leads to the original question of how do you
read a string
> # of unspecified size in and store it.....
>
> char *s = 0; int m = 0,n = 0,c; FILE *f = ...;
> *s = 0;
> while ((c=fgetc(f))!=EOF) {
> if (n+2>=m) {m = 2*(n+2); s = realloc(s,m);}
> s[n++] = c; s[n] = 0;
> if (c=='\n') {s = realloc(s,n+1); break;}
> }
> if (s) {
> ....
> }else {
> end of file....
> }
>

So, when you execute

*s = 0;

where does that 0 get stored?

IIRC, the OP didn't specify that the input string was coming from
a file.

If it IS in a file, then the solution is trivial - scan the file
and count the number of characters you are going to read in then,
allocate the correct amount of memory, rewind the file, and read
the contents into the allocated memory. This will probably be
quicker than reallocating memory so often (though, admittedly, by
doubling the amount of allocated memory each time your total
number of calls to realloc() isn't going to be excessive). But if
you only get one shot at reading each character, then you have to
store them someplace as they are read.

Notice that each time you allocated memory you ask for twice the
amount of memory that you have already used. The only real
benefit that I can see to repeated realloc calls is if the goal
is to keep the maximum allocated memory to a minimum. If you can
afford to allocate so much memory, why not allocate it in blocks
that get written to once and then stick around to get
concatenated at the end.



Relevant Pages

  • Re: How to take in a string of any size?
    ... >the contents into the allocated memory. ... nobody said the string started at the beginning of the file. ... >number of calls to realloc() isn't going to be excessive). ...
    (comp.lang.c)
  • Re: [newbie] question about free() and
    ... >Here is some string tokenization code: ... >resource from being freed in the end? ... You have to call freeas soon as you no longer need the allocated memory ... address of an allocated memory block is called creating a memory leak, ...
    (comp.lang.c)
  • Re: A better BinaryConverter function in C++
    ... string ConvertToBinary(string Msg) ... You seem to believe these two lines copy the string into a newly ... allocate a block of memory and store its address in pCMsg pointer. ... and the allocated memory is leaked. ...
    (microsoft.public.vc.language)
  • Re: "" notation, does it define strings?
    ... Is the "abcde" treated as a string or is it ... just used to initialize the character array? ... they like to initialize all allocated memory ...
    (comp.lang.c)
  • Re: How to take in a string of any size?
    ... > is to keep the maximum allocated memory to a minimum. ... number of times realloc() is called by a significant amount. ... The idea is that if the string becomes as large as the buffer, ...
    (comp.lang.c)