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

实测 C11 共用体内存大小、独占分时读写、覆盖写位模式与字节重叠视图

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

union Data {
    int i;
    float f;
    char str[20];
};

union ByteView {
    uint32_t u32;
    uint8_t bytes[4];
};

int main(void) {
    /* Case 1: union size equals largest member (with alignment) */
    union Data d_size;
    size_t sz = sizeof(d_size);
    int c1_pass = (sz == 20);

    /* Case 2: sequential exclusive member usage preserves exact values */
    union Data d_seq;
    d_seq.i = 10;
    int got_i = d_seq.i;
    d_seq.f = 220.5f;
    float got_f = d_seq.f;
    snprintf(d_seq.str, sizeof(d_seq.str), "%s", "C Programming");
    char got_str[20];
    snprintf(got_str, sizeof(got_str), "%s", d_seq.str);
    int c2_pass = (got_i == 10 && got_f == 220.5f && strcmp(got_str, "C Programming") == 0);

    /* Case 3: overwriting shared memory replaces earlier member bytes */
    union Data d_ov;
    d_ov.i = 10;
    d_ov.f = 220.5f;
    snprintf(d_ov.str, sizeof(d_ov.str), "%s", "C Programming");
    uint32_t first4_le = (uint32_t)(unsigned char)d_ov.str[0]
                       | ((uint32_t)(unsigned char)d_ov.str[1] << 8)
                       | ((uint32_t)(unsigned char)d_ov.str[2] << 16)
                       | ((uint32_t)(unsigned char)d_ov.str[3] << 24);
    int overlapped_i = d_ov.i; /* C11 6.5.2.3p3: inspects reinterpreted bytes */
    int c3_pass = ((uint32_t)overlapped_i == first4_le && overlapped_i != 10 && strcmp(d_ov.str, "C Programming") == 0);

    /* Case 4: shared byte representation of uint32_t and uint8_t[4] */
    union ByteView bv;
    bv.u32 = 0x12345678u;
    int same_addr = ((void *)&bv.u32 == (void *)&bv.bytes[0]);
    unsigned int sum_bytes = (unsigned int)bv.bytes[0] + bv.bytes[1] + bv.bytes[2] + bv.bytes[3];
    unsigned int exp_sum = 0x12u + 0x34u + 0x56u + 0x78u;
    int c4_pass = (same_addr && sizeof(bv) == 4 && sum_bytes == exp_sum);

    printf("{\n");
    printf("  \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
    printf("  \"cases\": [\n");
    printf("    {\"name\": \"union_memory_size\", \"expected\": 20, \"actual\": %zu, \"passed\": %s},\n",
           sz, c1_pass ? "true" : "false");
    printf("    {\"name\": \"exclusive_member_access\", \"expected\": \"10|220.5|C Programming\", \"actual\": \"%d|%.1f|%s\", \"passed\": %s},\n",
           got_i, got_f, got_str, c2_pass ? "true" : "false");
    printf("    {\"name\": \"shared_memory_overwrite\", \"expected\": %u, \"actual\": %u, \"passed\": %s},\n",
           first4_le, (uint32_t)overlapped_i, c3_pass ? "true" : "false");
    printf("    {\"name\": \"byte_overlay_and_address\", \"expected\": {\"same_addr\": true, \"size\": 4, \"byte_sum\": %u}, \"actual\": {\"same_addr\": %s, \"size\": %zu, \"byte_sum\": %u}, \"passed\": %s}\n",
           exp_sum, same_addr ? "true" : "false", sizeof(bv), sum_bytes, 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-unions-lab/explore-unions.c

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

下载课程关联工程包:C

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

下载课程关联工程包:C

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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