Re: How to take in a string of any size?
From: William L. Bahn (william_at_toomuchspam.net)
Date: 08/23/04
- Next message: ur8x_at_ur8x.com: "Re: Double pointer and 2D array"
- Previous message: Alan Connor: "Re: [OT] Perl to C Converter?"
- In reply to: SM Ryan: "Re: How to take in a string of any size?"
- Next in thread: Richard Pennington: "Re: How to take in a string of any size?"
- Reply: Richard Pennington: "Re: How to take in a string of any size?"
- Reply: Gordon Burditt: "Re: How to take in a string of any size?"
- Reply: SM Ryan: "Re: How to take in a string of any size?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: ur8x_at_ur8x.com: "Re: Double pointer and 2D array"
- Previous message: Alan Connor: "Re: [OT] Perl to C Converter?"
- In reply to: SM Ryan: "Re: How to take in a string of any size?"
- Next in thread: Richard Pennington: "Re: How to take in a string of any size?"
- Reply: Richard Pennington: "Re: How to take in a string of any size?"
- Reply: Gordon Burditt: "Re: How to take in a string of any size?"
- Reply: SM Ryan: "Re: How to take in a string of any size?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|