[ale] C memory management basic question.

Joseph A Knapka jknapka at earthlink.net
Wed Oct 31 16:31:17 EST 2001


Christopher Fowler wrote:
> 
> I am begining to write some fuctions that replace <pwd.h> functions.
> 
> one will be struct uentry *(char *name).  The function will take a name and
> return a pointer
> to a uentry structure
> 
> struct
> uentry *(char *name)

Uh, presumably you want a function name somewhere in there?

> {
>         struct uentry *uuser;
> 
>         uentry = (struct uentry *)malloc(1);  //Allocate space for one;

No, malloc(1) allocates 1 -byte-. You want

         uentry = (struct uentry *)malloc(sizeof(struct uentry));

>         /* .. Do some stuff  get the correct user */
> 
>         return *uuser;

This returns the thing pointed to by uuser. You want to
return uuser itself:

          return uuser;

> }
> 
> When this function terminates, will the memory allocated for uuser in the
> function
> be reaped?

No. The whole point of malloc() is to allocate memory
from the heap that will persist across function-call
boundaries. You must, of course, release the allocated
memory at some point by calling

free(<value returned by malloc>);

>  Therefor exposing me to possible seg faults down the road? 

If you use C, you -will- experience segfaults. Get used
to it, and learn how to track down their causes.

> What
> would be
> the best way to do the above.  I basically writing a replacement for
> gwtpwnam(char *name);
> 
> I'm not finding many good topics on memory management in my text books.  I
> will look on the web
> for some good tutorials.

It's simple:

- Automatic variables (function parameters and variables
declared within functions) are allocated on the stack; they
vanish when the function they're declared within returns.

- malloc(), calloc() et al. return pointers to memory
allocated on the heap (the free memory pool); such
regions are usable until they are freed using free().

- Global and static functions are allocated from the
"base segment", a permanent storage area set aside
when the executable loads. They are always valid.

So in your function, the 4-byte pointer value "uuser"
is allocated on the stack; it goes away when the
function returns. When you call

uuser = malloc(sizeof(struct uentry));

the value assigned to uuser is the address of a uentry-
sized chunk of memory allocated from the heap. You
can return that value from the function and the memory
it points to will be useable until it's released by a
call to free().

BTW, I'm considering launching a "Programming FAQ",
which will cover general programming principles in
a language-independent manner, and contain pointers
to more in-depth info on algorithms, data structures,
OOP, functional programming, etc. I haven't been able
to find such a thing on the web or on www.faqs.org.
Do y'all think there would be an audience for such
an animal? Or does it already exist?

Cheers,

-- Joe
# "You know how many remote castles there are along the
#  gorges? You can't MOVE for remote castles!" - Lu Tze re. Uberwald
# (Obsolete) Linux MM docs:
http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html

---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list