Originally Posted by
itsme86
Wow, if you don't mind the incredible inefficiency.
I guess my statement about needing realloc() was mistaken nonetheless
Why the memset() though? Wouldn't s[size+1] = 0 work just as well since the preceding characters will all get filled with input?
Very clever use of the stack for temporary input storage
How is that any more inefficient then calling realloc over and over? I dare say it's more efficient. Yeah, memset isn't needed, but it would actually be:
Not "size +1".
Illustrated for the unknowing:
Code:
foo( 0 )
{
c = 'b', size = 0
foo( 1 )
{
c = 'a', size = 1
foo( 2 )
{
c = 'r', size = 2
foo( 3 )
{
c = newline, size = 3
s = malloc( 4 )
s[3] = null
}
s[2] = c;
}
s[1] = c;
}
s[0] = c;
}
So yeah, if we kill the memset call and replace it with a null assignment that works better.
[edit]
To clarify, we'd have to define "efficient" and the frame of reference. It would probably be deemed less efficient in terms of memory. However, in terms of speed it should win out hands down.
[/edit]
Quzah.