Buffer* b_create(int init_capacity, char inc_factor,char o_mode){
Buffer* buffer=NULL; /*local pointer to the buffer structure on heap*/
if (o_mode=='f' || inc_factor==0){
o_mode=0;
inc_factor=0;
}else if (o_mode=='f' && inc_factor!=0){
o_mode=0;
inc_factor=0;
}else if (o_mode=='a' && 1<=inc_factor<=255){
o_mode=1;
inc_factor=inc_factor;
}else if (o_mode=='m' && 1<=inc_factor<=100){
o_mode=-1;
inc_factor=inc_factor;
}
else{
buffer=NULL;
return buffer;
}
//throw a more meaningful error??
//cleanup :: how about i just call the ca_destroy() ??
/*
free(buffer->ca_head);
buffer->ca_head=NULL;
free(buffer);
buffer=NULL;
*/
buffer = (Buffer*)malloc(sizeof(Buffer));
buffer->ca_head=(char*)malloc(sizeof(char)*init_capacity);
buffer->mode=o_mode;
buffer->inc_factor=inc_factor;
buffer->capacity=init_capacity;
buffer->addc_offset=0;
buffer->mark_offset=0;
buffer->r_flag=0;
return buffer;
}
我有上述方法。我正在努力让它工作。首先,我检查并设置了几个变量。如果没有任何情况适用,那么我将缓冲区设置为 NULL 并尝试将值返回给调用函数。
else{
buffer=NULL;
return buffer;
}
然而,在返回之后,函数继续并继续创建缓冲区结构。我希望该函数一旦看到返回语句就会退出。为什么它继续执行?