70,020
社区成员




#include <stdlib.h>
#define step(i) (1 << i)
int con_x(int i)
{
return rand() % (i + 1);
}
int con_y(int i)
{
return rand() % (i + 2);
}
int main()
{
int init_mask = 0;
int status = 0;
int* ptr1 = 0;
int* ptr2 = 0;
float* ptr3 = 0;
float* ptr4 = 0;
char* ptr5 = 0;
char* ptr6 = 0;
if (con_x(1))
{
ptr1 = (int*)malloc(sizeof(int));
init_mask |= step(1);
}
else
{
status = 1;
goto cleanup;
}
if (con_y(2))
{
ptr1 = (int*)malloc(sizeof(int));
init_mask |= step(2);
}
else
{
status = 1;
goto cleanup;
}
// ...
cleanup:
if (init_mask & step(1))
{
free(ptr1);
ptr1 = 0;
}
if (init_mask & step(2))
{
free(ptr2);
ptr1 = 0;
}
// ...
return status;
}
...
for(int i = 0; i < 1; ++i)
{
if (con_x(1))
{
ptr1 = (int*)malloc(sizeof(int));
init_mask |= step(1);
}
else
{
status = 1;
break;
}
if (con_y(2))
{
ptr1 = (int*)malloc(sizeof(int));
init_mask |= step(2);
}
else
{
status = 1;
break;
}
...
}
...
....
float* ptr4 = 0;
char* ptr5 = 0;
char* ptr6 = 0;
typedef int (*con_f)(int);
con_f conlist[] = { con_x, con_y /*, ... */ };
for(int i = 0; i < sizeof(conlist) / sizeof(conlist[0]); ++i)
{
int x = i + 1;
if(conlist[i](x))
{
ptr1 = (int*)malloc(sizeof(int));
init_mask |= step(x);
}
else
{
status = 1;
break;
}
}
if (init_mask & step(1))
{
free(ptr1);
ptr1 = 0;
}
.....
if (con_x(1) || con_y(2))
{
ptr1 = (int*)malloc(sizeof(int));
if(con_x(1))
init_mask |= step(1);
else if(con_x(2))
init_mask |= step(2);
}
else
{
status = 1;
}
//如果下面的注释没有语句的话这样改是可以的。
// ...
//cleanup:
if (init_mask & step(1))
{
free(ptr1);
ptr1 = 0;
}
if (init_mask & step(2))
{
free(ptr2);
ptr1 = 0;
}
// ...
switch(a)
{
case 1:
{
//do something
a = 2;
}
break;
case 2:
{
//do something
//finish doing
return 1;//从这里返回这个switch
}
break;
case 3:
{
//do something
a = 2;
}
break;
}