Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!adm!mike@BRL.ARPA From: mike@BRL.ARPA (Mike Muuss) Newsgroups: comp.unix.wizards Subject: Re: Problems with pointers Message-ID: <9560@brl-adm.ARPA> Date: Thu, 1-Oct-87 08:28:31 EDT Article-I.D.: brl-adm.9560 Posted: Thu Oct 1 08:28:31 1987 Date-Received: Mon, 5-Oct-87 02:07:48 EDT Sender: news@brl-adm.ARPA Lines: 22 malloc(3) puts a small "header" in memory immediately before the address of the pointer that it returns to you. In there, it stores the size of the block. Thus, just handing the pointer back to free(3) has the proper effect. Here is some code that I often use to do this sort of thing: /* Acquire storage for a given struct, eg, GETSTRUCT(ptr,structname); */ #define GETSTRUCT(p,str) \ if( (p = (struct str *)malloc(sizeof(struct str)) == \ (struct str *)0 ) {\ fprintf(stderr,"malloc str failure\n"); \ exit(17); } \ bzero( (char *)p, sizeof(struct str)); Memory acquired in this way is then released just by saying: free( (char *) p ); The cast is desirable as free(3) expects a char * argument. Best, -Mike