0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Find bug test

Last updated at Posted at 2013-06-08
#include <stdlib.h>
#include <stdio.h>

//Example #1 (C code): What is the bug and why is it a problem? 
/* 配列a[10]の宣言位置に問題がある。繰り返しの値を受けるためにはbuggy内で宣言しなければならない
int a[10];         //a配列宣言	
void buggy() {     //関数の宣言
	int i;         //i変数宣言
	for(i = 0; i < 10; i++);{  //iが10未満の時1ずつ足していく
    a[i] = 0;   //0をiに入れていく。???**
  }
}

int main(void){
	int i;
	int a[10];	
	for( i = 0; i < 10 ; i++){
		a[i] = i;
	printf("iの値は%2dです ",i);
	printf("aの値は%2dです\n",a[i]);
	}
	return 0;
}
*/

//Example #2 (C code): What is the bug and why is it a problem?
//l_lengthのwhileを抜ける条件が正しく定義されていない。
/*

int main(void){
int my_crazy_calc(int area, int length, int width) {
  int l_length = length;
  while (l_length) {
    area += length * width;
    length--;
  }
	return area;
	}
return 0;
}
int main(void){
	int my_crazy_cal(
	int area = 1;
	int length = 2;
	int width = 3;
	int l_length = length;
		while(l_length){area += length * width;
			length--;
		
		//return area;
		}
	printf("length is %d\n", length);
	printf("l_length is %d\n", l_length);
 return 0;	
}
*/

//Example #6 (C code): What is the bug and why is it a problem?
// バグではないが#include<string.h> が必ず必要
// 最初の”が全角。"を使用しなければいけない。
// 0を返すのではなく'\0' strncpy関数は文字列の最後にNullを付け加える必要がある。
/*
		char * PrintMe() {
  char str[12];
  strncpy (str, ”hello world", 11);
  str[11] = 0;
  return str;
	}
*/
		
//Example #7 (C code): What is the bug and why is it a problem?
//

struct my_struct { int val; };
int my_func( struct my_struct *ptr, int x, int y)  {
  int z = 0; 
  if((ptr != NULL) && (x < y) || (ptr->val > 0)) {
    z = x + y - ptr->val;
  }
  return z; 
} 


//Example #9 (C code): What is the bug and why is it a problem?
//i=10となったとき、配列の領域(11番目)が確保できないためエラーとなる
/*配列の要素数を増やすor whileの条件をi < 9にする等
int main(void){
void foo(int x[10]) {
  int i = 0;
  do { 
    x[i] = i++; 
  } while( i < 10 );
}
*/
//Example #10 (C code): What is the bug and why is it a problem?
/*
int foo(int in) {
  int *buf1 = (int *)malloc(1000*sizeof(int));
  if(!buf1) return -1;
  int *buf2 = (int *)malloc(2000*sizeof(int));
  if(!buf2) return -1;

  int retVal = process(in,buf1,buf2);

  free(buf1);
  free(buf2);

  return retVal;
}

*/
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?