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

实测 C11 malloc 分配释放、calloc 全零初始化、realloc 安全扩容及 memcpy/memmove 重叠拷贝

C本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。explore-memory-management.c
explore-memory-management.c81 行
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int id;
    int score;
} Record;

int main(void) {
    /* Case 1: malloc dynamic string allocation + free */
    char *desc = (char *)malloc(200 * sizeof(char));
    int c1_pass = 0;
    size_t len1 = 0;
    if (desc != NULL) {
        snprintf(desc, 200, "%s", "Zara Ali a DPS student in class 10th");
        len1 = strlen(desc);
        c1_pass = (strcmp(desc, "Zara Ali a DPS student in class 10th") == 0 && len1 == 36);
        free(desc);
        desc = NULL;
    }

    /* Case 2: calloc zero-initializes every allocated byte */
    int *zeros = (int *)calloc(8, sizeof(int));
    int all_zero = 1;
    if (zeros != NULL) {
        for (int i = 0; i < 8; i++) {
            if (zeros[i] != 0) all_zero = 0;
        }
        free(zeros);
        zeros = NULL;
    } else {
        all_zero = 0;
    }
    int c2_pass = (all_zero == 1);

    /* Case 3: realloc expands buffer while preserving prior content */
    char *buf = (char *)malloc(30 * sizeof(char));
    int c3_pass = 0;
    char final_text[100] = {0};
    if (buf != NULL) {
        snprintf(buf, 30, "%s", "Zara Ali a DPS student.");
        char *grown = (char *)realloc(buf, 100 * sizeof(char));
        if (grown != NULL) {
            buf = grown;
            strncat(buf, " She is in class 10th", 100 - strlen(buf) - 1);
            snprintf(final_text, sizeof(final_text), "%s", buf);
            c3_pass = (strcmp(final_text, "Zara Ali a DPS student. She is in class 10th") == 0);
        }
        free(buf);
        buf = NULL;
    }

    /* Case 4: pointer operators (&, *, ->), sizeof, memcpy, and overlapping memmove */
    Record rec = {7, 98};
    Record *prec = &rec;
    int via_arrow = prec->score;
    char non_overlap_dst[16] = {0};
    memcpy(non_overlap_dst, "C11-MEM", 8);
    char overlap[16] = "ABCDEF";
    memmove(overlap + 2, overlap, 4); /* overlap: "ABABCD" */
    int c4_pass = (sizeof(Record) == 2 * sizeof(int) && via_arrow == 98 &&
                   strcmp(non_overlap_dst, "C11-MEM") == 0 &&
                   strcmp(overlap, "ABABCD") == 0);

    printf("{\n");
    printf("  \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
    printf("  \"cases\": [\n");
    printf("    {\"name\": \"malloc_alloc_and_free\", \"expected\": 36, \"actual\": %zu, \"passed\": %s},\n",
           len1, c1_pass ? "true" : "false");
    printf("    {\"name\": \"calloc_zero_init\", \"expected\": true, \"actual\": %s, \"passed\": %s},\n",
           all_zero ? "true" : "false", c2_pass ? "true" : "false");
    printf("    {\"name\": \"realloc_grow_and_append\", \"expected\": \"Zara Ali a DPS student. She is in class 10th\", \"actual\": \"%s\", \"passed\": %s},\n",
           final_text, c3_pass ? "true" : "false");
    printf("    {\"name\": \"memcpy_memmove_and_arrow\", \"expected\": \"98|C11-MEM|ABABCD\", \"actual\": \"%d|%s|%s\", \"passed\": %s}\n",
           via_arrow, non_overlap_dst, overlap, c4_pass ? "true" : "false");
    printf("  ]\n");
    printf("}\n");
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。

入口文件:projects/c-memory-management-lab/explore-memory-management.c

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

下载课程关联工程包:C

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

下载课程关联工程包:C

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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