S
Stephen Ramsay
I'm wondering if someone can help me untangle some muddled thinking
about memory allocation in C. I'm writing my first non-trivial app in
C (and having a great time), but I'm finding it difficult to stamp out
memory leaks.
I'm sure most of what I'm about to say is wrong. I'd really be very
grateful to know why.
The use of malloc() in most of the books I've read make it sound pretty
straightforward. If you allocate some memory (because the required
amount of space isn't known until runtime), you have to free that space
before the program ends.
The standard examples go something like this: You need to allocate space
for an array of ints with n elements. When the program knows the value
of n, you can allocate the memory like so:
int *p;
p = malloc(n * sizeof(int));
(I understand from lurking on this list that p = (int *) malloc(n *
sizeof(int)) is deeply offensive to many C fans
When I'm done with that array, I need to free it like so:
free(p);
My way of describing this may not be too precise, but I think I
understand this, and I've been able to use it this way in my programs
without incident.
However, I quickly run into problems the minute things get even slightly
more complex. For example:
0. Let's suppose that I allocate space for p, but there are branches in
the program that won't actually use p. Do I need to free the memory at
every exit point? Or does the fact that this can happen indicate a bad
design on my part?
1. Let's suppose that I pass p into a function. If that function is the
last one that will ever need p, then presumably I can (and should) free
it. But what if p is also used elsewhere? At a certain point, it seems
like I would have to write some kind of reference counter . . .
2. On a similar note, when I pass a pointer to some allocated memory as
an argument to a function, pass it back out, pass it in somewhere else,
aren't I always dealing with the same memory block? Will it not suffice
to release the memory after I'm done with all of this, or am I thinking
about it incorrectly?
3. Is there anything wrong with just freeing all allocated memory blocks
at every exit point? I realize I might still have other sorts of
problems, but wouldn't this prevent leakage?
4. What happens if I attempt to free memory that has already been freed?
My apologies if all of this seems too basic for this list. Perhaps
someone knows of a book that deals with these sorts of things in detail?
Steve
about memory allocation in C. I'm writing my first non-trivial app in
C (and having a great time), but I'm finding it difficult to stamp out
memory leaks.
I'm sure most of what I'm about to say is wrong. I'd really be very
grateful to know why.
The use of malloc() in most of the books I've read make it sound pretty
straightforward. If you allocate some memory (because the required
amount of space isn't known until runtime), you have to free that space
before the program ends.
The standard examples go something like this: You need to allocate space
for an array of ints with n elements. When the program knows the value
of n, you can allocate the memory like so:
int *p;
p = malloc(n * sizeof(int));
(I understand from lurking on this list that p = (int *) malloc(n *
sizeof(int)) is deeply offensive to many C fans
When I'm done with that array, I need to free it like so:
free(p);
My way of describing this may not be too precise, but I think I
understand this, and I've been able to use it this way in my programs
without incident.
However, I quickly run into problems the minute things get even slightly
more complex. For example:
0. Let's suppose that I allocate space for p, but there are branches in
the program that won't actually use p. Do I need to free the memory at
every exit point? Or does the fact that this can happen indicate a bad
design on my part?
1. Let's suppose that I pass p into a function. If that function is the
last one that will ever need p, then presumably I can (and should) free
it. But what if p is also used elsewhere? At a certain point, it seems
like I would have to write some kind of reference counter . . .
2. On a similar note, when I pass a pointer to some allocated memory as
an argument to a function, pass it back out, pass it in somewhere else,
aren't I always dealing with the same memory block? Will it not suffice
to release the memory after I'm done with all of this, or am I thinking
about it incorrectly?
3. Is there anything wrong with just freeing all allocated memory blocks
at every exit point? I realize I might still have other sorts of
problems, but wouldn't this prevent leakage?
4. What happens if I attempt to free memory that has already been freed?
My apologies if all of this seems too basic for this list. Perhaps
someone knows of a book that deals with these sorts of things in detail?
Steve