
C 算法系列
I am 006!
code in China make for World
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
指向函数返回值与局部作用域
指向函数返回值与局部作用域原创 2023-04-09 16:33:12 · 150 阅读 · 0 评论 -
stderror_r有bug
stderror_r有bug原创 2023-03-27 16:35:51 · 132 阅读 · 0 评论 -
使用C语言实现接水问题
使用C语言实现接水问题原创 2022-12-15 14:11:43 · 712 阅读 · 0 评论 -
C语言实现 字符编码
用C语言实现字符串编码原创 2022-12-09 10:00:59 · 1474 阅读 · 0 评论 -
日历生成程序
使用C语言代码生成某年某月日历原创 2022-12-07 09:25:45 · 287 阅读 · 0 评论 -
多条件变量应用--传送与搬运问题
多条件变量应用--传送与搬运问题原创 2022-12-06 09:36:53 · 217 阅读 · 0 评论 -
scanf导致程序运行时出现stack smashing
栈溢出检测小结原创 2022-11-30 15:16:06 · 949 阅读 · 0 评论 -
勾股定理证明
建立在相似三角形 对应边比平方等于面积比 证明勾股定理原创 2022-11-29 16:57:21 · 224 阅读 · 0 评论 -
C语言实现享元模式
#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct _Template{ int title; int font; int lineDistance; void (*operate)(struct _Template *pTemplate);} Template;void operate(struct _Template *pTempla.原创 2022-04-21 11:23:51 · 748 阅读 · 0 评论 -
C语言实现桥接模式
#include <stdio.h>#include <stdlib.h>typedef struct _BodyBuilder{ void (*play)();} BodyBuilder;typedef struct _BailPlaying{ void (*play)();} BailPlaying;typedef struct _PlayRequest{ int type; void *pPlaying;} PlayRequ.原创 2022-04-20 17:01:47 · 1164 阅读 · 0 评论 -
C语言实现抽象工厂模式
#include <stdio.h>#include <stdlib.h>typedef struct _Coffee{ void (*have_coffee)();} Coffee;typedef struct _Tea{ void (*have_tea)();} Tea;void have_black_coffee(){ printf("have black coffee!\n");}void have_white_coffee(.原创 2022-04-01 10:18:14 · 1137 阅读 · 0 评论 -
C语言实现工厂模式
#include <stdio.h>#include <stdlib.h>typedef struct _shoes{ int type; void (*produce_shoes)(struct _shoes *);}myShoes;void produce_leather_shoes(myShoes *pshoes) { if (NULL != pshoes) { printf("produce the leather.原创 2022-04-01 09:22:45 · 706 阅读 · 0 评论 -
C语言的单例模式
#include <stdio.h>#include <stdlib.h>#include <assert.h>#define MAKE_INSTANCE 1#define FREE_INSTANCE 0typedef struct { int age; char name[12];} record;static record * operate_instance(int flag) { static record *pReco.原创 2022-03-26 10:18:49 · 1310 阅读 · 0 评论 -
使用typedef定义const 指针
#include <stdio.h>typedef char * PSTR;int main() { char string[4] = "abc"; const char *p1 = string; const PSTR p2 = string; p1++; //p2++; // 编译出错 *p2 = 'd'; printf("%s\n", string); return 0;}...原创 2022-03-02 13:44:59 · 250 阅读 · 0 评论 -
bit结构体好例子
#include <stdio.h>struct bit1 { unsigned a:20; unsigned b:6; unsigned c:2;}test_bit1;struct bit2 { unsigned d:20; unsigned e:6; unsigned f:20;}test_bit2;union UN { unsigned int u; struct { unsigned char a.原创 2022-03-01 11:31:33 · 454 阅读 · 0 评论 -
C语言函数表
#include <stdio.h>#include <string.h>int fun1() { return 1;}int fun2() { return 2;}int fun3() { return 3;}int fun4() { return 4;}struct { char *name; int (*func)();}symtab[] = { "fun1", fun1, "fun2",.原创 2022-02-22 16:14:34 · 1155 阅读 · 0 评论 -
将结构体成员从小到大排列可节省空间
#include <stdio.h>#include <string.h>#define PRINT_SZ(intValue) printf(#intValue" is %zd\n", (intValue));#define STRUCT_MEMBER_OFFSET(type, member) ((char *)&((type *)0)->member - (char *)0)/*struct Test { char a; short b;.原创 2022-02-18 15:43:19 · 680 阅读 · 0 评论 -
函数指针表
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>double my_add(double x, double y) { return x + y;}double my_sub(double x, double y) { return x - y;}double my_mul(double x, double y) { re.原创 2022-02-14 14:15:14 · 670 阅读 · 0 评论 -
神奇三维数组定义
原创 2022-02-07 16:06:05 · 247 阅读 · 0 评论 -
在C语言main函数之前和之后做些事情
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>__attribute((constructor)) void before_main(){ printf("%s\n", __FUNCTION__);}__attribute((constructor)) void after_main(){ printf("%s\n", __.原创 2022-01-21 15:03:51 · 414 阅读 · 0 评论 -
const无法深入typedef定义的内容
#include <stdio.h>int main() { typedef char *charp; // charp是一种类型即char * const charp p = "123"; // const仅仅修饰p 这行代码与const int p = 123;类似 都表明p是一个常量 *p = '1'; // 无编译错误 说明上述代码确实不能达到const char *p = "123"的效果 但运行会出现段错误 因为它企图改.原创 2021-12-22 14:45:08 · 217 阅读 · 0 评论 -
使用const char **绕过编译器修改const char
#include <stdio.h>int main() { const char c = 'X'; // c为常量 不能修改 char *p1; // 危险的未赋值的悬挂指针 const char **p2 = &p1; // 会有编译警告 因为=两边指针类型不同 *p2 = &c; // 修改p2指向的地址 这导致p1被修改为&c *p1 = 'Y'; .原创 2021-12-22 14:31:32 · 339 阅读 · 0 评论 -
使用__LINE__,__FILE__调式代码
#include <stdio.h>#define DEBUG printf("file:%s, line:%d", __FILE__, __LINE__), printfint main() { DEBUG("X is %d\n", 100); // 宏替换后("X is %d\n", 100);会写在printf最后 return 0;}原创 2021-12-20 11:23:43 · 165 阅读 · 0 评论 -
realloc引发的内存泄漏
int main() { char *p = (char *)malloc(10); char *newp = (char *)realloc(p, 20); // realloc不能保证newp与p指向同一块内存 if (newp) { p = newp; } free(p); return 0;}上述代码,如果注释4~6行将引发内存泄漏,free只释放了p生成的内存空间,而realloc生成的内存没有释放。...原创 2021-11-29 19:47:47 · 679 阅读 · 0 评论 -
指针数组参数传递不当导致安全问题
#define ROW 2#define COL 3void fun(int **p) { int i = 0; int j = 0; for (;i < ROW;i++) { for (j = 0;j < COL;j++) { printf("%d\n", p[i][j]); } }}int main() { int a[ROW][COL] = { { 1, 2, 3}, { 4, 5, .原创 2021-11-17 11:07:45 · 376 阅读 · 0 评论 -
两种创建二维动态数组效率对比
#include <stdio.h>#include <stdlib.h>#include <string.h>// fun1使用方式1创建n行m列动态二维整数数组void fun1(int n, int m) { int **array1 = (int **)malloc(n * sizeof(int *)); int i = 0; int j = 0; for (;i < n;i++) { array1[.原创 2021-11-15 15:03:24 · 270 阅读 · 0 评论 -
令人混淆的数组指针加法运算
int a[3] = { 1, 2, 3 };int (*p)[3] = &a; // 赋值正确,p保存指向a的指针,是一个二维指针p++; // 指针跑飞*((*p) + 1) = 10; // stack smashingint b[2][3] = { { 1, 2, 3 }, { 1, 2, 3 } };int (*p1)[3] = b; // 赋值正确,p1保存指向b的指针,.原创 2021-11-11 11:14:59 · 169 阅读 · 0 评论 -
snort检测插件初始化流程
一 检测插件概述1.代码位置:snort源码src/detection-plugins2.作用:snort规则体可定义关键词,如对于ICMP协议的itype,icode等,这些关键词及其参数都会被检测插件解析,并调用检测插件相关函数进行处理。如在规则体定义itype:3,那么检测插件会根据输入分组packet结构的icmp协议的type和规则里itype进行比较操作。二 检测插件Setup每个检测插件都有Setup函数,例如sp_icmp_type_check.c中的SetupIcmpTyp原创 2021-11-11 10:30:01 · 286 阅读 · 0 评论 -
三元表达式“?:“赋值
int a = 1, b = 0;// (a > b ? a : b) = 100; // 编译错误 ?:不能作为左值*(a > b ? &a : &b) = 100; // 正确原创 2021-10-30 09:34:54 · 698 阅读 · 0 评论 -
适合分数的计数排序可以秒数快速排序
#include <iostream>#include <algorithm>#include "count_sort.hpp"int main() { static const int LEN = 10; int loop = 10000000; for (int i = 0;i < loop;i++) { int array[LEN] = { 1, 0, 1, 2, 4, 10, 11, 13, 2, 5 }; .原创 2021-04-01 17:01:39 · 222 阅读 · 0 评论 -
snort会话控制块scb
一 会话控制块session control block含义 scb是snort用来保存一次完整会话信息,包括会话Key,会话首次时间,会话最终时间,会话过期时间,ttl信息,snort策略ID,服务器端IP,客户端IP,服务端通信端口,客户端通信端口,协议,MAC地址,客户端请求字节数,服务端响应字节数,会话ID,客户端请求包数,服务端响应包数等,其定义在snort源码preprocessors/Session/session_common.h:typedef struct _Sessio..原创 2021-03-20 16:04:56 · 524 阅读 · 0 评论 -
“struct sockaddr_un“unix socket ipc通信
1.socket ipc通信优势:不需要经过网络协议栈;不需要打包拆包;不需要计算校验和;不需要维护序号和应答等;2.sockaddr_un结构:#define UNIX_PATH_MAX 108 struct sockaddr_un { sa_family_t sun_family; /* AF_UNIX */ char sun_path[UNIX_PATH_MAX]; /* pathname */};3.用法:服务端与客户端通过本地socket文件进行通原创 2020-12-24 15:55:52 · 754 阅读 · 1 评论 -
pcap_dispatch与pcap_loop使用区别
1.pcap_dispatch和pcap_loop使用回调函数结构都一样:static void process_packet(u_char *user, struct pcap_pkthdr *pHeadr, u_char *pkt_data) { // 如果process_packet定义在类中,那么必须以static修饰}2.pcap_dispatch和pcap_loop函数参数都一样:int pkts_read = -1; // 让libpcap一直抓包,如果pkts原创 2020-10-17 10:32:43 · 4914 阅读 · 0 评论 -
linux C原子加操作
gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作。可以对1,2,4或8字节长度的数值类型或指针进行原子操作,其声明如下:type __sync_fetch_and_add (type *ptr, type value, ...)type __sync_fetch_and_sub (type *ptr, type value, ...)type __sync_fetch_and_or (type *ptr, type value, ...)typ原创 2020-10-13 17:16:39 · 1576 阅读 · 1 评论 -
leetcode有效括号
#include <iostream>#include <string>#include <stack>using namespace std;class Solution {public: bool isValid(string s) { int i = 0; int size = s.size(); char ch; char left_bracket; std::st.原创 2020-07-29 16:00:47 · 120 阅读 · 0 评论 -
mac地址转换
#pragma oncetemplate <class T>class single_instance {public: static inline T instance() { static T obj; return obj; }private: single_instance() = default; virtual ~single_instance() = default;};#pragma once#i.原创 2020-06-29 20:35:04 · 2089 阅读 · 0 评论 -
leetcode pow(x,n)
#include <iostream>class Solution {public: double myPow(double x, int n) { if (fabs(x - 1.0) < 0.00000001) { return 1; } if (fabs(x + 1.0) < 0.00000001) { return n % 2 ? -1 : 1; } .原创 2020-06-21 20:33:28 · 184 阅读 · 0 评论 -
一次弱智而又隐藏的故障
原创 2020-06-11 20:56:04 · 211 阅读 · 0 评论 -
leetcode还原二叉搜索树
#include <stdio.h>#include <iostream>#include <algorithm>struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};TreeNode *root;class Solution {public:.原创 2020-06-04 15:00:44 · 183 阅读 · 0 评论 -
libcap应用:按协议抓取网络包
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <sys/time.h>#include <stdint.h>#include <string.h>#include <sys/socket.h>#include <n...原创 2020-03-22 15:33:41 · 571 阅读 · 0 评论