
cmiN
By: a guest on Feb 9th, 2010 | syntax:
C++ | size: 0.96 KB | hits: 12 | expires: Never
#include <iostream>
#include <new>
using namespace std;
void f1()
{
// bad, must be used dynamic memory
unsigned int i, *x, j;
i = 0;
cout << "Enter numbers (until you enter 0).\n";
do
{
cin >> *(x + i);
i++;
}
while (*(x + i - 1) != 0);
cout << "The numbers: ";
for (j = 0; j < i - 1; j++)
{
cout << x[j] << " ";
}
cout << "\n";
}
void f2()
{
// good, dynamic memory used
unsigned int i, *x, j;
x = new(nothrow) unsigned int;
i = 0;
cout << "Enter numbers (until you enter 0).\n";
do
{
cin >> *(x + i);
i++;
}
while (*(x + i - 1) != 0);
cout << "The numbers: ";
for (j = 0; j < i - 1; j++)
{
cout << x[j] << " ";
}
cout << "\n";
}
int main()
{
cout << "Testing f2...\n";
f2();
cout << "Testing f1...\n";
f1();
system("pause >NUL");
return 0;
}