From owner-freebsd-questions@FreeBSD.ORG Sun Sep 21 12:57:07 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E994E1065678 for ; Sun, 21 Sep 2008 12:57:07 +0000 (UTC) (envelope-from trashy_bumper@yahoo.com) Received: from web110502.mail.gq1.yahoo.com (web110502.mail.gq1.yahoo.com [67.195.8.250]) by mx1.freebsd.org (Postfix) with SMTP id C20688FC08 for ; Sun, 21 Sep 2008 12:57:07 +0000 (UTC) (envelope-from trashy_bumper@yahoo.com) Received: (qmail 93711 invoked by uid 60001); 21 Sep 2008 12:57:07 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Message-ID; b=k9TH/yieHGDTOrzn988AfmrzTMKGbtgUfql+eS25ammYYpgQO3vkz6o4mWlxzKZC3dnRo+jl0ZdVcT320A6Hber+GIpONfKzZsey6jxaT2aBY4ky7Ybi7LG3HNa5a5aomZTy65myfxiSkh9l9eN6j3ulTtfrhKwLz5l97+usYi4=; X-YMail-OSG: hyCK5gAVM1nIFbn5QKDK.pQvW3it_xLvgb2hgnfxJHwzfIiW38bZdHl6QkcbdXjnbU4RRi27ZKNn0knk3O9rcm9izIbQWcWroNhJoDWYFkLLx5hZiLjnrFJYVzbN5EjlR9sALA-- Received: from [77.122.205.244] by web110502.mail.gq1.yahoo.com via HTTP; Sun, 21 Sep 2008 05:57:06 PDT X-Mailer: YahooMailWebService/0.7.218.2 Date: Sun, 21 Sep 2008 05:57:06 -0700 (PDT) From: Nash Nipples To: freebsd-questions@freebsd.org In-Reply-To: <200809210206.52409.fbsd.questions@rachie.is-a-geek.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Message-ID: <266527.92897.qm@web110502.mail.gq1.yahoo.com> Subject: Re: Segmentation fault when free X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: trashy_bumper@yahoo.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 21 Sep 2008 12:57:08 -0000 > > can someone please explain to me what happens to the > allocated memory > > called within a function assigned to its local pointer > after this function > > ends > > Ok - let's see if I get this right: > - the allocated memory > - called within a function > - assigned to a local pointer > > Any malloc'ed memory is application global accessible. > Assigning a pointer to > a variable doesn't allocate memory (the compiler and > runtime libraries > already setup storage for the variable, at declaration > time). So, I have no > idea what you mean with the "called within a > function" part. > > -- > Mel > thanks for making it even more clear to me. actually what i meant was this: void function(void){ char *p; p = malloc(1); } int main(void){ while (1){ function(); /* in the end of this function function() * the memory is still allocated * even when the only pointer who knows its address * does not longer exist * which is why we have to free() the memory * during the application runtime * to avoid it from growing to ridiculous size */ } } but even if you kill -SEGV `pgrep this` (Segmentation fault (core dumped) the memory is getting freed anyway (presumably by the glorious kernel). which you can see dynamicly by typing top in the console. in other words segmentation fault when free() is not a scary thing here. it is a matter of style and the way to find own errors. or maybe reading warnings if you compile with the flags -ansi -pedantic oh and by the way: > char * > function(void) > { > char buffer[100]; > > return buffer; > } that is an easier approach because you get warned on passing an address to a local variable