#include <cstddef>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <new>
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
namespace {
class MyClass {
public:
static int constructed;
static int destroyed;
int value;
explicit MyClass(int v = 0) : value(v) { ++constructed; }
~MyClass() { ++destroyed; }
};
int MyClass::constructed = 0;
int MyClass::destroyed = 0;
} // namespace
int main() {
std::vector<lab::Case> cases;
// Case 1: Single object new / delete
MyClass* obj = new MyClass;
obj->value = 10;
bool mid = (obj->value == 10 && MyClass::constructed == 1 && MyClass::destroyed == 0);
delete obj;
bool c1 = mid && (MyClass::destroyed == 1);
cases.push_back({"single_object_new_delete", lab::jbool(true), lab::jbool(c1), c1});
// Case 2: Dynamic array new[] / delete[]
int* arr = new int[10];
int sum = 0;
for (int i = 0; i < 10; ++i) {
arr[i] = i * 2;
sum += arr[i];
}
bool c2 = (arr[0] == 0 && arr[9] == 18 && sum == 90);
delete[] arr;
cases.push_back({"dynamic_array_new_delete", lab::jbool(true), lab::jbool(c2), c2});
// Case 3: std::nothrow returns nullptr and default new throws std::bad_alloc
volatile std::size_t impossible_size = std::numeric_limits<std::size_t>::max() / 2;
char* p_nothrow = new (std::nothrow) char[static_cast<std::size_t>(impossible_size)];
bool nothrow_null = (p_nothrow == nullptr);
if (p_nothrow) delete[] p_nothrow;
bool caught_bad_alloc = false;
try {
char* p_throw = new char[static_cast<std::size_t>(impossible_size)];
delete[] p_throw;
} catch (const std::bad_alloc& e) {
caught_bad_alloc = (std::strlen(e.what()) > 0);
}
bool c3 = nothrow_null && caught_bad_alloc;
cases.push_back({"nothrow_allocation_and_bad_alloc", lab::jbool(true), lab::jbool(c3), c3});
// Case 4: Placement new on aligned stack buffer + explicit destructor call
alignas(MyClass) unsigned char storage[sizeof(MyClass)];
MyClass* placed = new (storage) MyClass(77);
bool placed_ok = (placed->value == 77) && (reinterpret_cast<unsigned char*>(placed) == storage);
int before_d = MyClass::destroyed;
placed->~MyClass();
bool c4 = placed_ok && (MyClass::destroyed == before_d + 1);
cases.push_back({"placement_new_on_aligned_buffer", lab::jbool(true), lab::jbool(c4), c4});
lab::emit(cases);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-libs-new-lab/explore-cpp-libs-new.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-new.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2360.zip