65,208
社区成员
发帖
与我相关
我的任务
分享
//c++版
#include<string>
#include<iostream>
#include<exception>
using namespace std;
int main()
{
try{
string p1("this is p1");
string p2("this is p2");
string p3("this is p3");
cout << p1 << endl;
cout << p2 << endl;
cout << p3 << endl;
}
catch(...){
return -1;
}
return 0;
}
//c版
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *p1;
char *p2;
char *p3;
int result=-1;
do{
p1=malloc(0x10 * sizeof(char));
if(NULL == p1)break;
strcpy(p1,"this is p1");
p2=malloc(0x10 * sizeof(char));
if(NULL == p2)break;
strcpy(p2,"this is p2");
p3=malloc(0x10 * sizeof(char));
if(NULL ==p3)break;
strcpy(p3,"this is p3");
printf("%s\n%s\n%s\n",p1,p2,p3);
result=0;
}while(0);
if(NULL != p1)free(p1);
if(NULL != p2)free(p2);
if(NULL != p3)free(p3);
return result;
}
int a()
{
int ret = -1;
O *o1 = open_xxx();
if (!xxx_read(o1)) goto err_o1;
O *o2 = open_xxx();
if (!xxx_write(o1)) goto err_o2;
.....
ret = 0;
......
:err_o3
close_xxx(o2);
:err_o2
close_xxx(o1);
:err_o1
return ret;
}
int a()
{
int ret = 0;
O *o1 = open_xxx();
if (!xxx_read(o1)) {
close_xxx(o1);
return -1;
}
O *o2 = open_xxx();
if (!xxx_write(o1)) {
close_xxx(o1);
close_xxx(o2);
return -1;
}
// 以此类推, 后面的 return 都得把前面的一堆东西都清理掉才能return
if ....
return 0;
}