use_new.cpp 902 B

123456789101112131415161718192021222324252627
  1. // use_new.cpp -- using the new operator
  2. #include <iostream>
  3. int main()
  4. {
  5. using namespace std;
  6. int nights = 1001;
  7. int * pt = new int; // allocate space for an int
  8. *pt = 1001; // store a value there
  9. cout << "nights value = ";
  10. cout << nights << ": location " << &nights << endl;
  11. cout << "int ";
  12. cout << "value = " << *pt << ": location = " << pt << endl;
  13. double * pd = new double; // allocate space for a double
  14. *pd = 10000001.0; // store a double there
  15. cout << "double ";
  16. cout << "value = " << *pd << ": location = " << pd << endl;
  17. cout << "location of pointer pd: " << &pd << endl;
  18. cout << "size of pt = " << sizeof(pt);
  19. cout << ": size of *pt = " << sizeof(*pt) << endl;
  20. cout << "size of pd = " << sizeof pd;
  21. cout << ": size of *pd = " << sizeof(*pd) << endl;
  22. // cin.get();
  23. return 0;
  24. }