Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 01 Apr 1999 07:25:33 -0800
From:      John Milford <jwm@CSUA.Berkeley.EDU>
To:        James Snow <sno@teardrop.net>
Cc:        freebsd-questions@FreeBSD.org
Subject:   Re: Curiosity Killed the Array
Message-ID:  <199904011525.HAA14508@soda.CSUA.Berkeley.EDU>
In-Reply-To: Message from James Snow <sno@teardrop.net> of "Thu, 01 Apr 1999 09:03:32 EST." <Pine.BSF.4.05.9904010852560.36839-100000@silver.teardrop.net>

next in thread | previous in thread | raw e-mail | index | archive | help

	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 <sno@teardrop.net>  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


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199904011525.HAA14508>