home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX0701.CPP
- // program demonstrate memory allocation
-
- // files in this example:
- // EX0701.CPP this file -- main() and supporting functions
-
- #include <iostream.h> // needed for stream output
- #include <string.h> // string manipulation functions
-
- void func_a(int& arg_a);
-
- void func_b(int* arg_b);
-
- int global = 1000; // in data area
- static int static_in_file = 1500; // in the data area
-
- void main()
- { cout << global << endl;
- cout << static_in_file << endl;
- int local_in_main = 2000; // in stack
- int* free_in_main; // in stack
- char* point_to_free; // in stack
- point_to_free = new char[13]; // allocate free store
- strcpy( point_to_free, "text in code"); // string copy to free
- func_a(local_in_main);
- func_b(free_in_main);
- delete [] point_to_free; // deallocate free store
- delete [] free_in_main; // deallocate free store
- } // destroy all objects
-
- void func_a(int& arg_a)
- { int local_in_func_a = 3000; // in stack
- static int static_in_func_a = 3500; // in data area
- cout << arg_a << endl;
- cout << local_in_func_a << endl;
- cout << static_in_func_a << endl;
- } // destroy local autos
-
- void func_b(int* arg_b)
- { int local_in_func_b = 4000; // in stack
- arg_b = new int[global]; // allocate free store
- for (int i = 0; i < global; arg_b[i++] = i);
- cout << "free_in_main contains integers " << arg_b[0]
- << " to " << arg_b[global-1] << endl;
- cout << local_in_func_b << endl;
- } // destroy local auto
-