影醉阏轩窗

衣带渐宽终不悔,为伊消得人憔悴。
扩大
缩小

工作小结一

工作小结一

1.1 如何进行C矩阵运算

很多时候我们使用matlabpythonc++等进行矩阵操作,但是当进行实际操作的时候,我们得把这些语言使用C去实现。

实现起来有两种方式:

定义方式如下:

typedef struct Matrix_t
{
    unsigned int rows;
    unsigned int cols;
    void* data;		//具体实现看个人需求
}Matrix;

新建和释放方式如下:

//Matrix* matrix = (Matrix*)malloc(sizeof(Matrix));//返回返回加入,参数进入不需要
/*Example
	void CreatMatrix(Matrix* src)
	{
		....;
	}
	Matrix* CreatMatrix()
	{
		return ...;
	}
*/
matrix->data = (void*)malloc(sizeof(void)*rows*cols);
free(matrix->data);
//free(matrix);//返回返回加入,参数进入不需要

遍历方式如下:

for (size_t i=0;i<rows;i++)
    for(size_t j=0;j<cols;j++)
        data[i*cols+j] = value;//operate

定义方式如下:

typedef struct Matrix_t
{
    unsigned int rows;
    unsigned int cols;
    void** data;		//具体实现看个人需求
}Matrix;

新建和释放方式如下:

//Matrix* matrix = (Matrix*)malloc(sizeof(Matrix));//返回返回加入,参数进入不需要
/*Example
	void CreatMatrix(Matrix* src)
	{
		....;
	}
	Matrix* CreatMatrix()
	{
		return ...;
	}
*/
matrix->data = (void**)malloc(sizeof(void*)*rows);
for(size_t i=0;i<rows;i++)
    matrix->data[i] = (void*)malloc(sizeof(void)*cols);
for(size_t i=0;i<rows;i++)
    free(matrix->data[i]);
free(matrix->data);
//free(matrix);//返回返回加入,参数进入不需要

遍历方式如下:

for (size_t i=0;i<rows;i++)
    for(size_t j=0;j<cols;j++)
        data[i][j] = value;//operate

1.2 如何提升malloc和free的效率

当我们使用动态数组二叉树自建vector等操作的时候,得经常用到mallocfree操作,其实这两个操作都很费时。

比如:(1)新建和释放10000次的int。(2)新建和释放1个大小为10000的int内存。这两个笔者未亲自尝试,猜测前者不比后者快多少(或者更慢)。

先申请一块,不够就加倍申请

typedef struct Vector_t
{
    unsigned int realNum;	//当前使用数量
    unsigned int TotalNum;	//申请内存容量
    void* data;		
}Vector;
if(realNum >= TotalNum-2)//保留两个预留位
{
    void* tmp = (void*)malloc(sizeof(void)*Vector->TotalNum*2.0);//不够就扩大两倍
    for(size_t i=0;i<Vector->realNum;i++)
        tmp[i] = Vector->data[i];//复制之前数据
   	free(Vector->data);//释放之前数据
    Vector->data = tmp;//指向新的数据
}

直接申请一大块内存,之后申请的空间全部在内存池之内

笔者没有亲自实现过内存池代码,只是使用别人已经写好的库(公司大神写的)

以下是Github上的库(仅供参考,笔者未测试):

C语言版本

C++版本

1.3 C实现深林

typedef struct TreeNode_t//节点
{
    void 	data;	//data struct storage
    void* 	children;
    void* 	parent;
}TreeNode;
typedef struct TreeNode_t//一棵树
{
    unsigned int realNum;	//当前使用数量
    unsigned int TotalNum;	//申请内存容量
    TreeNode* treeNode;
}TreeNode;
typedef struct Forest_t//一片深林
{
    unsigned int realNum;	//当前使用数量
    unsigned int TotalNum;	//申请内存容量
    TreeNode** treeNode;
}Forest;

建立和释放方法参考1.1节所述,可变长度方法参考1.2节所述

1.4 C优雅的函数

举个例子:

​ 当一系列函数A1、A2、A3.....是为完成某一个项目的,另外一系列函数B1、B2、B3.....是为完成某一个项目的。当然你可以直接定义,通过名字取区分。。。

​ 如何优雅的解决这个问题?

结构体中存放函数指针!!!

#include<stdio.h>
#include<malloc.h>
struct Hello{
	void (*sayHello)(char* name); 
};
void sayHello(char* name){
	printf("你好,%s\n",name);
}
int main(){
	//struct Hello* hello=(struct Hello *)malloc(sizeof(struct Hello));
	//hello->sayHello=sayHello;
    //hello->sayHello("a");
	struct Hello hello= {sayHello};
    hello.sayHello("a");
    return 0;
}

那么我们就可以建立两个A和B结构体,把相应的函数指针放入其中即可。

posted on 2019-10-09 22:31  影醉阏轩窗  阅读(184)  评论(0编辑  收藏  举报

导航

/* 线条鼠标集合 */ /* 鼠标点击求赞文字特效 */ //带头像评论