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

相关推荐
鸽芷咕9 小时前
【Python报错已解决】xlrd.biffh.XLRDError: Excel xlsx file; not supported
开发语言·python·机器学习·bug·excel
鸽芷咕16 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘tensorflow‘
python·机器学习·tensorflow·bug·neo4j
鸽芷咕1 天前
【Python报错已解决】python setup.py bdist_wheel did not run successfully.
开发语言·python·机器学习·bug
Tatakai251 天前
Mybatis Plus分页查询返回total为0问题
java·spring·bug·mybatis
鸽芷咕2 天前
【Python报错已解决】libpng warning: iccp: known incorrect sRGB profile
开发语言·python·机器学习·bug
鸽芷咕2 天前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
腾飞的信仰4 天前
如何快速解决程序中的BUG
bug
Everglowwwwww4 天前
【bug】通过lora方式微调sdxl inpainting踩坑
学习·计算机视觉·ai作画·stable diffusion·bug
数据最前线4 天前
创建索引遇到这个Bug,19c中还没有修复
oracle·bug
CV金科4 天前
蓝桥杯-STM32G431RBT6(UART解析字符串sscanf和解决串口BUG)
c语言·stm32·单片机·嵌入式硬件·mcu·算法·bug