home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / TI_BC1.ZIP / TI1705.ZIP / TI1705.ASC
Text File  |  1993-10-12  |  3KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1705
  9.   VERSION  :  3.X
  10.        OS  :  DOS OS/2
  11.      DATE  :  October 12, 1993                         PAGE  :  1/2
  12.  
  13.     TITLE  :  User implemented memory management
  14.  
  15.  
  16.  
  17.  
  18.   /* TESTPARY.CPP: User implemented memory management
  19.  
  20.      The following example allocates a block of memory, creates
  21.      an array of offsets at the start of this memory and installs
  22.      an arrays of strings to which these offsets point. No
  23.      compiler generated arrays are specified. The offsets are
  24.      relative to the start of the allocated block of memory, i.e.,
  25.      'string' location = 'startOfMemoryBlock'+'offset'.
  26.  
  27.      After initializing the memory allocation, this example calls
  28.      a function which displays the strings.
  29.  
  30.      DEMONSTRATES:
  31.        - use of ostrstream with user supplied buffer
  32.        - use of casting operations to control pointer math
  33.        - use of user managed arrays
  34.  
  35.      NOTES: (1) Tested with BC++ for OS/2 1.0 and BC++ for DOS 3.1
  36.   */
  37.  
  38.   #include <iostream.h>
  39.   #include <strstrea.h>
  40.  
  41.   void testPass(short *t) {
  42.     short i=0;
  43.     char *tt = ((char*)(unsigned int)t+(short)t[i]);
  44.  
  45.     // Array is terminated by a -1 (0xffff).
  46.     while (t[i] != -1) {
  47.       cout << endl << tt;
  48.  
  49.       // 't' must be cast to 'int' so that the compiler operates
  50.       // on its address. The sum is cast to char* so that the
  51.       // iostream machinery will display a string rather than an
  52.       // address. The whole expression is enclosed in parens to
  53.       // resolve the precedence ambiguity between the summation
  54.       // operator and the insertion operator.
  55.       tt = ((char*)(unsigned int)t+(short)t[++i]);
  56.     }
  57.   }
  58.  
  59.   //**************************************************************
  60.   int main(void) {
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1705
  75.   VERSION  :  3.X
  76.        OS  :  DOS OS/2
  77.      DATE  :  October 12, 1993                         PAGE  :  2/2
  78.  
  79.     TITLE  :  User implemented memory management
  80.  
  81.  
  82.  
  83.  
  84.     // Give us 1000 bytes (sizeof(short)*500) of heap.
  85.     short *newMemory = new short[500];
  86.     if (!newMemory) {
  87.       cout << "\nError";
  88.       return 1;
  89.     }
  90.  
  91.     // Initialize 1st 20 bytes with 10 shorts; These 10 shorts
  92.     // represent offsets from the start of 'newMemory' to
  93.     // data stored later in 'newMemory'. Each item is a maximum
  94.     // of 20 bytes long and the first item starts at location
  95.     // 'newMemory'+0x300.
  96.     for (int i=0; i<10; i++) {
  97.  
  98.       // Stuff offset into 'i'th offset array slot.
  99.       newMemory[i] = (unsigned short)(0x300+(i*20));
  100.  
  101.       // Create a string pointer to the 'i'th data item and...
  102.       char *slot = (char*)newMemory+newMemory[i];
  103.  
  104.       // ...setup an ostrstream object using that location.
  105.       ostrstream aslot(slot, 20);
  106.  
  107.       // Now compose a string into that location.
  108.       aslot << "string" << i << ends;
  109.     }
  110.  
  111.     newMemory[i] = 0x0ffff;
  112.  
  113.     // Using the 10 offsets, display the strings.
  114.     testPass(newMemory);
  115.     return 0;
  116.   } // end of main()
  117.  
  118.  
  119.   DISCLAIMER: You have the right to use this technical information
  120.   subject to the terms of the No-Nonsense License Statement that
  121.   you received with the Borland product to which this information
  122.   pertains.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.