Originally Posted by
tabstop
Eh, you only have to type it once, but it needs to be inside the loop (if getline happens 1000000 times, free needs to happen 1000000 times, or else you've leaked the first 999999 of them).
Well. This is probably due to my misunderstanding of this (boldface) statement in The GNU C Library Reference Manual:
Do not expect to find any data (such as a pointer to the next block in a chain of blocks) in the block after freeing it.
to mean that free just plain eliminates the variable. So I thought this would fail
Code:
int main() {
char *this;
while (1) {
this=malloc(5);
strcpy(this,"that");
puts(this);
free(this);
}
}
which it doesn't. More thinking for me to do...thanx tabstop
ps. by "leaking" memory you mean using excessively unnessesarily -- or could something genuinely bad happen, like an overwrite? And isn't that counter-intuitive -- shouldn't multiple malloc's on the the same variable deal with the same starting block? Or is that impossible because the size will vary in the stack (or wherever)?