From owner-freebsd-hackers Mon Oct 21 11: 0:47 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 9B0C037B404 for ; Mon, 21 Oct 2002 11:00:45 -0700 (PDT) Received: from gull.mail.pas.earthlink.net (gull.mail.pas.earthlink.net [207.217.120.84]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5C7C543E65 for ; Mon, 21 Oct 2002 11:00:42 -0700 (PDT) (envelope-from tlambert2@mindspring.com) Received: from pool0274.cvx21-bradley.dialup.earthlink.net ([209.179.193.19] helo=mindspring.com) by gull.mail.pas.earthlink.net with esmtp (Exim 3.33 #1) id 183gqy-0005y0-00; Mon, 21 Oct 2002 11:00:37 -0700 Message-ID: <3DB4407E.A9F06218@mindspring.com> Date: Mon, 21 Oct 2002 10:59:26 -0700 From: Terry Lambert X-Mailer: Mozilla 4.79 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: Danny Braniss Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: malloc References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Danny Braniss wrote: > the attached program - which shows the 'efficiancy' of our scientific > programmers - tickled my curiosity. [ ... ] > comments? Your code is not efficient; try this instead: > #include > #include > #include > > int MAX_N, mem; > > int main(int argc , char ** argv){ > int i; > int ** arr; int *space; > struct timeval t1, t2; > > if(argc > 1) > MAX_N = atoi(argv[1]); > else > MAX_N = 100000000; > > gettimeofday(&t1, 0); > mem = sizeof (int *) * MAX_N; > arr = malloc(mem); > if(arr == NULL) { > perror("Malloc"); > exit(1); > } space = malloc(sizeof(int)* MAX_N); if(space == NULL) { perror("malloc2"); printf("failed to allocate array space\n"); exit(1); } for (i = 0 ; i < MAX_N ; ++i ){ arr[i] = &space[ i]; *arr[i] = i; } > gettimeofday(&t2, 0); > printf ("%s took %f seconds, mem used=%d(%dM)\n", > getenv ("HOST"), > t2.tv_sec-t1.tv_sec + (t2.tv_usec-t1.tv_usec)/(float)(1000000), > mem, mem / (1024 * 1024)); > > for (i = 0 ; i < MAX_N ; ++i ){ > if(*arr[i] != i) > printf("GUEVALT! %d] %d\n", i, *arr[i]); > } > exit(0); > } The reason it crashes under FreeBSD is that you are allocating more memory than you have swap + RAM for, but not more memory than you have available address space for; see also "man malloc", and see if you have debugging options turned on. Note that there is no "magic flag" to turn overcommit into spelicit commit on memory allocation (maybe "J" would qualify, since it forces initialization). The normal way you would do this is to write your own allocator that allocated in page-sized chunks, touched each page, and had a fault signal handler, and backing off, OR setting the 'memoryuse' and 'datasize' limits before tunning the process ("man limit" and also "man login.conf"). -- Terry To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message