一个编码BUG

如下代码执行结果是什么?

objectivec 复制代码
#include <stdio.h>
#include <string.h>

struct test_info {
        int a;
        char *name;
};

static struct test_info hhh_info[2];
static char test_name[2][8];

int main()
{
        for (int i = 0; i < 2; i++) {
                memset(&hhh_info[i], 0, sizeof(hhh_info));
                snprintf(test_name[i], sizeof(test_name[i]), "good%d", i);
                hhh_info[i].name = test_name[i];
        }

        for (int j = 0; j < 2; j++) {
                printf("hhh_info name: %s\n", hhh_info[j].name);
                printf("test_name[j] is: %s\n", test_name[j]);
        }
        printf("test_name addr is: %p\n", test_name);
        printf("hhh_info addr is: %p\n", hhh_info);

        return 0;
}

gcc编译后执行结果如下:

bash 复制代码
hhh_info name:
test_name[j] is:
hhh_info name: good1
test_name[j] is: good1
test_name addr is: 0x564026243060
hhh_info addr is: 0x564026243040

会发现hhh_info第一个元素为空,奇怪?分析下看下

首先代码bug是由于memset误将整个数组的大小传入,导致第二次循环越界。

objectivec 复制代码
memset(&hhh_info[i], 0, sizeof(struct test_info));

但是为什么会导致第一个元素为空呢?从打印的地址可以看到,test_name和hhh_info相差32字节。struct test_info大小为16字节,hhh_info大小为32字节,所以test_name在内存布局上紧挨着hhh_info,因此,第二次循环时,会将整个test_name空间情况,2x8正好为16字节大小。

稍微调整下代码实现,看下输出结果:

objectivec 复制代码
#include <stdio.h>
#include <string.h>

struct test_info {
        int a;
        char *name;
};

static char test_name[2][8];
static struct test_info hhh_info[2];

int main()
{
        for (int i = 0; i < 2; i++) {
                memset(&hhh_info[i], 0, sizeof(hhh_info));
                snprintf(test_name[i], sizeof(test_name[i]), "good%d", i);
                hhh_info[i].name = test_name[i];
        }

        for (int j = 0; j < 2; j++) {
                printf("hhh_info name: %s\n", hhh_info[j].name);
                printf("test_name[j] is: %s\n", test_name[j]);
        }
        printf("test_name addr is: %p\n", test_name);
        printf("hhh_info addr is: %p\n", hhh_info);

        return 0;
}

gcc编译后执行结果如下:

bash 复制代码
hhh_info name: good0
test_name[j] is: good0
hhh_info name: good1
test_name[j] is: good1
test_name addr is: 0x5649a73c1040
hhh_info addr is: 0x5649a73c1060

低级编码bug可能会产生奇奇怪怪的问题。

相关推荐
兰令水19 小时前
topcode【随机算法题】【2026.5.17打卡-java版本】
java·算法·leetcode
吃好睡好便好19 小时前
在Matlab中绘制柱面图
开发语言·学习·算法·matlab
沐怡旸20 小时前
彻底告别解析崩溃:深入解析大模型 Structured Outputs(结构化输出)技术
算法
giszz20 小时前
量子算法简化解析:肖尔算法与格罗弗算法核心原理
算法·量子计算
熬夜敲代码的猫21 小时前
教你如何使用set和map
c++·算法
z200509301 天前
每日简单算法题——————跟着卡尔
算法
️是781 天前
信息奥赛一本通—编程启蒙(3395:练68.3 车牌问题)
数据结构·c++·算法
Liangwei Lin1 天前
LeetCode 118. 杨辉三角
算法·leetcode·职场和发展
计算机安禾1 天前
【c++面向对象编程】第24篇:类型转换运算符:自定义隐式转换与explicit
java·c++·算法
鼠鼠我(‘-ωก̀ )好困1 天前
leetGPU
算法