蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

验证标量 new/delete、1D/2D/3D 动态数组、Box[4] 构造/析构对称性及 unique_ptr/shared_ptr

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-dynamic-memory.cpp
explore-cpp-dynamic-memory.cpp127 行
#include <iostream>
#include <memory>
#include <new>
#include <string>

struct TrackedBox {
    static int constructed;
    static int destroyed;
    int id = 0;

    TrackedBox() { ++constructed; }
    explicit TrackedBox(int v) : id(v) { ++constructed; }
    ~TrackedBox() { ++destroyed; }
};

int TrackedBox::constructed = 0;
int TrackedBox::destroyed = 0;

int main() {
    // 1. Scalar new/delete and nothrow new
    double* pvalue = new (std::nothrow) double(29494.99);
    int scaled = 0;
    if (pvalue != nullptr) {
        scaled = static_cast<int>((*pvalue) * 100.0 + 0.5);
        delete pvalue;
        pvalue = nullptr;
    }
    bool ok1 = (scaled == 2949499) && (pvalue == nullptr);

    // 2. Dynamic 1D, 2D (4x8), and 3D (2x3x4) arrays with matching delete[]
    int* arr1d = new int[5];
    int sum1d = 0;
    for (int i = 0; i < 5; ++i) {
        arr1d[i] = (i + 1) * 10;
        sum1d += arr1d[i];
    }
    delete[] arr1d;

    int** arr2d = new int*[4];
    for (int i = 0; i < 4; ++i) arr2d[i] = new int[8];
    int sum2d = 0;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 8; ++j) {
            arr2d[i][j] = i * j;
            sum2d += arr2d[i][j];
        }
    }
    for (int i = 0; i < 4; ++i) delete[] arr2d[i];
    delete[] arr2d;

    int*** arr3d = new int**[2];
    for (int i = 0; i < 2; ++i) {
        arr3d[i] = new int*[3];
        for (int j = 0; j < 3; ++j) arr3d[i][j] = new int[4];
    }
    int sum3d = 0;
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 4; ++k) {
                arr3d[i][j][k] = i + j + k;
                sum3d += arr3d[i][j][k];
            }
        }
    }
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) delete[] arr3d[i][j];
        delete[] arr3d[i];
    }
    delete[] arr3d;

    std::string act2 = "s1=" + std::to_string(sum1d) + ",s2=" + std::to_string(sum2d) +
                       ",s3=" + std::to_string(sum3d);
    const std::string exp2 = "s1=150,s2=168,s3=72";
    bool ok2 = (act2 == exp2);

    // 3. Dynamic object array new Box[4] / delete[] calls 4 ctors & 4 dtors
    TrackedBox::constructed = 0;
    TrackedBox::destroyed = 0;
    TrackedBox* boxes = new TrackedBox[4];
    int mid_ctor = TrackedBox::constructed;
    int mid_dtor = TrackedBox::destroyed;
    delete[] boxes;
    std::string act3 = std::to_string(mid_ctor) + "/" + std::to_string(mid_dtor) + "->" +
                       std::to_string(TrackedBox::constructed) + "/" + std::to_string(TrackedBox::destroyed);
    const std::string exp3 = "4/0->4/4";
    bool ok3 = (act3 == exp3);

    // 4. Modern C++17 smart pointers: unique_ptr move & shared_ptr reference counting
    TrackedBox::constructed = 0;
    TrackedBox::destroyed = 0;
    long uc1 = 0, uc2 = 0, uc3 = 0;
    bool moved_empty = false;
    {
        auto u1 = std::make_unique<TrackedBox>(77);
        auto u2 = std::move(u1);
        moved_empty = (u1 == nullptr) && (u2 != nullptr) && (u2->id == 77);

        auto sp1 = std::make_shared<TrackedBox>(88);
        uc1 = sp1.use_count();
        {
            auto sp2 = sp1;
            uc2 = sp1.use_count();
        }
        uc3 = sp1.use_count();
    }
    std::string act4 = "mv=" + std::string(moved_empty ? "1" : "0") +
                       ",uc=" + std::to_string(uc1) + "-" + std::to_string(uc2) + "-" + std::to_string(uc3) +
                       ",cd=" + std::to_string(TrackedBox::constructed) + "/" + std::to_string(TrackedBox::destroyed);
    const std::string exp4 = "mv=1,uc=1-2-1,cd=2/2";
    bool ok4 = (act4 == exp4);

    std::cout << "{\n"
              << "  \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
              << "  \"cases\": [\n"
              << "    {\"name\": \"scalar_new_delete_and_nothrow\", \"expected\": true, \"actual\": "
              << (ok1 ? "true" : "false") << ", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
              << "    {\"name\": \"dynamic_1d_2d_3d_arrays\", \"expected\": \"" << exp2
              << "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
              << "    {\"name\": \"object_array_ctor_dtor_symmetry\", \"expected\": \"" << exp3
              << "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
              << "    {\"name\": \"smart_pointers_unique_and_shared\", \"expected\": \"" << exp4
              << "\", \"actual\": \"" << act4 << "\", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
              << "  ]\n"
              << "}\n";
    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-dynamic-memory-lab/explore-cpp-dynamic-memory.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-dynamic-memory.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2320.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习