C++/Language/New
Содержание
- 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.
<source lang="cpp">
- 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;
}
</source>
Dynamic int array
<source lang="cpp">
- 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;
}
</source>
Force an allocation failure.
<source lang="cpp">
- 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;
}
</source>
Handle exceptions thrown by new.
<source lang="cpp">
- 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;
}
</source>
Pointer for double and use new to allocate memory
<source lang="cpp">
- include <iostream>
using namespace std; int main() {
double *p; p = new double (-123.0987); cout << *p << "\n"; return 0;
}
</source>
Use new to allocate memory for a class and check error
<source lang="cpp">
- 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;
}
</source>
Use new to allocate memory for primitive type
<source lang="cpp">
- 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;
}
</source>
Watch for allocation errors using both old-style and new-style error handling.
<source lang="cpp">
- 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;
}
</source>