Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ukma!uunet!mcvax!dnlunx!gew From: gew@dnlunx.UUCP Newsgroups: comp.unix.wizards Subject: Re: Problems with pointers Message-ID: <265@dnlunx.UUCP> Date: Tue, 29-Sep-87 05:20:23 EDT Article-I.D.: dnlunx.265 Posted: Tue Sep 29 05:20:23 1987 Date-Received: Sat, 3-Oct-87 09:24:34 EDT References: <9519@brl-adm.ARPA> Organization: Dr Neher Laboratory (PTT) Lines: 56 Summary: free() problem = not a problem at all In article <9519@brl-adm.ARPA>, GLASGOW@NOSC-TECR.arpa writes: ) int *p; ) ) and by using: ) ) p = (some_big_structure *)malloc(sizeof(some_big_structure)); ) ) I can get p pointing to a memory block of the correct size for the structre. ) ) 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. free() uses size information left behind by malloc() or calloc() (usually in a few bytes just preceding the memory allocated). there is no way of deciding from the C code which size the allocated object is, as you can in Pascal. So the answer is *GO*RIGHT*AHEAD*. Using an integer pointer probably won't pose any problems, but generic pointers can be declared like this: union gptr { struct big *big_p; struct even_bigger *ebgr_p; struct huge *huge_p; } If you happen to know the type of object you're freeing, you might even use switch(ptrtype){ case a_big_p: free(p.bigp); break; case an_ebgr_p: free(p.ebgr_p); break; case a_huge_p: free(p.huge_p); break; default: panic(); } None of this will make any difference though on 'normal' computer architectures. If you don't use the above strategy, use a char * pointer (this is malloc()s result type anyway) -- | Ge' Weijers | Disclaimer: the views expressed | | PTT Dr. Neher Laboratories | are usually entirely my own, | | Leidschendam, the Netherlands | not my employers'. I don't own | | uucp: ...!mcvax!dnlunx!gew | a cat to share them with. |