添加调试日志,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必定崩溃,是其他随机值时,怎么不崩溃?

相关推荐
Swift社区13 小时前
RN 项目中“页面存在 ≠ 页面可见”会导致哪些隐藏 Bug?
react native·bug·react
tongyue14 小时前
天问esp32驱动DHT11官方程序BUG修复
bug
laocooon5238578862 天前
写代码 调bug相关信息
bug
鸿儒5172 天前
记录一个C++操作8位影像的一个bug
开发语言·c++·bug
·云扬·2 天前
【Bug】 Elasticsearch启动失败(exit code 78):2个bootstrap检查问题排查与解决
elasticsearch·bootstrap·bug
BrianGriffin3 天前
get_download_file_path: command not found (asdf bug已解决)
bug
咸虾米_3 天前
解决九两酒商城[uni-pay-co]: Error:执行失败,积 分需要大于等于1的bug
bug·unicloud云开发·微信小程序商城·uniapp项目
凯子坚持 c3 天前
Git分支实战指南:如何优雅地管理版本、修复Bug并解决合并冲突
git·bug
北冥有渔jy3 天前
BT6.0常见的BUG
网络·安全·bug·蓝牙
测试19983 天前
一个只能通过压测发现Bug
自动化测试·软件测试·python·selenium·测试工具·bug·压力测试