c语言实现动态数组。其它c的数据结构实现,hashTable參考点击打开链接 treeStruct參考点击打开链接

基本原理:事先准备好一个固定长度的数组。

假设长度不够的时候。realloc一块区域。

另外:在数组元素降低的情况下。须要缩减数组长度。

主要接口:

  1. cp_bool DyArrayAppend(DyArray* pArr, void* pData)//加数据到数组末尾
  2. cp_bool DyArrayExpand(DyArray* pArr, cp_int32 nNeed)//扩展数组
  3. cp_bool DyArrayDelete(DyArray* pArr, cp_int32 nIndex)//删除元素by index
  4. cp_bool DyArrayShrink(DyArray* pArr)//缩减数组

源码例如以下:(iOS平台的c实现)

  1. //
  2. // dyArray.h
  3. // dataStruct
  4. //
  5. // Created by hherima on 14-7-28.
  6. // Copyright (c) 2014年 . All rights reserved.
  7. //
  8.  
  9. #ifndef dataStruct_dyArray_h
  10. #define dataStruct_dyArray_h
  11. #include <stdlib.h>
  12.  
  13. enum
  14. {
  15. CP_FALSE = 0,
  16. CP_TRUE = !CP_FALSE
  17. };
  18.  
  19. typedef unsigned char cp_bool;
  20. typedef signed int cp_int32;
  21. typedef void (*DataDestroyFunc)(void *);
  22. typedef cp_bool (*DataCmpFunc)(void *,void *);
  23. typedef void (*DataVisitFunc)(void *);
  24.  
  25. #define F_MALLOC_TYPE(s) (s*)f_malloc(sizeof(s))
  26. #define FREEFUN free
  27. #define MIN_PRE_ALLOCATE_SIZE 10 //The initial size of the dynamic array.
  28. #define MEMSETFUN memset
  29. #define REALLOCFUN realloc
  30. #define MALLOCFUN malloc
  31.  
  32. struct DynamicArray
  33. {
  34. void **m_ppData; //the address of the allocated array.
  35. cp_int32 m_nAllocSize; //the allocated array size.
  36. cp_int32 m_nSize; //the used size of the array.
  37. DataDestroyFunc m_fDestroy; //the callback function to destroy one data.
  38. DataCmpFunc m_fCmp; //the callback function to compare one data.
  39. DataVisitFunc m_fVisit; //the callback function to visit each data.
  40. };
  41. typedef struct DynamicArray DyArray;
  42.  
  43. DyArray* DyArrayCreate(DataDestroyFunc pDataDestroy);
  44. cp_bool DyArrayInsert(DyArray* pArr, cp_int32 nIndex, void* pData);
  45. cp_bool DyArrayPrepend(DyArray* pArr, void* pData);
  46. cp_bool DyArrayAppend(DyArray* pArr, void* pData);
  47. cp_bool DyArrayDelete(DyArray* pArr, cp_int32 nIndex);
  48. cp_bool DyArrayDeleteEx(DyArray* pArr, cp_int32 nBegin,cp_int32 nEnd);
  49. cp_bool DyArrayGetByIndex(DyArray* pArr, cp_int32 nIndex, void** ppData);
  50. cp_bool DyArrayGetFirst(DyArray* pArr,void** ppData);
  51. cp_bool DyArrayGetLast(DyArray* pArr,void** ppData);
  52. cp_bool DyArraySetByIndex(DyArray* pArr, cp_int32 nIndex, void* pData);
  53. cp_int32 DyArrayLength(DyArray* pArr);
  54. cp_int32 DyArrayFind(DyArray* pArr, DataCmpFunc pCmp,void *pData);
  55. cp_bool DyArrayForEach(DyArray* pArr, DataVisitFunc pVisit);
  56. void DyArrayDestroy(DyArray* pArr);
  57. void DyArrayDestroyCustom(DyArray* pArr,DataDestroyFunc pDataDestroy);
  58. void DyArrayReset(DyArray* pArr);//shrink
  59. void DyArrayClear(DyArray* pArr);//not shrink
  60. void DyArrayResetCustom(DyArray* pArr,DataDestroyFunc pDataDestroy);
  61. void DyArrayClearCustom(DyArray* pArr,DataDestroyFunc pDataDestroy);//not shrink
  62.  
  63. #endif
  1. //
  2. // dyArray.c
  3. // dataStruct
  4. //
  5. // Created by hherima on 14-7-28.
  6. // Copyright (c) 2014年 . All rights reserved.
  7. //
  8.  
  9. #include "dyArray.h"
  10.  
  11. void* f_malloc(cp_int32 size)
  12. {
  13. void* p = MALLOCFUN(size);
  14. if(p)
  15. MEMSETFUN(p, 0, size);
  16.  
  17. return p;
  18. }
  19. /**************************************************************************************************
  20. 【函数名】: DyArrayCreate
  21. 【描写叙述】: 创建长度为MIN_PRE_ALLOCATE_SIZE的动态指针数组
  22. 【參数】:
  23. pDataDestroy: the callback function to destroy one data in data array.
  24. 【返回值】: 动态指针数组的地址
  25. ***************************************************************************************************/
  26.  
  27. DyArray* DyArrayCreate(DataDestroyFunc pDataDestroy)
  28. {
  29. DyArray *pArr = NULL;
  30. pArr = F_MALLOC_TYPE(DyArray);
  31.  
  32. //if the input parameter is invalid, return.
  33. if(!pArr)
  34. {
  35. return NULL;
  36. }
  37. //malloc memory for dynamic array and iniatilize it.
  38. pArr->m_fDestroy = pDataDestroy;
  39. pArr->m_ppData = (void *)f_malloc(sizeof(void *)*MIN_PRE_ALLOCATE_SIZE);
  40. if(!pArr->m_ppData)
  41. {
  42. FREEFUN(pArr);
  43. return NULL;
  44. }
  45. pArr->m_nAllocSize = MIN_PRE_ALLOCATE_SIZE;
  46. return pArr;
  47. }
  48.  
  49. /**************************************************************************************************
  50. 【函数名】: DyArrayGetByIndex
  51. 【描写叙述】:获取数组元素by index
  52. 【參数】:
  53. pArr: the array's address.
  54. nIndex: the element's position.
  55. ppData: out parameter, to record the element's pointer.
  56. 【返回值】: true or false.
  57. ***************************************************************************************************/
  58.  
  59. cp_bool DyArrayGetByIndex(DyArray* pArr, cp_int32 nIndex, void** ppData)
  60. {
  61. //if the input parameter is invalid, return.
  62. if(!pArr || nIndex < 0 || !ppData || nIndex >= pArr->m_nSize)
  63. {
  64. *ppData = NULL;
  65. return CP_FALSE;
  66. }
  67. *ppData = pArr->m_ppData[nIndex];//get the related element.
  68. return CP_TRUE;
  69. }
  70.  
  71. /**************************************************************************************************
  72. 【函数名】: DyArrayGetFirst
  73. 【描写叙述】:获取数组第一个元素
  74. 【參数】:
  75. pArr: the array's address.
  76. ppData: out parameter, to record the element's pointer.
  77. 【返回值】: true or false.
  78. ***************************************************************************************************/
  79.  
  80. cp_bool DyArrayGetFirst(DyArray* pArr,void** ppData)
  81. {
  82. return DyArrayGetByIndex(pArr,0,ppData) ?
  83.  
  84. CP_TRUE : CP_FALSE;
  85. }
  86.  
  87. /**************************************************************************************************
  88. 【函数名】: DyArrayGetLast
  89. 【描写叙述】: 获取数组最后一个元素
  90. 【參数】:
  91. pArr: the array's address.
  92. ppData: out parameter, to record the element's pointer.
  93. 【返回值】: true or false.
  94. ***************************************************************************************************/
  95.  
  96. cp_bool DyArrayGetLast(DyArray* pArr,void** ppData)
  97. {
  98. return DyArrayGetByIndex(pArr,pArr->m_nSize-1,ppData) ? CP_TRUE : CP_FALSE;
  99. }
  100.  
  101. /**************************************************************************************************
  102. 【函数名】:DyArraySetByIndex
  103. 【描写叙述】: 设置数组元素by index
  104. 【參数】:
  105. pArr: the array's address.
  106. nIndex: the element's position.
  107. pData: the element's pointer.
  108. 【返回值】: true or false.
  109. ***************************************************************************************************/
  110.  
  111. cp_bool DyArraySetByIndex(DyArray* pArr, cp_int32 nIndex, void* pData)
  112. {
  113. //if the input parameter is invalid, return.
  114. if(!pArr || nIndex < 0)
  115. {
  116. return CP_FALSE;
  117. }
  118. pArr->m_ppData[nIndex] = pData;//find the related position and set its value.
  119. return CP_TRUE;
  120. }
  121.  
  122. /**************************************************************************************************
  123. 【函数名】: DyArrayLength
  124. 【描写叙述】:获取数组的长度
  125. 【參数】:
  126. pArr: the array's address.
  127. 【返回值】: 数组长度.
  128. ***************************************************************************************************/
  129.  
  130. cp_int32 DyArrayLength(DyArray* pArr)
  131. {
  132. return pArr ? pArr->m_nSize : -1;
  133. }
  134.  
  135. /**************************************************************************************************
  136. 【函数名】: DyArrayFind
  137. 【描写叙述】:查找数组中的指定元素
  138. 【參数】:
  139. pArr: the array's address.
  140. pCmp: the callback function to compare the data.
  141. pData: the search destination in the array.
  142. 【返回值】:假设成功返回位置,否则返回-1
  143. ***************************************************************************************************/
  144.  
  145. cp_int32 DyArrayFind(DyArray* pArr, DataCmpFunc pCmp,void *pData)
  146. {
  147. cp_int32 i;
  148.  
  149. //if the input parameter is invalid, return.
  150. if(!pArr)
  151. {
  152. return -1;
  153. }
  154. //visit each one to find the right one.
  155. for(i=0; i<pArr->m_nSize;i++)
  156. {
  157. if(pCmp)
  158. {
  159. if(pCmp(pArr->m_ppData[i],pData) == CP_TRUE)
  160. {
  161. return i;
  162. }
  163. }else
  164. {
  165. //if NO compare funciton, just compare the address.
  166. if(pArr->m_ppData[i] == pData)
  167. {
  168. return i;
  169. }
  170. }
  171.  
  172. }
  173. return -1;
  174. }
  175.  
  176. /**************************************************************************************************
  177. 【函数名】: DyArrayForEach
  178. 【描写叙述】:遍历数组
  179. 【參数】:
  180. pArr: the array's address.
  181. pVisit: the callback function to visit the data.
  182. 【返回值】:假设成功返回true,否则false
  183. ***************************************************************************************************/
  184.  
  185. cp_bool DyArrayForEach(DyArray* pArr, DataVisitFunc pVisit)
  186. {
  187. cp_int32 i;
  188.  
  189. //if the input parameter is invalid, return.
  190. if(!pArr || !pVisit)
  191. {
  192. return CP_FALSE;
  193. }
  194. //visit each one with the visit function.
  195. for(i=0; i<pArr->m_nSize;i++)
  196. {
  197. pVisit(pArr->m_ppData[i]);
  198. }
  199. return CP_TRUE;
  200. }
  201.  
  202. /**************************************************************************************************
  203. 【函数名】: DyArrayDestroy
  204. 【描写叙述】:销毁整个数组
  205. 【參数】:
  206. pArr: the array's address.
  207. 【返回值】:NA
  208. ***************************************************************************************************/
  209.  
  210. void DyArrayDestroy(DyArray* pArr)
  211. {
  212. cp_int32 i;
  213.  
  214. //if the input parameter is invalid, return.
  215. if(!pArr)
  216. {
  217. return;
  218. }
  219. //Using destroy function to destroy each element's memory.
  220. if(pArr->m_fDestroy)
  221. {
  222. for(i=0; i<pArr->m_nSize; i++)
  223. {
  224. pArr->m_fDestroy(pArr->m_ppData[i]);
  225. }
  226. }
  227. //free the array.
  228. FREEFUN(pArr->m_ppData);
  229. pArr->m_ppData = NULL;
  230. FREEFUN(pArr);
  231. pArr = NULL;
  232. }
  233.  
  234. /**************************************************************************************************
  235. 【函数名】: DyArrayDestroyCustom
  236. 【描写叙述】:使用用户函数。小围数组
  237. 【參数】:
  238. pArr: the array's address.
  239. pDataDestroy: user's destroy function.
  240. 【返回值】:NA
  241. ***************************************************************************************************/
  242.  
  243. void DyArrayDestroyCustom(DyArray* pArr,DataDestroyFunc pDataDestroy)
  244. {
  245. cp_int32 i;
  246.  
  247. //if the input parameter is invalid, return.
  248. if(!pArr)
  249. {
  250. return;
  251. }
  252. //Using destroy function to destroy each element's memory.
  253. if(pDataDestroy)
  254. {
  255. for(i=0; i<pArr->m_nSize;i++)
  256. {
  257. pDataDestroy(pArr->m_ppData[i]);
  258. }
  259. }
  260. //free the array.
  261. FREEFUN(pArr->m_ppData);
  262. pArr->m_ppData = NULL;
  263. FREEFUN(pArr);
  264. }
  265.  
  266. /**************************************************************************************************
  267. 【函数名】: DyArrayExpand
  268. 【描写叙述】:扩展数组
  269. 【參数】:
  270. pArr: the array's address.
  271. nNeed: the needed new size.
  272. 【返回值】:假设成功返回ture否则返回false
  273. ***************************************************************************************************/
  274.  
  275. cp_bool DyArrayExpand(DyArray* pArr, cp_int32 nNeed)
  276. {
  277. cp_int32 allocSize = 0;
  278. void** data = NULL;
  279.  
  280. //if the input parameter is invalid, return.
  281. if(!pArr)
  282. {
  283. return CP_FALSE;
  284. }
  285. //if need, expand to 1.5 times of the original size.
  286. if((pArr->m_nSize + nNeed) > pArr->m_nAllocSize)
  287. {
  288. allocSize = pArr->m_nAllocSize + (pArr->m_nAllocSize>>1);
  289. data = (void**)REALLOCFUN(pArr->m_ppData, sizeof(void*) * allocSize);
  290. if(data != NULL)
  291. {
  292. //clear the expanded space of the new memory.
  293. MEMSETFUN(data+pArr->m_nAllocSize,0,(allocSize-pArr->m_nAllocSize)*sizeof(void*));
  294. pArr->m_ppData = data;
  295. pArr->m_nAllocSize = allocSize;
  296. }
  297. }
  298. return ((pArr->m_nSize + nNeed) <= pArr->m_nAllocSize) ? CP_TRUE : CP_FALSE;
  299. }
  300.  
  301. /**************************************************************************************************
  302. 【函数名】: DyArrayInsert
  303. 【描写叙述】:插入数组by index
  304. 【參数】:
  305. pArr: the array's address.
  306. nIndex: the position in the array to insert new data.
  307. pData: the data to be inserted.
  308. 【返回值】:假设成功返回true否则false
  309. ***************************************************************************************************/
  310.  
  311. cp_bool DyArrayInsert(DyArray* pArr, cp_int32 nIndex, void* pData)
  312. {
  313. cp_bool bRet = CP_FALSE;
  314. cp_int32 nCursor = nIndex;
  315. cp_int32 i;
  316.  
  317. //if the input parameter is invalid, return.
  318. if(!pArr)
  319. {
  320. return CP_FALSE;
  321. }
  322. //get the right cursor.
  323. nCursor = nCursor < pArr->m_nSize ? nCursor : pArr->m_nSize;
  324. if(DyArrayExpand(pArr, 1) == CP_TRUE)
  325. {
  326. //move all the elements after the cursor to the next positon.
  327. for(i = pArr->m_nSize; i > nCursor; i--)
  328. {
  329. pArr->m_ppData[i] = pArr->m_ppData[i-1];
  330. }
  331. //set the cursor's value.
  332. pArr->m_ppData[nCursor] = pData;
  333. pArr->m_nSize++;
  334. bRet = CP_TRUE;
  335. }
  336. return bRet;
  337. }
  338.  
  339. /**************************************************************************************************
  340. 【函数名】: DyArrayPrepend
  341. 【描写叙述】:在数组開始加入一个数据
  342. 【參数】:
  343. pArr: the array's address.
  344. pData: the data to be added.
  345. 【返回值】:假设成功返回ture,否则false
  346. ***************************************************************************************************/
  347.  
  348. cp_bool DyArrayPrepend(DyArray* pArr, void* pData)
  349. {
  350. return DyArrayInsert(pArr,0,pData) ? CP_TRUE:CP_FALSE;
  351. }
  352.  
  353. /**************************************************************************************************
  354. 【函数名】: DyArrayAppend
  355. 【描写叙述】:加入数据到数组末尾
  356. 【參数】:
  357. pArr: the array's address.
  358. pData: the data to be added.
  359. 【返回值】:假设成功返回true。否则false
  360. ***************************************************************************************************/
  361.  
  362. cp_bool DyArrayAppend(DyArray* pArr, void* pData)
  363. {
  364. return DyArrayInsert(pArr,pArr->m_nSize,pData) ?
  365.  
  366. CP_TRUE:CP_FALSE;
  367. }
  368.  
  369. /**************************************************************************************************
  370. 【函数名】: DyArrayShrink
  371. 【描写叙述】:缩减数组
  372. 【參数】:
  373. pArr: the array's address.
  374. 【返回值】:ture 或者false
  375. ***************************************************************************************************/
  376.  
  377. cp_bool DyArrayShrink(DyArray* pArr)
  378. {
  379. cp_int32 nAllocSize = 0;
  380. void** pData = NULL;
  381.  
  382. //if the input 【參数】 is invalid, return.
  383. if(!pArr)
  384. {
  385. return CP_FALSE;
  386. }
  387. //if need, shrink the array to 1.5 times of elements number.
  388. if((pArr->m_nSize < (pArr->m_nAllocSize >> 1)) && (pArr->m_nAllocSize > MIN_PRE_ALLOCATE_SIZE))
  389. {
  390. nAllocSize = pArr->m_nSize + (pArr->m_nSize >> 1);
  391. pData = (void**)REALLOCFUN(pArr->m_ppData, sizeof(void*) * nAllocSize);
  392. if(pData != NULL)
  393. {
  394. //clear memory of the unused space of new memory.
  395. MEMSETFUN(pData+pArr->m_nSize,0,(nAllocSize-pArr->m_nSize)*sizeof(void*));
  396. pArr->m_ppData = pData;
  397. pArr->m_nAllocSize = nAllocSize;
  398. }
  399. }
  400. return CP_TRUE;
  401. }
  402.  
  403. /**************************************************************************************************
  404. 【函数名】: DyArrayDelete
  405. 【描写叙述】:删除元素by index
  406. 【參数】:
  407. pArr: the array's address.
  408. nIndex: the position in the array to delete useless data.
  409. 【返回值】:ture 或者false
  410. ***************************************************************************************************/
  411.  
  412. cp_bool DyArrayDelete(DyArray* pArr, cp_int32 nIndex)
  413. {
  414. cp_int32 i;
  415.  
  416. //if the input parameter is invalid, return.
  417. if(!pArr)
  418. {
  419. return CP_FALSE;
  420. }
  421. //destroy the element with destroy function.
  422. if(pArr->m_fDestroy)
  423. {
  424. pArr->m_fDestroy(pArr->m_ppData[nIndex]);
  425. }
  426. //move the elements after 'nIndex' to the previous one.
  427. for(i = nIndex; (i+1) < pArr->m_nSize; i++)
  428. {
  429. pArr->m_ppData[i] = pArr->m_ppData[i+1];
  430. }
  431. //set the last one to null.
  432. pArr->m_ppData[i] = NULL;
  433. pArr->m_nSize--;
  434. //if need ,shrink the size.
  435. DyArrayShrink(pArr);
  436. return CP_TRUE;
  437. }
  438. /**************************************************************************************************
  439. 【函数名】: DyArrayDeleteEx
  440. 【描写叙述】:删除元素by index (扩展)
  441. 【參数】:
  442. pArr: the array's address.
  443. nIndex: the position in the array to delete useless data.
  444. nEdn:
  445. 【返回值】:ture 或者false
  446. ***************************************************************************************************/
  447.  
  448. cp_bool DyArrayDeleteEx(DyArray* pArr, cp_int32 nBegin,cp_int32 nEnd)
  449. {
  450. cp_int32 i,nLen = 0;
  451.  
  452. //if the input parameter is invalid, return.
  453. //if(!pArr && nBegin>nEnd && nBegin<0 && nBegin>=pArr->m_nSize && nEnd<0 && nEnd>=pArr->m_nSize)
  454. if(!pArr)
  455. {
  456. return CP_FALSE;
  457. }
  458. //destroy the element with destroy function.
  459. if(pArr->m_fDestroy)
  460. {
  461. for(i=nBegin; i<=nEnd; i++)
  462. {
  463. pArr->m_fDestroy(pArr->m_ppData[i]);
  464. }
  465. }
  466. //move the elements after 'nIndex' to the previous one.
  467. nLen = nEnd - nBegin + 1;
  468. for(i = nBegin; (i+nLen) < pArr->m_nSize; i++)
  469. {
  470. pArr->m_ppData[i] = pArr->m_ppData[i+nLen];
  471. }
  472. //set the last one to null.
  473. //pArr->m_ppData[i] = NULL;
  474. pArr->m_nSize -= nLen;
  475. //if need ,shrink the size.
  476. DyArrayShrink(pArr);
  477. return CP_TRUE;
  478. }
  479.  
  480. /**************************************************************************************************
  481. 【函数名】: DyArrayReset
  482. 【描写叙述】:清除数组数据,缩减数组到原始大小
  483. 【參数】:
  484. pArr: the array's address.
  485. 【返回值】:
  486. none.
  487. ***************************************************************************************************/
  488.  
  489. void DyArrayReset(DyArray* pArr)
  490. {
  491. cp_int32 i;
  492. void** pData = NULL;
  493.  
  494. //if the input parameter is invalid, return.
  495. if(!pArr)
  496. {
  497. return;
  498. }
  499. //reset all the elements with destroy function.
  500. if(pArr->m_fDestroy)
  501. {
  502. for(i=0; i<pArr->m_nSize;i++)
  503. {
  504. pArr->m_fDestroy(pArr->m_ppData[i]);
  505. }
  506. }
  507. pArr->m_nSize = 0;
  508. //if need, shrink the size.
  509. if(pArr->m_nAllocSize > MIN_PRE_ALLOCATE_SIZE)
  510. {
  511. pData = (void**)REALLOCFUN(pArr->m_ppData, sizeof(void*) * MIN_PRE_ALLOCATE_SIZE);
  512. if(pData != NULL)
  513. {
  514. pArr->m_ppData = pData;
  515. pArr->m_nAllocSize = MIN_PRE_ALLOCATE_SIZE;
  516. }
  517. if(pArr->m_ppData)
  518. {
  519. MEMSETFUN(pArr->m_ppData,0,sizeof(void*)*pArr->m_nAllocSize);
  520. }
  521. }
  522. }
  523. /**************************************************************************************************
  524. 【函数名】: DyArrayClear
  525. 【描写叙述】:清除数组数据。不缩减数组到原始大小
  526. 【參数】:
  527. pArr: the array's address.
  528. 【返回值】:NA
  529. ***************************************************************************************************/
  530.  
  531. void DyArrayClear(DyArray* pArr)
  532. {
  533. cp_int32 i;
  534. void** pData = NULL;
  535.  
  536. //if the input parameter is invalid, return.
  537. if(!pArr)
  538. {
  539. return;
  540. }
  541. //reset all the elements with destroy function.
  542. if(pArr->m_fDestroy)
  543. {
  544. for(i=0; i<pArr->m_nSize;i++)
  545. {
  546. pArr->m_fDestroy(pArr->m_ppData[i]);
  547. }
  548. }
  549. pArr->m_nSize = 0;
  550. }
  551.  
  552. /**************************************************************************************************
  553. 【函数名】: DyArrayResetCustom
  554. 【描写叙述】:清除数组使用用户函数,缩减数组到原始大小
  555. 【參数】:
  556. pArr: the array's address.
  557. pDataDestroy: user's destroy function.
  558. 【返回值】:NA
  559. ***************************************************************************************************/
  560.  
  561. void DyArrayResetCustom(DyArray* pArr,DataDestroyFunc pDataDestroy)
  562. {
  563. cp_int32 i;
  564. void** pData = NULL;
  565.  
  566. //if the input parameter is invalid, return.
  567. if(!pArr)
  568. {
  569. return;
  570. }
  571. //reset all the elements with destroy function.
  572. if(pDataDestroy)
  573. {
  574. for(i=0; i<pArr->m_nSize;i++)
  575. {
  576. pDataDestroy(pArr->m_ppData[i]);
  577. }
  578. }
  579. pArr->m_nSize = 0;
  580. //if need, shrink the size.
  581. if(pArr->m_nAllocSize > MIN_PRE_ALLOCATE_SIZE)
  582. {
  583. pData = (void**)REALLOCFUN(pArr->m_ppData, sizeof(void*) * MIN_PRE_ALLOCATE_SIZE);
  584. if(pData != NULL)
  585. {
  586. pArr->m_ppData = pData;
  587. pArr->m_nAllocSize = MIN_PRE_ALLOCATE_SIZE;
  588. }
  589. if(pArr->m_ppData)
  590. {
  591. MEMSETFUN(pArr->m_ppData,0,sizeof(void*)*pArr->m_nAllocSize);
  592. }
  593. }
  594. }
  595. /**************************************************************************************************
  596. 【函数名】: DyArrayClearCustom
  597. 【描写叙述】:清除数组使用用户函数,不缩减数组到原始大小
  598. 【參数】:
  599. pArr: the array's address.
  600. pDataDestroy: user's destroy function.
  601. 【返回值】:NA
  602. ***************************************************************************************************/
  603.  
  604. void DyArrayClearCustom(DyArray* pArr,DataDestroyFunc pDataDestroy)
  605. {
  606. cp_int32 i;
  607. void** pData = NULL;
  608.  
  609. //if the input parameter is invalid, return.
  610. if(!pArr)
  611. {
  612. return;
  613. }
  614. //reset all the elements with destroy function.
  615. if(pDataDestroy)
  616. {
  617. for(i=0; i<pArr->m_nSize;i++)
  618. {
  619. pDataDestroy(pArr->m_ppData[i]);
  620. }
  621. }
  622. pArr->m_nSize = 0;
  623.  
  624. }

c语言实现动态指针数组Dynamic arrays的更多相关文章

  1. C语言中的指针数组

    C语言中的指针数组是什么,像 char *a[]={"ddd","dsidd","lll"}; 这里讲一下注意如果我们使用了a也就是首元素的 ...

  2. C语言中的指针数组和数组指针

    代码: #include <iostream> using namespace std; int main(){ ]; ]; cout<<sizeof(a)<<en ...

  3. C语言的函数指针数组(好绕啊~看完这篇估计就通关了)

    转自https://www.cnblogs.com/chr-wonder/p/5168858.html int *(*p(int))[3] 今天有人问这个是啥?我一看直接就懵逼了…… 下面做一些简单的 ...

  4. C语言深度剖析-----指针数组和数组指针的分析

    指针数组和数组指针的分析 数组类型 定义数组类型 数组指针 这个不可能为数组指针,指向数组首元素 例 指针数组 例    main函数的参数 例 小结

  5. 嵌入式-C语言基础:指针数组(和数组指针区分开来)

    指针数组:一个数组,若其元素均为指针类型的数据,称为指针数组,指针数组存放的是指针类型的数据,也就是指针数组的每个元素都存放一个地址.下面定义一个指针数组: int * p[4];//[]的优先级是比 ...

  6. C#委托与C语言函数指针及函数指针数组

    C#委托与C语言函数指针及函数指针数组 在使用C#时总会为委托而感到疑惑,但现在总新温习了一遍C语言后,才真正理解的委托. 其实委托就类似于C/C++里的函数指针,在函数传参时传递的是函数指针,在调用 ...

  7. C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com

    原文:C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | I ...

  8. 【嵌入式开发】C语言 内存分配 地址 指针 数组 参数 实例解析

    . Android源码看的鸭梨大啊, 补一下C语言基础 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/detai ...

  9. 数据结构基础(1)--数组C语言实现--动态内存分配

    数据结构基础(1)--数组C语言实现--动态内存分配 基本思想:数组是最常用的数据结构,在内存中连续存储,可以静态初始化(int a[2]={1,2}),可以动态初始化 malloc(). 难点就是数 ...

随机推荐

  1. 通过xml生成word文档

    Xml生成word总结 使用xml生成word的基本步骤在<使用xslt转化xml数据形成word文档导出.doc>中说明比较清楚了.但是其中的细节并未说到,因此自己折腾了两天总算成功了. ...

  2. Android 设备管理器 阻止用户取消激活

    该方案测试可行,系统版本4.4.2.它算是借助android系统的一个bug,不确定在后续更高的版本中是否修复. 该功能和360防卸载功能一样的实现原理. 主要的参考资料是:http://bbs.pe ...

  3. linux系统文件属性-硬连接、软连接

    1 硬链接概念 硬链接是指通过索引节点(Inode)来进行链接,在Linux(ext2,ext3)文件系统中,保存在磁盘分区中的文件不管是什么类型都会给它分配一个编号,这个编号被称为索引节点编号(In ...

  4. Android 程式开发:(二十)内容提供者 —— 20.6 自定义ContentProvider的使用

    现在,ContentProvider已经创建好了,可以去尝试使用一下. 1. 使用之前的工程,在布局文件main.xml中添加一些控件. <?xml version="1.0" ...

  5. 怎样用HTML5 Canvas制作一个简单的游戏

    原文连接: How To Make A Simple HTML5 Canvas Game 自从我制作了一些HTML5游戏(例如Crypt Run)后,我收到了很多建议,要求我写一篇关于怎样利用HTML ...

  6. HDU-1664-Different Digits(BFS)

    Problem Description Given a positive integer n, your task is to find a positive integer m, which is ...

  7. javascript笔记整理(window对象)

    浏览器对象模型 (BOM--Browser Object Model),window对象是BOM中所有对象的核心 A.属性 1.(位置类型-获得浏览器的位置) IE:window.screenLeft ...

  8. 修改OpenSSL默认编译出的动态库文件名称

    在 Windows 平台上调用动态链接库 dll 文件时,有两种方式:a) 隐式的加载时链接:使用 *.lib (导入库)文件,在 IDE 的链接器相关设置中加入导入库 lib 文件的名称,或在程序中 ...

  9. Spring MVC 遇到的一点点问题(转)

    今天下午下班之前看了看凯歌给的Spring Training的教程的lab篇,我之前有跟着做没有遇到什么问题,但是到了跟Spring MVC integrating的时候,遇到一点点有趣的事情. 这个 ...

  10. Swift - 正则表达式的使用(附用户名、邮箱、URL等常用格式验证)

    Swift虽然是一个新出的语言,但却不提供专门的处理正则的语法和类.所以我们只能使用古老的NSRegularExpression类进行正则匹配. 即先接受一个正则表达式的字符串,由此生成NSRegul ...