home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!pagesat!spssig.spss.com!news.oc.com!eff!sol.ctr.columbia.edu!caen!sdd.hp.com!swrinde!elroy.jpl.nasa.gov!decwrl!concert!sas!mozart.unx.sas.com!jamie
- From: jamie@cdevil.unx.sas.com (James Cooper)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: What is wrong with this very simple c program?
- Message-ID: <BxIpyB.E7@unx.sas.com>
- Date: 10 Nov 92 20:51:47 GMT
- References: <1992Nov10.184406.25907@sol.UVic.CA>
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Organization: SAS Institute Inc.
- Lines: 53
- Originator: jamie@cdevil.unx.sas.com
- Nntp-Posting-Host: cdevil.unx.sas.com
-
-
- In article <1992Nov10.184406.25907@sol.UVic.CA>, aramsey@ugly.UVic.CA (Aaron Ramsey) writes:
- >I wrote this as part of a larger program, and it keeps locking up my machine.
- >When I run it, it prints "started....done...." then my machine either locks
- >up, or a requestor comes up asking me to either suspend or reboot. The
- >guru error on this is 81000004. I can't figure out why such a simple program
- >should be doing this. I tried setting my stack very very high to see if
- >that made any difference (stack 200000) but it still did the same. If I use
- >the ordinary stack size (whatever that might be... around 2000 I guess)
- >the program terminates with a stack overflow error, but doesn't lock up
- >my machine. I have a vxl-30, and have tried compiling it under both 030
- >and normal 000 code, but it makes no difference.... Does anybody have
- >a clue? I've only been programming in C for a week or two, and I can't
- >spot any obvious problems... 8-(
- >
- >Here is the code.
- >
- >#include <stdio.h>
- >
- >main()
- >{
- > int zoop[1000];
- > int counter;
- >
- > printf ("started....");
- > for (counter = 0; counter < 5000; counter += 5) {
- > zoop[counter] = counter;
- > }
- > printf("done....");
- >}
-
- You only allocated enough space to store 1000 'int's, then tried to
- write 5000 'int's into that space. This means 4000 'int's worth of
- memory your program didn't own got trampled on. Its no wonder your
- machine crashed.... :-) Yes, you did increment the counter by 5 instead
- of just one, but this just means you skipped trashing a few bytes,
- instead of trashing them all.
-
- C doesn't have any protection mechanism to see if you go outside the
- boundaries of an array. You have to check for this yourself.
-
- Also, remember that in C, arrays start from element 0, and proceed to
- arraysize-1. In your example, you can legally address zoop[0] to
- zoop[999], but anything outside that range means you are writing into
- memory you don't own.
-
- --
- ---------------
- Jim Cooper
- (jamie@unx.sas.com) bix: jcooper
-
- Any opinions expressed herein are mine (Mine, all mine! Ha, ha, ha!),
- and not necessarily those of my employer.
-