用宏实现动态数组

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

提示:以下代码均未经详细测试。如果哪位有空,提出点意见,不胜感激,另本主题是用宏实现,所以不相关的就不要多说了。

#ifndef _VECTOR_H_

#define _VECTOR_H_

#include

typedef struct _vector{

int size;

void *data;

}VECTOR,*PVECTOR;

#define vector_init(x,t,z) do{ PVECTOR v;int n; v=(PVECTOR)(x);n=(int)(z); v->data=malloc(v->size=sizeof(t)*n);\

}while(0)

#define vector_cleanup(x) free((x)->data)

#define vector_get(v,t,n) (((t*)((v)->data))[n])

#define vector_set_fast(v,t,n,d) (((t*)((v)->data))[n]=(d))

#define vector_set(x,t,z,d) do{ PVECTOR v;int n;t temp; v=(PVECTOR)(x);n=(int)(z);temp=(d) if(n*sizeof(t)>v->size) { void *dt;

dt=realloc(v->size=n*sizeof(t)); if(!dt) break; v->data=dt; } memcpy(((t*)v->data)+n,&temp,sizeof(t)); }while(0)

#endif

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航