添加调试日志,bug消失

参考:就删了个printf,代码崩了!

1、运行报错代码
复制代码
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void func1()
{
    int arr[10];
    memset(arr, 1, sizeof(arr));
}

void func2()
{
    int index;
    int* arr = (int*) malloc(5);
    arr[index] = 55;
}

int main()
{
    func1();
    func2();

    return 0;
}
2、添加打印日志,段错误消失
复制代码
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void func1()
{
    int arr[10];
    memset(arr, 1, sizeof(arr));
}

void func2()
{
    int index;
    int* arr = (int*) malloc(5);
    arr[index] = 55;
}

void printLog()
{
    printf("Hello world\n");
}

int main()
{
    func1();
    printLog();
    func2();

    return 0;
}
3、原因:

未添加打印日志时,index的值是func1中残留的值

4、扩展问题:

1)printf中没加'\n'时,必定崩溃,func2函数中index一定是0x01010101;

2)在func1函数中加printf("Hello world\n");必定崩溃,func2函数中index一定是0x01010101;

3)当func1函数中arr数组大小是10时,index才必定是0x01010101;当为其他值时,index也是随机值;

4)index是0x01010101必定崩溃,是其他随机值时,怎么不崩溃?

相关推荐
淘矿人1 天前
Claude辅助算法设计与优化
人工智能·python·算法·microsoft·github·bug·pygame
朱穆朗1 天前
Cmder创建npm等项目中,使用CLI的BUG
前端·npm·bug
AI 编程助手GPT2 天前
【实战】Codex 接管电脑 + Claude Routines 云端值守:一次 Bug 排查的“无人化”闭环
人工智能·gpt·ai·chatgpt·bug
minxihou2 天前
iwlwifi/iwlmld missed beacons 与 802.11ax HE beacon timing 固件 Bug
bug·协议
阿部多瑞 ABU5 天前
《智能学号抽取系统》V5.9.5 发布:精简代码,修复移动端文件读取核心 Bug
vue·html·bug
qq_452396235 天前
【工程实战】第八篇:报告美学 —— Allure 深度定制:让 Bug 定位精准到秒
开发语言·python·bug
tkokof16 天前
捉虫(Bug)小记
人工智能·深度学习·bug·游戏开发
南宫萧幕6 天前
基于上一篇文章VMware+openweb UI+ollama+docker的bug问题总结
docker·容器·bug·openweb ui