Share Pastebin
Guest
Public paste!

cmiN

By: a guest | Feb 9th, 2010 | Syntax: C++ | Size: 0.96 KB | Hits: 10 | Expires: Never
Copy text to clipboard
  1. #include <iostream>
  2. #include <new>
  3. using namespace std;
  4.  
  5. void f1()
  6. {
  7.     // bad, must be used dynamic memory
  8.     unsigned int i, *x, j;
  9.     i = 0;
  10.     cout << "Enter numbers (until you enter 0).\n";
  11.     do
  12.     {
  13.         cin >> *(x + i);
  14.         i++;
  15.     }
  16.     while (*(x + i - 1) != 0);
  17.     cout << "The numbers: ";
  18.     for (j = 0; j < i - 1; j++)
  19.     {
  20.         cout << x[j] << " ";
  21.     }
  22.     cout << "\n";
  23. }
  24.  
  25. void f2()
  26. {
  27.     // good, dynamic memory used
  28.     unsigned int i, *x, j;
  29.     x = new(nothrow) unsigned int;
  30.     i = 0;
  31.     cout << "Enter numbers (until you enter 0).\n";
  32.     do
  33.     {
  34.         cin >> *(x + i);
  35.         i++;
  36.     }
  37.     while (*(x + i - 1) != 0);
  38.     cout << "The numbers: ";
  39.     for (j = 0; j < i - 1; j++)
  40.     {
  41.         cout << x[j] << " ";
  42.     }
  43.     cout << "\n";
  44. }
  45.  
  46. int main()
  47. {
  48.     cout << "Testing f2...\n";
  49.     f2();
  50.     cout << "Testing f1...\n";
  51.     f1();
  52.     system("pause >NUL");
  53.     return 0;
  54. }