那些年写过的bug[0]
文章目录
在此记录一下自己写过的那些bug owo。
1)指向数组的指针在malloc前忘记初始化(赋值为NULL)
2)
some_struct * ptr = A;
array[1] = *A;然后就忘了释放ptr...
3)
char * buffer = NULL;
size_t sz = 0;
while(getline(&buffer, &sz, file) > 0){
...
}然后忘记释放buffer...
4)
假设line是一个字符串(比如"apple")。
line[2] = '\0';然后
strchr(line, 'e');就返回NULL了。因为现在的line是"ap";
5)
int* array = malloc(5 * sizeof(*array));
for (int i = 0; i < 5; i++){
free(array + i);
}重复释放内存了。
6)
size_t idx = 0;
unsigned num = n;
int j = -1;
while(num < 5){
j++;
if((j >= idx) && (j <= idx + n - 1)){
continue;
}
ans.cards[num] = hand->cards[j];
num++;
}但是当n = 0时, idx + n - 1是一个极大的正数,因此循环会重复许多许多次。可以改为:
if((j >= idx) && (j < idx + n))
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论