C++/Language/New
Версия от 14:21, 25 мая 2010; (обсуждение)
Содержание
- 1 Demonstrate the new(nothrow) alternative.
- 2 Dynamic int array
- 3 Force an allocation failure.
- 4 Handle exceptions thrown by new.
- 5 Pointer for double and use new to allocate memory
- 6 Use new to allocate memory for a class and check error
- 7 Use new to allocate memory for primitive type
- 8 Watch for allocation errors using both old-style and new-style error handling.
Demonstrate the new(nothrow) alternative.
#include <iostream>
#include <new>
using namespace std;
int main()
{
double *p;
do { // this will eventually run out of memory
p = new(nothrow) double[100000];
if(p)
cout << "Allocation OK.\n";
else
cout << "Allocation Error.\n";
} while(p);
return 0;
}
Dynamic int array
#include <iostream>
#include <cstdlib>
using namespace std;
class DynamicIntArray {
int *p;
int size;
public:
DynamicIntArray(int s);
DynamicIntArray &operator=(DynamicIntArray &ob);
int &operator[](int i);
};
DynamicIntArray::DynamicIntArray(int s)
{
p = new int [s];
if(!p) {
cout << "Allocation error\n";
exit(1);
}
size = s;
}
// Overload =
DynamicIntArray &DynamicIntArray::operator=(DynamicIntArray &ob)
{
int i;
if(size!=ob.size) {
cout << "Cannot copy arrays of differing sizes!\n";
exit(1);
}
for(i = 0; i <size; i++) p[ i ] = ob.p[ i ];
return *this;
}
// Overload []
int &DynamicIntArray::operator[](int i)
{
if(i <0 || i>size) {
cout << "\nIndex value of ";
cout << i << " is out-of-bounds.\n";
exit(1);
}
return p[ i ];
}
int main()
{
int i;
DynamicIntArray object1(10), object2(10), object3(100);
object1[3] = 10;
i = object1[3];
cout << i << endl;
object2 = object1;
i = object2[3];
cout << i << endl;
// generates an error
object1 = object3; // arrays differ sizes
return 0;
}
Force an allocation failure.
#include <iostream>
#include <new>
using namespace std;
int main()
{
double *p;
// this will eventually run out of memory
do {
try {
p = new double[100000];
} catch (bad_alloc xa) {
cout << "Allocation failure.\n";
return 1;
}
cout << "Allocation OK.\n";
} while(p);
return 0;
}
Handle exceptions thrown by new.
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p, i;
try {
p = new int[32]; // allocate memory for 32-element int array
} catch (bad_alloc xa) {
cout << "Allocation failure.\n";
return 1;
}
for(i = 0; i <32; i++)
p[i] = i;
for(i = 0; i <32; i++)
cout << p[i] << " ";
delete [] p; // free the memory
return 0;
}
Pointer for double and use new to allocate memory
#include <iostream>
using namespace std;
int main()
{
double *p;
p = new double (-123.0987);
cout << *p << "\n";
return 0;
}
Use new to allocate memory for a class and check error
#include <iostream>
#include <cstring>
using namespace std;
class phone {
char name[40];
char number[14];
public:
void store(char *n, char *num);
void show();
};
void phone::store(char *n, char *num)
{
strcpy(name, n);
strcpy(number, num);
}
void phone::show()
{
cout << name << ": " << number;
cout << endl;
}
int main()
{
phone *p;
p = new phone;
if(!p) {
cout << "Allocation error.";
return 1;
}
p->store("Joe", "999 555-4444");
p->show();
delete p;
return 0;
}
Use new to allocate memory for primitive type
#include <iostream>
using namespace std;
int main()
{
float *f;
long *l;
char *c;
f = new float;
l = new long;
c = new char;
if(!f || !l || !c) {
cout << "Allocation error.";
return 1;
}
*f = 10.102;
*l = 100000;
*c = "A";
cout << *f << " " << *l << " " << *c;
cout << "\n";
delete f; delete l; delete c;
return 0;
}
Watch for allocation errors using both old-style and new-style error handling.
#include <iostream>
#include <new>
using namespace std;
class MyClass {
static int count;
public:
MyClass() {
count++;
}
~MyClass() {
count--;
}
int getcount() {
return count;
}
};
int MyClass::count = 0;
int main()
{
MyClass object1, object2, object3;
cout << object1.getcount() << " objects in existence\n";
MyClass *p;
try {
p = new MyClass;
if(!p) {
cout << "Allocation error\n";
return 1;
}
} catch(bad_alloc ba) {
cout << "Allocation error\n";
return 1;
}
cout << object1.getcount();
cout << " objects in existence after allocation\n";
delete p;
cout << object1.getcount();
cout << " objects in existence after deletion\n";
return 0;
}