C 语言设计模式(一) 动态数组

       C 语言中也有很多经典的设计,这些设计被大量的用在软件中。 下面的动态数组的设计比较经典,其实自己也设计过,这次在flex 的源代码中发现这个设计方法,认为是一个可以提炼出来的模式。
      头文件:

#ifndef __BUF_H__
#define __BUF_H__

#include <stdio.h>

struct Buf {
void * elts;
int nelts;
size_t elmsize;
int nmax;
};

void buf_init(struct Buf * , size_t );
void buf_destroy(struct Buf *);

struct Buf* buf_append(struct Buf * , void * , size_t);

#endif

   
         实现文件为:


  
  
   
   

   
   
#include <stdlib.h> 
   
   
    
     #include <string.h> 
    
    
     
      #include "buf.h"    /* stub code */   void * allocate_array(int n, size_t es)  { 	 	register void * mem; 	 	size_t  cbsize = n * es; 	 	mem = malloc(cbsize); 	 	 	if(!mem) { 		 		/* fatal error , no memory */ 		 		return NULL; 	 	} 	 	return mem;  }   void * reallocate_array(void *ptr, int n , size_t es)  { 	 	register void * mem; 	 	size_t cbsize = n * es; 	 	mem = (void*)realloc(ptr,cbsize); 	 	 	if(!mem) { 		 		/* fatal error, no memory */ 		 		return NULL; 	 	} 	 	return mem;  }   void flex_free(void *ptr)  { 	 	if(ptr) { 		 		free(ptr); 	 	}  }   void buf_init(struct Buf * buf, size_t size)  { 	 	buf->elts = NULL; 	 	buf->nelts = buf->nmax = 0 ; 	 	buf->elmsize = size;  }   void buf_destroy(struct Buf *buf)  { 	 	if(buf && buf->elts) { 		 		flex_free(buf->elts); 		 		buf->elts = NULL; 	 	}  }   struct Buf* buf_append(struct Buf * buf, void * ptr, size_t n)  { 	 	int cbsize = 0 ; 	 	if(!ptr || n == 0) return buf; 	 	 	if( n + buf->nelts > buf->nmax) { 		 		cbsize = n + buf->nelts;  		 		 		if(((cbsize * buf->elmsize) % 512 != 0) && buf->elmsize < 512) { 			 			cbsize += ( 512 - (cbsize * buf->elmsize) % 512 ) / buf->elmsize;  		 		} 		 		 		if(!buf->elts) { 			 			buf->elts = allocate_array(cbsize,buf->elmsize); 		 		} else { 			 			buf->elts = reallocate_array(buf->elts,cbsize,buf->elmsize); 		 		}   	} 	 	memcpy((char*) buf->elts +  buf->nelts * buf->elmsize , ptr, n * buf->elmsize); 	 	buf->nelts += n;	 	 	return buf;  } 
    
    
   
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值