jhunt@omews3.intel.com (Jim Hunt) (11/29/88)
I have been programming with Turbo C 1.5 recently and have found what appears to be a bug in the code generated for a malloc call. USUALLY the code that is generated looks something like this: mov ax,# of bytes to allocate push ax call far malloc pop cx mov [bp+something],ax mov [bp+something+2],dx (malloc apparently returns its pointer in the ax & dx registers) Ok, so far, so good. However, sometimes the generated code looks like this: mov ax, # bytes push ax call far malloc pop cx cwd mov [bp+x],ax mov [bp+x+2],dx The 'cwd' instruction stands for convert word to double. In effect, it takes the value in the ax register and sign extends it into the dx register, convert- ing it from a signed 16 bit word (in ax) to a signed 32 bit word (in ax & dx). This is nifty (sarcasm intended) but it trashes the pointer returned by malloc before it has been stored! Moreover, the 'cwd' itself seems to be doing nothing useful. If I use my debugger to change the 'cwd' to a 'nop', my program works fine. This bug has defied all my attempts to work around it. I have tried all the memory models; I have tried it with optimization on and optimization off; I have tried rearranging the original source code and even putting the original source statements in separate, stand alone functions. Regardless of all tinkering, the malloc calls that worked originally still work and those that were originally broken stayed broken. Is this a known bug in Turbo C 1.5? Are there any patches/workarounds? Any suggestions at all? Reply by e-mail preferred, will summarize if sufficient interesting responses are sent. Thanks. Jim Hunt Intel Corp. "The company, as usual, disavows any knowledge of my actions."
shurr@cbnews.ATT.COM (Larry A. Shurr) (12/03/88)
In article <3960@omepd> jhunt@omews3.intel.com (Jim Hunt) writes: [Calls to malloc in Turbo C sometimes come out wrong:] [Should be something like this:] > mov ax,# of bytes to allocate > push ax > call far malloc > pop cx > mov [bp+something],ax > mov [bp+something+2],dx >However, sometimes the generated code looks like this: > mov ax, # bytes > push ax > call far malloc > pop cx > cwd > mov [bp+x],ax > mov [bp+x+2],dx I'm using Turbo C 2.0 and I have it right here, so wait a sec... [Time passes] O.K., I'm back now. I tried it and got: push ax call far _malloc pop cx cwd mov word ptr [bp-2],dx mov word ptr [bp-4],ax That's essentially what you're getting. Then I added an include: #include <alloc.h> With that I got: push ax call far _malloc pop cx mov word ptr [bp-2],dx mov word ptr [bp-4],ax Which is what you want. Alloc.h declares the appropriate function prototype for malloc() and other related functions. Without it, of course, the compiler assumes that malloc() returns an int. So it appears that you simply need to get malloc() defined correctly. Good luck (hope I'm right). regards, Larry -- Signed: Larry A. Shurr (att!cbnews!shurr or osu-cis!apr!las) Clever signature, Wonderful wit, Outdo the others, Be a big hit! - Burma Shave (With apologies to the real thing. Above represents my views only.)
allbery@ncoast.UUCP (Brandon S. Allbery) (12/07/88)
As quoted from <3960@omepd> by jhunt@omews3.intel.com (Jim Hunt): +--------------- | I have been programming with Turbo C 1.5 recently and have found what appears | to be a bug in the code generated for a malloc call. USUALLY the code that | is generated looks something like this: | | push ax | call far malloc | pop cx | mov [bp+something],ax | mov [bp+something+2],dx | | However, sometimes the generated code looks like this: | | push ax | call far malloc | pop cx | cwd | mov [bp+x],ax | mov [bp+x+2],dx +--------------- As a guess, I'd say that the latter occurs when you forget to declare malloc() as returning "char *" or "char far *". Remember that sizeof (int) != sizeof (char *).... ++Brandon -- Brandon S. Allbery, comp.sources.misc moderator and one admin of ncoast PA UN*X uunet!hal.cwru.edu!ncoast!allbery <PREFERRED!> ncoast!allbery@hal.cwru.edu allberyb@skybridge.sdi.cwru.edu <ALSO> allbery@uunet.uu.net comp.sources.misc is moving off ncoast -- please do NOT send submissions direct Send comp.sources.misc submissions to comp-sources-misc@<backbone>.
jhunt@omews3.intel.com (Jim Hunt) (12/13/88)
Sorry it took so long to post this, our news link has been somewhat hosed for the past couple of weeks. Thanks to all who responded to my original post -- the error was mine, not Turbo C's (forgot to declare malloc -- should have included alloc.h or stdlib.h at the top of the file). Jim