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

实测 <cstdlib> malloc/realloc/free、atoi/strtol、srand/rand/qsort/bsearch 与 exit/system 探针

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-cstdlib.cpp
explore-cpp-libs-cstdlib.cpp132 行
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#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 {

int cmp_int(const void* a, const void* b) {
    int va = *static_cast<const int*>(a);
    int vb = *static_cast<const int*>(b);
    return (va > vb) - (va < vb);
}

void noop_exit_handler() {}

} // namespace

int main() {
    std::vector<lab::Case> cases;

    // Case 1: malloc / realloc / free dynamic buffer management
    int* ptr = static_cast<int*>(std::malloc(10 * sizeof(int)));
    bool c1 = false;
    if (ptr != nullptr) {
        int sum = 0;
        for (int i = 0; i < 10; ++i) {
            ptr[i] = i * i;
            sum += ptr[i];
        }
        int* grown = static_cast<int*>(std::realloc(ptr, 12 * sizeof(int)));
        if (grown != nullptr) {
            ptr = grown;
            ptr[10] = 100;
            ptr[11] = 121;
            c1 = (sum == 285) && (ptr[9] == 81) && (ptr[11] == 121);
        }
        std::free(ptr);
    }
    cases.push_back({"malloc_calloc_realloc_free", lab::jbool(true), lab::jbool(c1), c1});

    // Case 2: atoi / atof vs safer strtol / strtod with endptr validation
    int n1 = std::atoi("123");
    double d1 = std::atof("456.78");
    char* end_i = nullptr;
    long n2 = std::strtol("255abc", &end_i, 10);
    char* end_d = nullptr;
    double d2 = std::strtod("3.5xyz", &end_d);
    bool c2 = (n1 == 123) && (std::fabs(d1 - 456.78) < 1e-9) &&
              (n2 == 255 && end_i != nullptr && *end_i == 'a') &&
              (std::fabs(d2 - 3.5) < 1e-9 && end_d != nullptr && *end_d == 'x');
    cases.push_back({"atoi_atof_and_strtol_strtod", lab::jbool(true), lab::jbool(c2), c2});

    // Case 3: srand / rand reproducibility + qsort / bsearch + abs / div
    std::srand(12345u);
    std::vector<int> seq1(5);
    for (int& v : seq1) v = std::rand() % 100;
    std::srand(12345u);
    std::vector<int> seq2(5);
    for (int& v : seq2) v = std::rand() % 100;

    int arr[5] = {42, 7, 19, 3, 25};
    std::qsort(arr, 5, sizeof(int), cmp_int);
    int key = 19;
    int* found = static_cast<int*>(std::bsearch(&key, arr, 5, sizeof(int), cmp_int));
    std::div_t qr = std::div(23, 5);
    bool c3 = (seq1 == seq2) && (arr[0] == 3 && arr[4] == 42) &&
              (found != nullptr && *found == 19) &&
              (std::abs(-42) == 42) && (qr.quot == 4 && qr.rem == 3);
    cases.push_back({"srand_rand_and_qsort_bsearch", lab::jbool(true), lab::jbool(c3), c3});

    // Case 4: EXIT_SUCCESS / EXIT_FAILURE, atexit registration, system(nullptr), getenv
    int reg_rc = std::atexit(noop_exit_handler);
    int shell_avail = std::system(nullptr);
    const char* path_env = std::getenv("PATH");
    bool c4 = (EXIT_SUCCESS == 0) && (EXIT_FAILURE != 0) &&
              (reg_rc == 0) && (shell_avail != 0) && (path_env != nullptr);
    cases.push_back({"exit_constants_and_system_probe", 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-cstdlib-lab/explore-cpp-libs-cstdlib.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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