From: Christoph Kukulies Message-Id: <199701081113.MAA01144@gilberto.physik.rwth-aachen.de> Subject: Re: free() question In-Reply-To: <32D35994.43BD@post.comstar.ru> from Dim Zegebart at "Jan 8, 97 11:23:48 am" To: zager AT post DOT comstar DOT ru Date: Wed, 8 Jan 1997 12:13:09 +0100 (MET) Cc: djgpp AT delorie DOT com Reply-To: Christoph Kukulies MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit > Hi, > > Just a small question about free() function > > Let's : > > char *ptr1,*ptr2; > > ptr1=(char*)malloc(sizeof(char)*1024); > if (ptr1!=NULL) ptr2=ptr1; > .... > > //so my question. May i use > free(ptr2); Yes, you may use ptr2 as well. It's the address that corresponds to the malloc that's freed, not the pointer. But beware, in the example given you should only free(ptr1) or free(ptr2) if ptr1 or ptr2 have been initialized to some senseful value. In the above code portion ptr2 doesn't get initialized and if the malloc returned a 0 for some reason you would invoke free() later with a wild pointer. > //or i have to use > free(ptr1) > > -- > Regards, > Dim Zegebart, > Moscow Russia. > --Chris Christoph P. U. Kukulies kuku AT gil DOT physik DOT rwth-aachen DOT de