From owner-freebsd-hackers Thu Apr 1 7:25:55 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from soda.CSUA.Berkeley.EDU (soda.CSUA.Berkeley.EDU [128.32.43.52]) by hub.freebsd.org (Postfix) with ESMTP id 034FA14FEF for ; Thu, 1 Apr 1999 07:25:53 -0800 (PST) (envelope-from jwm@CSUA.Berkeley.EDU) Received: from soda.CSUA.Berkeley.EDU (localhost [127.0.0.1]) by soda.CSUA.Berkeley.EDU (8.8.8/) via ESMTP id HAA14511 for ; Thu, 1 Apr 1999 07:25:34 -0800 (PST) env-from (jwm@CSUA.Berkeley.EDU) Message-Id: <199904011525.HAA14511@soda.CSUA.Berkeley.EDU> Date: Thu, 01 Apr 1999 07:25:33 -0800 From: John Milford Subject: Re: Curiosity Killed the Array Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG ------- Blind-Carbon-Copy To: James Snow Cc: freebsd-questions@FreeBSD.org Subject: Re: Curiosity Killed the Array In-reply-to: Message from James Snow of "Thu, 01 Apr 1999 09:03:32 EST." Date: Thu, 01 Apr 1999 07:25:33 -0800 From: John Milford This is probably the wrong mailing list for this question, but I'll try to answer it anyway. I'm moving it to freebsd-questions as that seems to be the appropriate forum. This is because your array is on the stac and kernel automatcally grows the stack for you (up to the max stack size). If you move the array onto the heap you will get different behavior. --JOhn void main( void ) { int *array; int i = 0; array = (int *)malloc(10 * sizeof int); while ( 1 == 1) { array[i] = i; print("%d\n", i); i--; } } James Snow wrote: > > In working on a C program recently, I ran into some bugs, resolved them, > and then in resolving them realized that there isn't any run-time checking > of array boundaries. > > I thought this was kind of interesting and wondered about whether or not > it could be a problem somehow. So I wrote a little C program that looks > like this: > > void main( void ) { > int array[10]; > int i = 0; > while ( 1 == 1) { > array[i] = i; > print("%d\n", i); > i++; > } > } > > to see what would happen. Much to my suprise, it increments all the way to > 400 or 500 or so and then cores. (Bus error, I think.) > > I thought about this for a while and came to the conclusion that the > kernel allocates me a bit of space to work within and as long as I don't > step outside that space, it doesn't care what I'm doing. OK, that makes > sense. (To me anyway.) > > So then I wondered what would happen if I changed i++; to i--; > > When I ran it, it spews negative numbers as you would expect, but it just > keeps going. Watching the process in top, it started sucking up swap and > everything. It filled up the swap space on my box before the kernel jumped > in and said 'Bad!' and killed it. > > So, I'm just curious as to the technical reasons behind this. (If anyone > is bored and cares to explain this to someone who's recently gotten > curious as to how the kernel does stuff.) > > > TIA, > -sno > o - - - - - - - - - - - - - o - - - - - - - - - - - - o > | We live in the short term | sno at teardrop dot org | > | and hope for the best. | I am Geek. Hear me ^G | > o - - - - - - - - - - - - - o - - - - - - - - - - - - o > > > > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-hackers" in the body of the message ------- End of Blind-Carbon-Copy To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message