c语言怎么实现模块化vc,原创:在C语言中大概实现VC++中的CArray部分功能的两种方法...

这个代码段定义了一个名为`ListArray`的结构体,用于创建、管理和操作动态列表。它包含了添加、移除和析构元素的功能。`ListAdd`函数用于向列表中添加元素,`ListRemove`用于移除元素,`ListDelete`则用于释放列表占用的内存。整个库使用了C++的内存管理函数`rt_malloc`和`rt_free`来分配和释放内存。
摘要由CSDN通过智能技术生成

#ifndef __LISTARRAY_H__

#define __LISTARRAY_H__

#include "rtthread.h"

#include "finsh.h"

//LIST数组

typedef struct _ListArray ListArray;

struct _ListArray

{

void **pListPointArray;                         //LIST数组指针

int Total;                                      //元素个数

void (*Add)(ListArray *pList, void *pListPoint);     //添加

void (*Remove)(ListArray *pList, void *pListPoint);  //移除

void (*Delete)(void *pList);                    //析构

};

//List类的析构函数

static void ListDelete(void *pList)

{

if(((ListArray *)pList)->pListPointArray != RT_NULL)     //先释放指针数组

{

rt_free(((ListArray *)pList)->pListPointArray);

}

rt_free(pList);                                     //再释放整个List类

}

//元素增加函数

static void ListAdd(ListArray *pList, void *pListPoint)

{

void **tListPointArray = rt_malloc(sizeof(int *) * (pList->Total + 1));     //申请比原来大一个存储单元的内存

int pListIndex;

for(pListIndex = 0; pListIndex < pList->Total; pListIndex++)        //拷贝

{

if(pList->pListPointArray[pListIndex] == pListPoint)                 //判断,如果有相同的元素存在

{

rt_free(tListPointArray);                                        //释放现申请的内存

return;                                                     //返回

}

tListPointArray[pListIndex] = pList->pListPointArray[pListIndex];         //拷贝

}

tListPointArray[pList->Total] = pListPoint;                              //将添加的元素放到最后一个存储单元中

pList->Total += 1;                                                  //总数加1

if(pList->pListPointArray != RT_NULL) rt_free(pList->pListPointArray);                      //释放原来的内存

pList->pListPointArray = tListPointArray;                                            //将新的句柄替换原句柄

}

//元素移除函数

static void ListRemove(ListArray *pList, void *pListPoint)

{

int pListIndex, tListIndex;

void **tListPointArray;

void **FreePointArray;

void **SavePointArray;

if(pList->Total == 0) return;                                       //总数为0时退出

tListPointArray = rt_malloc(sizeof(int) * (pList->Total - 1));    //申请比原来小一个存储单元的内存

FreePointArray = tListPointArray;                                  //将刚申请的内存空间作为默认的释放空间

SavePointArray = pList->pListPointArray;                           //将已有的内存空间作为默认的存储空间

for(pListIndex = 0, tListIndex= 0; pListIndex < pList->Total; pListIndex++) //查找移除点

{

if(pList->pListPointArray[pListIndex] == pListPoint)         //当前点是移除点

{

FreePointArray = pList->pListPointArray;            //改变释放内存指针

SavePointArray = tListPointArray;                   //改变保留内存指针

continue;                                           //结束本次循环

}

if(tListIndex < (pList->Total -1))                      //如果当前点不是移除点,拷贝序号小于总量减1

{

tListPointArray[tListIndex] = pList->pListPointArray[pListIndex];     //拷贝

tListIndex++;                                               //拷贝序号加1

}

}

pList->Total = (SavePointArray == tListPointArray) ? pList->Total - 1 : pList->Total;   //根据保留的内存块改变总数的值

if(FreePointArray != RT_NULL) rt_free(FreePointArray);        //释放该释放的不用的内存块

pList->pListPointArray = SavePointArray; //保留该保留的

}

//List构造函数

static ListArray *ListCreate(void)

{

ListArray *pList = (ListArray *)rt_malloc(sizeof(ListArray));

pList->Total = 0;

pList->pListPointArray = RT_NULL;

pList->Add = ListAdd;

pList->Remove = ListRemove;

pList->Delete = ListDelete;

return pList;

}

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值