參考:就刪了個(gè)printf,代碼崩了!
1、運(yùn)行報(bào)錯(cuò)代碼
#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、添加打印日志,段錯(cuò)誤消失
#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、原因:
未添加打印日志時(shí),index的值是func1中殘留的值
4、擴(kuò)展問題:
1)printf中沒加'\n'時(shí),必定崩潰,func2函數(shù)中index一定是0x01010101;
2)在func1函數(shù)中加printf("Hello world\n");必定崩潰,func2函數(shù)中index一定是0x01010101;
3)當(dāng)func1函數(shù)中arr數(shù)組大小是10時(shí),index才必定是0x01010101;當(dāng)為其他值時(shí),index也是隨機(jī)值;文章來源:http://www.zghlxwxcb.cn/news/detail-766914.html
4)index是0x01010101必定崩潰,是其他隨機(jī)值時(shí),怎么不崩潰?文章來源地址http://www.zghlxwxcb.cn/news/detail-766914.html
到了這里,關(guān)于添加調(diào)試日志,bug消失的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!