经典指数          
原因
1766
浏览数
0
收藏数
 

找程序的错和不足: int test(char *value,int value_len,int flag) {     char temp_buf[BUF_SIZE];     sprintf(temp_buf,value);     char temp_new_buf=new char[value_len];     if(flag)     {         strcat(temp_buf,"flag is true");         printf(temp_buf);         return 1;     }     delete[] temp_new_buf;     return 0; }

     举报   纠错  
 
切换
1 个答案

1 在3行前加assert(value!=NULL);也可以if(value ==NULL) 设置个全局变量判断程序出错

2 temp_buf没有初始化

3 sprintf()函数,个人觉得这里没什么用 直接改成strcpy 并且对于返回值没有做判断

4 5行 temp_new_buf  new错了

5 strcat的返回值做判断

6 flag!=0的时候 不能delete 

 
切换
撰写答案