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