tx for answers so far
but i used string literals here, but suppose i use fgets in a loop to
process all lines in a file. i store the lines in a variable (char*
mystring). each line is assigned to the string variable. but what happens
with the strings allocated to it earlear?
If you are indeed assigning the line to a string variable with the =
operator, instead of strcpy() or a similar function, then the previous
value is simply ignored. It is as if your code has:
int a;
a=1;
a=2;
and then you're asking what happens to 1 after the third line.
Now, is it a bad thing that the previous values are ignored? It depends
on how they were initially created. If they were string literals, such
as:
mystring = "Hello world!";
then it's entirely OK, because string literals have program-wide
storage, and the compiler will automatically claim them up when the
program exits. However, if you used malloc() to allocate memory for
them, for example:
mystring = malloc(9); /* no need for sizeof(char) as it is always 1 */
then you get what I called "ghost memory", i.e. a memory leak.
for example:
char* mystring;
You should use malloc() to allocate some memory to store into mystring
here.
FILE *stream;
if((stream = fopen ("filename", "r")) != (FILE *)0) {
while((fgets(mystring, 1023, stream)) != (char *)0 ) {
This fgets() call does *NOT* assign a new value to mystring at all.
The same value - i.e. the same memory location - is reused over and
over again. Only the *contents* of that memory location change.
Therefore it is entirely safe to overwrite the previous line with the
next, without free()ing it. Actually, trying to free() it *would* be
wrong, because then the next fgets() call would try to read data into
unallocated memory.
What does this <process each line> contain? Does it have any statements
of the form
mystring = /* ... */;
? If it doesn't, you're all OK, fine, kosher, hunky-dory. If it *does*,
though, then you have to be careful not to end up with memory leaks.
}
} else {
<do fopen error handling>
}
but what happens with the strings i assign to mystring? will i end up with
the whole file except the last line in memory, without references to it?
By the looks of it, I'd guess not. A definite not if my experience is of
any use. It all depends on what you do in the <process each line> that
you did not show. But if we ignore the <process each line>, then the
above code is completely OK, as long as you remember to actually
allocate some memory for mystring to point to. You should not end up
with the whole file except the last line in memory if my intuition about
your not shown code is of any good. Only the last line should remain in
memory.
i don't think C will delete it automatically, because if i say in the loop
char* anotherstring=mystring, i'll end up with two references to the same
string and the compiler can't know the string is still referenced by
another variable.
This won't matter. If you are indeed using the same value for mystring
over and over again, as I suspect you are, then char *anotherstring =
mystring will simply reuse the same value for anotherstring over and
over again too. Both variables are *always* pointing to the *same*
memory location. You can then later call free(mystring) or
free(anotherstring) - BUT NOT BOTH - to free up the allocated memory.