Path: utzoo!attcan!uunet!decwrl!ucbvax!bisco.kodak.com!bilbo From: bilbo@bisco.kodak.com Newsgroups: comp.lang.c Subject: Re: Life after free? Message-ID: <9010021433.AA20306@bisco.kodak.COM> Date: 2 Oct 90 14:33:44 GMT References: Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: nobody@Kodak.COM Organization: Eastman Kodak Company; Rochester, NY Lines: 45 > char *funny(ch) > char ch; > { > char *tmp; > int i; > > tmp = (char *) malloc(100); > for (i=0; i<=99 ; i++) *(tmp+i) = ch; > free(tmp); > return tmp; > } > > Any comments on free-ing tmp before it is return-ed? GAG! CHOKE! COUGH! 1/2 :-) I'd say you might get lucky if the calling function copied the result out of the "buffer" IMMEDIATELY (i.e., in the very next statement) after the return. Most machines wouldn't have destroyed it by that time, but I wouldn't guarantee ANYTHING. This looks like the (BROKEN) code I've seen many times: char * foobar() { char x[100]; /* NOTE: this is an AUTOMATIC variable */ /* put something in x[] */ return x; } Note that, since x is on the stack, it MIGHT not get blown away until after you have a chance to move it somewhere, but don't bet your life on it! -- Chuck Tryon (PLEASE use this address, as Kodak foobars one in header!) USmail: 46 Post Ave.;Roch. NY 14619 B. Baggins <<...include standard disclamer...>> At Your Service "Then again, squirrels could be stupid." (D. Mocsny)