Zhonghui

每个不曾起舞的日子,都是对生命的辜负

User Tools

Site Tools


程序:cpp:内存分配

C++内存分配


分配连续的内存

// operator new[] example
#include <iostream>     // std::cout
#include <new>          // ::operator new[]
 
struct MyClass {
  int data;
  MyClass() {std::cout << '*';}  // print an asterisk for each construction
};
 
int main () {
  std::cout << "constructions (1): ";
  // allocates and constructs five objects:
  MyClass * p1 = new MyClass[5];
  std::cout << '\n';
 
  std::cout << "constructions (2): ";
  // allocates and constructs five objects (nothrow):
  MyClass * p2 = new (std::nothrow) MyClass[5];
  std::cout << '\n';
 
  delete[] p2;
  delete[] p1;
 
  return 0;
}
/var/www/DokuWikiStick/dokuwiki/data/pages/程序/cpp/内存分配.txt · Last modified: 2022/03/24 12:03 (external edit)