Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!bbn.com!syswerda From: syswerda@bbn.com (Gilbert Syswerda) Newsgroups: comp.lang.c++ Subject: Borland C++ memory allocation questions Message-ID: <60903@bbn.BBN.COM> Date: 16 Nov 90 14:51:56 GMT Sender: news@bbn.com Lines: 118 /* This program compiles without errors or warnings, using the large memory model. I have several questions. 1. The return type for sizeof() is defined in alloc.h as unsigned. If this is so, how can it be returning such large values? 2. Why is the size of c3 only 2 bytes? 3. Why is the size of b3 only 2 bytes, yet b3.vals 64K bytes? 4. Looking at c4, how can I define a huge class structure? 5. It would be interesting if someone would run this code under 1.01 to see if it does the same thing. Caveat: I am fairly new to both C and C++. However, I do understand Intel's segmented architecture (and expect Borland to understand it too). I made a similar posting a couple months ago, but was pulled away for a while. My apologies to those who attempted to answer this the last time. */ #include #include typedef char a1[65535]; typedef char a2[65536]; // This is *exactly* 64K worth of chars typedef char huge a3[65537]; // Compiler complains if not declared huge typedef char huge a4[12345678]; // Ditto class c1 { public: unsigned long l; }; class c2 { public: c1 vals[16383]; }; class c3 { public: c1 vals[16384]; // This is exactly 64K worth of longs }; /* Compiler will not compile the following: it complains that the structure is too large. I don't know how to make it huge. class c4 { public: c1 vals[16384]; // This is 64K bytes char ch; // Plus one more }; */ void main() { long l; // This is used to allow the compiler to do type conversion from // whatever sizeof is returning. c2 b2; c3 b3; char a5[65536]; // 64K worth of chars char huge a6[99999]; // more than 64K printf ("\n\n\n"); printf (" Coreleft: %ld\n", l = coreleft()); printf ("Farcoreleft: %ld\n\n", l = farcoreleft()); printf ("Sizeof a1: %ld\n", l = (sizeof (a1))); printf ("Sizeof a2: %ld\n", l = (sizeof (a2))); printf ("Sizeof a3: %ld\n", l = (sizeof (a3))); printf ("Sizeof a4: %ld\n\n", l = (sizeof (a4))); printf ("Sizeof a5: %ld\n", l = (sizeof (a5))); printf ("Sizeof a6: %ld\n\n", l = (sizeof (a6))); printf ("Sizeof c1: %ld\n\n", l = (sizeof (c1))); printf ("Sizeof c2: %ld\n", l = (sizeof (c2))); printf ("Sizeof c3: %ld\n\n", l = (sizeof (c3))); printf ("Sizeof b2: %ld\n", l = (sizeof (b2))); printf ("Sizeof b3: %ld\n\n", l = (sizeof (b3))); printf ("Sizeof b2.vals: %ld\n", l = (sizeof (b2.vals))); printf ("Sizeof b3.vals: %ld\n", l = (sizeof (b3.vals))); } /* Program output Coreleft: 403504 Farcoreleft: 403504 Sizeof a1: 65535 Sizeof a2: 65536 Sizeof a3: 65537 Sizeof a4: 12345678 Sizeof a5: 65536 Sizeof a6: 99999 Sizeof c1: 4 Sizeof c2: 65532 Sizeof c3: 2 Sizeof b2: 65532 Sizeof b3: 2 Sizeof b2.vals: 65532 Sizeof b3.vals: 65536 */