Traduisez - Übersetzen - Traduzca - Traduza - Tradurre - Translate

VanLUG Email Archive

VanLUG Mailing List
Re: Memory allocation under Linux

New Message Reply About this list Date view Thread view Subject view Author view

Curt Sampson (cjs@cynic.net)
Fri, 15 Jan 1999 13:56:13 -0800 (PST)


On Fri, 15 Jan 1999, Shane Wegner wrote:

> In the following function, does s free itself?
>
> int test(void) {
> char *s;
> s = malloc(1024);
> return 0;
> }

No.

> The reason I ask this is I am wondering if I have to free all my variables
> whenever I return out of a function. For example, when I use char *strstr
> in a function, is it necessary to free the result inorder to prevent a
> memory leak?

You must free all memory allocated with malloc. In the above
function, *s (the four or eight bytes allocated to the pointer) is
placed in the stack and will be freed automatically. The 1024 bytes
allocated by malloc is in the data area of your program (sometimes
referred to as the `heap') and will not be freed unless free() is
called with a pointer to it.

Some other functions allocate memory as well. For example, strdup()
allocates an area of memory (with malloc()), copies a string to
it, and returns a pointer to that area of memory. The memory this
returned pointer points to must be freed with free().

Other functions don't; they return a pointer to a static buffer
that they had previously allocated. crypt() is an example of this;
it has space compiled into its data area for the encrypted string
that it returns, and it returns a pointer to that space. The next
time it is called, the last return value is overwritten with the
more recent one. If you wanted a copy of the last value, you have
to copy it somewhere else (with strdup() :-)) before you call
crypt() again.

strstr() is a function that does neither; no memory is allocated
or written. It simply returns a pointer that points to a different
place than the first pointer. So, to try to illustrate it graphically,
if you have a[] = "foobarfoobar", b[] = "bar", and char *c, and call
it with `c = strstr(a, b);' you'll end up with this:

    foobarfoobar bar
    | ^--- *c ^---- *b;
    |------ *a

Note that *a and *c both point into the same string; one is just
a little further along than the other. No memory is allocated or
freed.

A very typical trick for this sort of thing is to turn the first
word in a string into a null terminated string on its own, for
parsing reasons or whatever:

    char a[] = "This is a sentence."
    char *c;

    c = strstr(a, " "); /* Find the first space. */
    *c = '\0'; /* Turn it into a null. */
    c++; /* Point to the string after the null. */

This leaves you with a[] = "This" and c[] = "is a sentence." After
dealing with a appropriately, you can set a = c and do it all over
again to deal with the next word.

cjs

--
Curt Sampson  <cjs@cynic.net>   604 801 5335   De gustibus, aut bene aut nihil.
The most widely ported operating system in the world: http://www.netbsd.org


New Message Reply About this list Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b3 on Fri 15 Jan 1999 - 13:56:40 PST