Subject: Re: calloc() and free() ps doesn'r report memory being freed, why?
To: John Maier <JohnAM@datastorm.com>
From: Jon Armstrong <jma@vulcan.lpa.com>
List: netbsd-users
Date: 04/23/1997 21:14:39
> I have been hacking on ftpd so I can add an alternate ftp password   

> So what am I doing wrong?  Do I need to worry about this?

>  passwdfile *password[NUMBER_OF_ENTRIES];
> 
>  *password = calloc(NUMBER_OF_ENTRIES, sizeof(passwdfile));
>  
>  /* manipulate data */
>  /* end data manipulation */
> 
>  free(*password); /* added out of desparation */

Hi John.

Excuse my late response.  I haven't been reading this group closely.

The way you define/declare that structure and the way you allocate
the memory leads me to believe you are being confused, understandably,
by the syntax of the language.

I think part of your problem is not using the right structure for
the job.  I can only guess at what you are trying to do, but here
goes.

The form ... type *name[NUM]; is an array of pointers, not a
pointer to an array of type's, which is what you may really want.

There are two ways to properly handle this.

type (*name)[NUM];  This is a pointer to an array of type's.
name = (type (*)[NUM]) calloc(NUM,sizeof(type));  To alloc/init.
(*name)[n].member;  is the method of access, if type is a struct.
(*name)[n];         is the method of access, if type is fundamental.

Note: I would normally use a typedef in the above section.

or

type *name;   Just a simple pointer to 1 of N type's, where the
              number N is not explicit, much like char *s;
name = (type *) calloc(NUM,sizeof(type));  To alloc/init.
name[n].member;   is the method of access, if type is a struct.
name[n];          is the method of access, if type is fundamental

In both cases above, free(name);  is the method to release the memory.

Hope this helps.

btw:

Your declaration of an array of pointers should be handled quite a
bit differently and I won't attempt to straighten that out here.

Give me a shout, at armstron@eznet.net if you really wanted an array
of pointers and I can help you with that as well.  If you send a bit
more code, I might be able to determine which you really intended..

---------------------------------------------------------------------
 Reply to: armstron@eznet.net - Jon M. Armstrong - LPA Software Inc.
---------------------------------------------------------------------