From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: To: 9fans@cse.psu.edu Subject: Re: [9fans] memory woes From: sretzki@gmx.de Date: Fri, 5 Jan 2007 15:51:22 +0100 In-Reply-To: <687643a97ce7ba1e1f4c5ab24eeb5959@coraid.com> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Topicbox-Message-UUID: fdf07a94-ead1-11e9-9d60-3106f5b1d025 > you are forgetting +1 for the null. you need strlen("Hello World")+1. Heh! Yeah, true. But then the strcpy() would write into non-allocated area, so why does the free()-call make it crash? > > - erik > >> #include >> #include >> >> >> void main(int argc, char **argv) { >> int i; >> char *m00; >> >> for(i=0; i<=5; i++) { >> if((m00 = malloc(strlen("Hello World"))) == nil) { >> print("drama! [%d]\n",i); >> exits("malloc"); >> } >> strcpy(m00,"Hello World"); >> print("%d> %s\n",i,m00); >> free(m00); >> } >> exits(nil); >> }