home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / sgi / 11363 < prev    next >
Encoding:
Text File  |  1992-07-25  |  1.7 KB  |  53 lines

  1. Newsgroups: comp.sys.sgi
  2. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!wupost!zaphod.mps.ohio-state.edu!uunet.ca!geac!alias!chk
  3. From: chk@alias.com (C. Harald Koch)
  4. Subject: Subtle bug missed on MIPS processors
  5. Message-ID: <1992Jul24.214731.25626@alias.com>
  6. Sender: news@alias.com (News Owner)
  7. Organization: Alias Research, Inc., Toronto ON Canada
  8. Distribution: na
  9. Date: Fri, 24 Jul 1992 21:47:31 GMT
  10. Lines: 41
  11.  
  12. I thought some of you software developers out there would find this amusing:
  13.  
  14. What's wrong with this piece of code?
  15.  
  16.  
  17. void *
  18. emalloc(size_t numbytes)
  19. {
  20.     void *p;
  21.  
  22.     if ((p = malloc(numbytes)) == NULL) {
  23.     fprintf(stderr, "Out of memory!\n");
  24.     exit(10);
  25.     }
  26. }
  27.  
  28.  
  29.  
  30. This runs fine on MIPS processors (and other register based machines, I
  31. suspect). However, on a stack-based architecture, it fails miserably.
  32.  
  33. the problem? The last line of the function should be
  34.  
  35.     return p;
  36.  
  37.  
  38. The MIPS architecture passes return values in a register. This register is
  39. untouched between the return from the malloc() and the return from
  40. emalloc(), so it works fine.
  41.  
  42. Of course, the really humourous bit (to me) is that the code generator
  43. doesn't optimize out the redundant store/load, making the code two
  44. instructions longer (sw v0,36(sp) followed immediately by lw v0,36(sp)). I
  45. have no idea what effect this has on performance, and I don't care in this
  46. case. I was just suprised.
  47.  
  48. --
  49. "If looks could kill, I     | C. Harald Koch  Alias Research, Inc. Toronto, ON
  50. would have to make a saving | chk@alias.com                (work-related mail)
  51. throw!"                     | chk@gpu.utcs.utoronto.ca     (permanent address)
  52.      -Gerry Smit, 19-May-92 | VE3TLA@VE3OY.#SCON.ON.CA.NA            (AMPRNet)
  53.