TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: ANTHONY TIBBS
from: GEORGE WHITE
date: 1998-03-07 17:56:00
subject: Strange allocation error

Hi Anthony,
You asked:
AT>I was attempting to allocate some memory today using another person's
AT>system (running TC++ 3.0) using the following code:
AT>#include 
AT>#include 
AT>#include 
AT>int main (void)
AT>{
AT>char *x;
AT>   printf ("Free mem=%ld\n", coreleft();
You need to cast the return from coreleft to unsigned long, as in small
model coreleft() returns an unsigned int, and also change the print
format specifier to match. So you need:
printf ("Free mem=%lu\n",(unsigned long)coreleft());
for a better overall display:
printf ("\nFree mem=%lu, Far=%lu, ",
                (unsigned long)coreleft(),farcoreleft());
AT>   /* In small memory model, this gives ~63.4k
AT>      In large memory model, result is random (-4382 one time,
AT>                                               4832382593 the next...) */
If you cast as above that should be fixed. I also suspect your recall is
faulty and the random result was in the small model.
AT>   x = (char *)malloc (20);
AT>   if (!x)
AT>   {
AT>      printf ("Out of memory.");
AT>      return 1;
AT>   }
In my test code I added:
    else
      printf ("%p",x);
AT>   /* In small memory model, x=ds:7436 (OK, this value changes)
AT>      In large memory model, x=0000:0004 EVERY SINGLE TIME. */
I got the same reply every time too...
I would expect the return value to be the same every time when called
under the same conditions in this simple instance.
AT>   free (x);
AT>   return 0;
AT>}
AT>Any idea what could be causing a pointer to 0000:0004 to be returned
AT>on every single malloc()?
I found xxxx:0004 everytime in large model, with xxxx the same for every
run under the same conditions but varied between IDE and standalone.
My results (BC 3.1, code modified as above)
Small Model
IDE               Free Mem= 63520, Far= 1424, 05A6
Shelled from SLMR Free Mem= 63520, Far= 525808, 05A6
Stand alone       Free Mem= 63520, Far= 537168, 05A6
Large Model
IDE               Free Mem= 61296, Far= 61296, 8EEC:0004
Shelled from SLMR Free Mem= 584144, Far= 584144, 1161:0004
Stand alone       Free Mem= 595504, Far= 595504, 0E9B:0004
George
 * SLMR 2.1a * All Trademarks acknowledged (just in case ).
--- Maximus/2 3.01
---------------
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.