home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / amiga / programm / 15649 < prev    next >
Encoding:
Text File  |  1992-11-10  |  2.7 KB  |  67 lines

  1. 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
  2. From: jamie@cdevil.unx.sas.com (James Cooper)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: What is wrong with this very simple c program?
  5. Message-ID: <BxIpyB.E7@unx.sas.com>
  6. Date: 10 Nov 92 20:51:47 GMT
  7. References: <1992Nov10.184406.25907@sol.UVic.CA>
  8. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  9. Organization: SAS Institute Inc.
  10. Lines: 53
  11. Originator: jamie@cdevil.unx.sas.com
  12. Nntp-Posting-Host: cdevil.unx.sas.com
  13.  
  14.  
  15. In article <1992Nov10.184406.25907@sol.UVic.CA>, aramsey@ugly.UVic.CA (Aaron  Ramsey) writes:
  16. >I wrote this as part of a larger program, and it keeps locking up my machine.
  17. >When I run it, it prints "started....done...." then my machine either locks
  18. >up, or a requestor comes up asking me to either suspend or reboot. The
  19. >guru error on this is 81000004. I can't figure out why such a simple program
  20. >should be doing this. I tried setting my stack very very high to see if
  21. >that made any difference (stack 200000) but it still did the same. If I use
  22. >the ordinary stack size (whatever that might be... around 2000 I guess)
  23. >the program terminates with a stack overflow error, but doesn't lock up
  24. >my machine. I have a vxl-30, and have tried compiling it under both 030
  25. >and normal 000 code, but it makes no difference....  Does anybody have
  26. >a clue? I've only been programming in C for a week or two, and I can't
  27. >spot any obvious problems... 8-(
  28. >
  29. >Here is the code.
  30. >
  31. >#include <stdio.h>
  32. >
  33. >main()
  34. >{
  35. >   int zoop[1000];
  36. >   int counter;
  37. >
  38. >   printf ("started....");
  39. >   for (counter = 0; counter < 5000; counter += 5) {
  40. >      zoop[counter] = counter;
  41. >   }
  42. >   printf("done....");
  43. >}
  44.  
  45. You only allocated enough space to store 1000 'int's, then tried to
  46. write 5000 'int's into that space.  This means 4000 'int's worth of
  47. memory your program didn't own got trampled on.  Its no wonder your
  48. machine crashed.... :-)  Yes, you did increment the counter by 5 instead
  49. of just one, but this just means you skipped trashing a few bytes,
  50. instead of trashing them all.
  51.  
  52. C doesn't have any protection mechanism to see if you go outside the
  53. boundaries of an array.  You have to check for this yourself.
  54.  
  55. Also, remember that in C, arrays start from element 0, and proceed to
  56. arraysize-1.  In your example, you can legally address zoop[0] to
  57. zoop[999], but anything outside that range means you are writing into
  58. memory you don't own.
  59.  
  60. -- 
  61. ---------------
  62. Jim Cooper
  63. (jamie@unx.sas.com)                             bix: jcooper
  64.  
  65. Any opinions expressed herein are mine (Mine, all mine!  Ha, ha, ha!),
  66. and not necessarily those of my employer.
  67.