static变量和函数如何巧妙调用
摘要:app.c 和 main.c 之间,在main.c中调用app.c的static变量和函数,需要利用一个结构体结合指针通过传地址的方式间接访问。 app main struct { int , func()}作为一种通道或载体 直接上一个代码: /*main.c*/ #include "common
阅读全文
mmap代替通用IO读取文件数据(curious)
摘要:提供一份测试demo: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> #include <sys/fil
阅读全文
进程间文件读写锁的应用
摘要:直接提供一份文件锁的demo: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> /*check lock
阅读全文
一个lseek引起的思考
摘要:先看一段代码: int find_value(int fd) { int ret; char buff[8] = ""; struct timeval st,ed; long long delta; gettimeofday(&st,NULL); ret = read(fd,buff,sizeof(
阅读全文
删除双向链表的其中一个节点
摘要:以B作为删除的节点,代码思路如下: { line *temp = head; while(temp) { if (temp.data == date) { //B as temp temp->next->prev = temp->prev; //M1 temp->prev->next = temp-
阅读全文
双向链表的三种插入方法研究
摘要:1. 初始化一个双向链表。 2. 具体方法插入,每个节点可以存在如下结构: struct node_each node{ int data; struct node_each *prev; struct node_each *next; }; A. 头插入法 代码思路: { D->next = he
阅读全文
数组判断全零的一些代码研究
摘要:最精简版本 int check_data(int *array, int n) { while(n--) if (*array++ != 0x00) return 0; return 1; } 返回1 array数组全零,否则正常非全零。 int check_data(int *array) { w
阅读全文
一个易出错的数组题目
摘要:上题目,下面上一个分析: n = 10, i = 9, pa[6] = pa[6] + pa[9]; ........ 过程中出现pa[6] + pa[6],此时的pa[6] != 7,而是现内存下的值pa[6] = 0 + 9 + 8 + 7 = 24 pa[6] = pa[6] + pa[6]
阅读全文
二分法的理解与进化
摘要:最基础的二分法算法C int search_digit(int *num,int cnt,int target) { int first = 0; int last = cnt - 1; int mid;/* |x x x x o| -> |x x m x o| x m x */ while(fir
阅读全文
strcpy、strncpy 和安全的strncpy_s
摘要:strcpy和strncpy摘于linux 内核源码的/lib/string.c char *self_strcpy(char *dest, const char *src) { char *tmp = dest; while ((*dest++ = *src++) != '\0') /* noth
阅读全文
如何看待malloc产生内存碎片
摘要:上代码直接研究: int main() { int *heap_d; int *heap_e; int *heap_f; heap_d = (int *)malloc(10); heap_e = (int *)malloc(10); printf("The d address is %p\n",he
阅读全文
Linux C申请内存三种基本方式
摘要:一份代码可以知道具体方式和原理: int main() { int stack_a; int stack_b; static int static_c; static int static_d; int *heap_e; int *heap_f; heap_e = (int *)malloc(10)
阅读全文
const char * 组合理解
摘要:1 . const char *ptr 从char *ptr 可以理解为指向字符常量的指针,ptr是一个指向char *的常量,*ptr的值为const,不能修改。 2. char const *ptr 同上 3. char * const ptr 定义一个指向字符的指针常数,不能修改指针,但可以修
阅读全文
分析一下函数指针的使用
摘要:线程池代码中存在一段结构体初始化的设计: /* all tasks in the linked list recalled function for task */ typedef struct worker { void *(*process) (void *arg); /*recalled fu
阅读全文
线程池的部分原理研究2
摘要:基于上一个博客的研究,接下来针对具体的代码进行分析。 程序设计流程: 线程池初始化(n个线程) > 往线程池仍任务(n个任务) > 销毁线程池(n个线程) pool_init(int pnt) pool_add_worker pool_destroy() ( (void *(*process) (v
阅读全文
线程池的部分原理研究1
摘要:线程池的目的是基于缩短反复创建和销毁线程的时间,提高复用线程的效率,性能优化,如下图理解: 搬运0000四个任务量(做一个很粗略的解释) 单线程: 0000 4t 多线程: 0 00 0 3t(maybe) 线程池:00 00 2t 基本原理可以这样理解: 假设T1、T2、T3和T4任务,两个容器分
阅读全文
linux下串口测试程序
摘要:通过简单的参数配置,执行文件+串口号+波特率 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #inc
阅读全文
入门级的Makefile制作dynamic lib
摘要:代码文件结构: . ├── dynamiclib_add.c ├── dynamiclib_mul.c ├── dynamiclibs.h ├── libs └── Makefile 1 directory, 4 files libs作为一个存放动态库的目录 Makefile: #defualt G
阅读全文
SP接口的全双工首发接口整合
摘要:unsigned char bits = 8; unsigned int speed = 50000; unsigned short delay; static void spi_transfer_data(unsigned char *cmd, int len, unsigned char *da
阅读全文
代码实现简单数据转义
摘要:通常遇到,0xFE 0xFF 则转换为0xFE + 0x00 和 0xFE + 0x01。 Talk is cheap, show me the code. #include <stdio.h> #include <assert.h> #include <string.h> typedef unsi
阅读全文