
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ New Library - operator new
Description
It allocates size bytes of storage, suitably aligned to represent any object of that size, and returns a non-null pointer to the first byte of this block.
Declaration
Following is the declaration for operator new.
void* operator new (std::size_t size) throw (std::bad_alloc); (throwing allocation) void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw(); (nothrow allocation) void* operator new (std::size_t size, void* ptr) throw(); (placement)
Parameters
size − It contain size in bytes of the requested memory block.
nothrow_value − It contains the constant nothrow.
ptr − It is a pointer to an already-allocated memory block of the proper size.
Return Value
It returns a pointer to the newly allocated storage space.
Exceptions
If it fails to allocate storage then it throws bad_alloc.
Data races
It modifies the storage referenced by the returned value.
Example
In below example explains about new operator.
#include <iostream> #include <new> struct MyClass { int data[100]; MyClass() {std::cout << "It constructed [" << this << "]\n";} }; int main () { std::cout << "1: "; MyClass * p1 = new MyClass; std::cout << "2: "; MyClass * p2 = new (std::nothrow) MyClass; std::cout << "3: "; new (p2) MyClass; std::cout << "4: "; MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass)); delete p1; delete p2; delete p3; return 0; }
Let us compile and run the above program, this will produce the following result −
1: It constructed [0x21f9ba0] 2: It constructed [0x21f9d40] 3: It constructed [0x21f9d40]
new.htm
Advertisements