Subject: Re: Bus error - simple (?) C program
To: None <netbsd-help@netbsd.org>
From: Christos Zoulas <christos@zoulas.com>
List: netbsd-help
Date: 09/10/2001 14:10:45
In article <Pine.NEB.4.31.0109100918430.1097-100000@behemoth.dreo.dnd.ca>,
Claude Marinier <claude.marinier@dreo.dnd.ca> wrote:
Don't allocate the memory on the stack, use malloc instead. You can
use ulimit [sh] or unlimit [csh] to increase the available stack size,
but usually you have more space in the data segment.
christos
>Hi,
>
>I have been staring at this for a few days. There is likely an obvious
>error in this code but I have not seen it yet. Could someone please point
>it out to me? (I am beginning to suspect that it has to to with internal
>addressing of the matrix elements.)
>
>I wrote this to work on 'ulimit' issues. It appears to be a simple program
>to create a large matrix and populate the diagonal with random numbers. It
>now seems, from experience, that it is not simple.
>
>When N is 700, it works as expected. When N is 800, it just prints "Bus
>error". The system is NetBSD 1.5 on i386 with 128 MB of memory. I have
>tried it on other systems.
>
>OS / Platform N result
>------------- - ------
>
>Solaris 7 / SPARC 4 GB 1400 OK
> 1500 Segmentation fault
>
>DecUnix 4.0G / Alpha 640 MB 700 OK
> 800 Segmentation fault
>
>VMS 7.2-1 / Alpha 320 MB 2000 OK
> 10000 runs until exceeds page file quota
>
>
>Note that, on the SPARC box, I used both the Sun and GNU compilers. On the
>other boxes, I used the native compilers.
>
>
>The program follows.
>
>Thank you.
>
>
>/*
> * Test program with a big matrix.
> */
>
>#include <stdio.h>
>#include <stdlib.h>
>
>#define N 10000
>
>int main( void )
>{
> int i;
> float matrix[N][N];
>
> printf( "Big Matrix\n\n" );
> printf( "The matrix is %d by %d; it has %d elements.\n", N, N, N*N );
> printf( "It uses %d bytes.\n", sizeof(matrix) );
>
> for ( i = 0; i < N; i++ ) {
> matrix[i][i] = (float) random();
> }
>
> printf( "\n" );
> printf( "The last element of the matrix if %12.10f\n", matrix[N-1][N-1] );
> printf( "\n" );
>
> return;
>}
>
>
>--
>Claude Marinier, Information Technology Group claude.marinier@dreo.dnd.ca
>Defence Research Establishment Ottawa (DREO) (613) 998-4901 FAX 998-2675
>3701 Carling Avenue, Ottawa, Ontario K1A 0Z4 http://www.dreo.dnd.ca
>