Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!mcvax!eurifb!wietse From: wietse@eurifb.UUCP (Wietse Venema) Newsgroups: comp.unix.wizards Subject: Re: Problems with pointers Message-ID: <214@eurifb.UUCP> Date: Tue, 29-Sep-87 15:37:23 EDT Article-I.D.: eurifb.214 Posted: Tue Sep 29 15:37:23 1987 Date-Received: Sat, 3-Oct-87 07:49:03 EDT References: <9519@brl-adm.ARPA> Organization: EUR/Bedrijfskunde,PObox 1738,Rotterdam,Neth. Lines: 26 Summary: don't worry In article <9519@brl-adm.ARPA>, GLASGOW@NOSC-TECR.arpa writes: > int *p; > p = (some_big_structure *)malloc(sizeof(some_big_structure)); > > My problem arises when I try to release this memory back to the system. If > I use free(), will it assume that p points to an integer and release only > two bytes, or will it know that p is a larger structure and release the > proper number of bytes. Also, if it releases only two bytes, is there a > way around this. All of the different structures are large and memory is > tight, so this is definitely a problem. The calling sequence of free() is (from /usr/lib/llib-lc): free(p) char *p; All pointer values should be converted to char * before passing them to free(). Hence, from the pointer value alone free() can't tell what block of memory a pointer is pointing to. Malloc() keeps its own administration of the size of blocks of memory it doles out so that free() knows how much memory is being released. A nice example of an implementation of malloc() can be found in "The C programming language" by Kernighan & Ritchie. Wietse Venema (uucp: mcvax!eutwc1!wietse) (bitnet: wswietse@heithe5)