home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX0701.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  1.7 KB  |  47 lines

  1. // \EXAMPLES\EX0701.CPP
  2. // program demonstrate memory allocation
  3.  
  4. // files in this example:
  5. // EX0701.CPP   this file -- main() and supporting functions
  6.  
  7. #include <iostream.h>       // needed for stream output
  8. #include <string.h>         // string manipulation functions
  9.  
  10. void func_a(int& arg_a);
  11.  
  12. void func_b(int* arg_b);
  13.  
  14. int global = 1000;                        // in data area
  15. static int static_in_file = 1500;         // in the data area
  16.  
  17. void main()
  18. { cout << global << endl;
  19.   cout << static_in_file << endl;
  20.   int local_in_main = 2000;               // in stack
  21.   int* free_in_main;                      // in stack
  22.   char* point_to_free;                    // in stack
  23.   point_to_free = new char[13];           // allocate free store
  24.   strcpy( point_to_free, "text in code"); // string copy to free
  25.   func_a(local_in_main);
  26.   func_b(free_in_main);
  27.   delete [] point_to_free;               // deallocate free store
  28.   delete [] free_in_main;                // deallocate free store
  29. }                                        // destroy all objects
  30.  
  31. void func_a(int& arg_a)
  32. { int local_in_func_a = 3000;             // in stack
  33.   static int static_in_func_a = 3500;     // in data area
  34.   cout << arg_a << endl;
  35.   cout << local_in_func_a << endl;
  36.   cout << static_in_func_a << endl;
  37. }                                         // destroy local autos
  38.  
  39. void func_b(int* arg_b)
  40. { int local_in_func_b = 4000;             // in stack
  41.   arg_b = new int[global];                // allocate free store
  42.   for (int i = 0; i < global; arg_b[i++] = i);
  43.   cout << "free_in_main contains integers " << arg_b[0]
  44.        << " to " << arg_b[global-1] << endl;
  45.   cout << local_in_func_b << endl;
  46. }                                         // destroy local auto
  47.