一个编码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可能会产生奇奇怪怪的问题。

相关推荐
承渊政道12 小时前
【优选算法】(实战:栈、队列、优先级队列高频考题通关全解)
数据结构·c++·笔记·学习·算法·leetcode·宽度优先
py有趣12 小时前
力扣热门100题之将有序数组转为二叉搜索树
算法·leetcode
天若有情67312 小时前
Python精神折磨系列(完整11集·无断层版)
数据库·python·算法
凌波粒12 小时前
LeetCode--383.赎金信(哈希表)
java·算法·leetcode·散列表
xiaoye-duck13 小时前
《算法题讲解指南:动态规划算法--子数组系列》--23.等差数列划分,24.最长湍流子数组
c++·算法·动态规划
小O的算法实验室13 小时前
2026年SEVC,高密度仓库中结合任务分配的多AGV无冲突调度框架,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
abant213 小时前
leetcode 108 有序数组转平衡二叉树
算法·leetcode·职场和发展
汀、人工智能13 小时前
[特殊字符] 第7课:移动零
数据结构·算法·数据库架构·图论·bfs·移动零
计算机安禾13 小时前
【数据结构与算法】第25篇:静态查找(一):顺序查找与折半查找
java·开发语言·数据结构·学习·算法·visual studio code·visual studio
B1acktion13 小时前
2.7.希尔排序——让插入排序先大步走,再小步收尾
c++·算法·排序算法