设计模式
I am 006!
code in China make for World
展开
-
qnx环境下编译ffmpeg及解码mp4实践
qnx环境下编译ffmpeg及解码mp4实践原创 2022-10-26 09:23:06 · 1570 阅读 · 0 评论 -
qnx ipc实践
qnx ipc简介原创 2022-10-24 09:31:25 · 627 阅读 · 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 · 605 阅读 · 0 评论 -
联合体成员标识
#include <stdio.h>struct XXX { enum { UNKNOWN, INT, CHAR, DOUBLE } code; union { int a; char b; double c; } uu;};int main() { struct XXX xx; xx.code = INT; xx.uu..原创 2021-10-23 17:30:39 · 96 阅读 · 0 评论 -
计算结构体成员在结构体中的偏移量
一 方式1:采用offsetof宏#include <stdio.h>#include <stddef.h>struct PP { char a; int b;};int main() { struct PP pp; printf("%zd\n", offsetof(struct PP, b)); // 输出4 return 0;}以上方法是调用系统宏offsetof,此宏是被定义在头文件stddef.h中,需要注原创 2021-10-22 10:02:48 · 365 阅读 · 0 评论 -
字符串分配旁门左道
一 示例代码#include <stdio.h>#include <stdlib.h>#include <string.h>struct name { int name_len; char name_val[1];};struct name *make_name(const char *new_name) { // 分配name并且除去name_val所占字节 在加上'\0'所占字节 int len = strlen(new原创 2021-10-20 14:41:22 · 96 阅读 · 0 评论 -
C++11模板参数类型推导
#include <typeinfo>#include <iostream>template <typename T>void f_ref(T &arg) { std::cout << typeid(arg).name() << std::endl;}template <typename T>void f_pointer(T *arg) { std::cout << typeid(arg.原创 2021-10-14 15:43:07 · 343 阅读 · 0 评论 -
c++17区域锁std::scoped_lock应用实例
一 std::scoped_lock简介template< class... MutexTypes >class scoped_lock;(1)将多个锁(std::mutex等)包装成一种锁类型,用于线程一次性申请多个锁,避免死锁。(2)当程序出现异常,可自动析构,完成锁的是否。二 实例#include <iostream>#include <string>#include <vector>#include <mutex&原创 2021-08-07 11:09:45 · 4156 阅读 · 0 评论 -
异步调用std::async实现并行计算
一 std::asynctemplate< class Function, class... Args>std::future<std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>> async( Function&& f, Args&&... args );std::async返回一个future对象,可通过g...原创 2021-07-28 14:47:59 · 392 阅读 · 0 评论 -
利用std::is_invocable在编译期间判断函数与传递参数是否匹配
一 可构造性判断std::is_invocabletemplate <class T, class... Args> struct is_constructible;T是待检查的类型,Args为可变参数例如:struct A { A (int,int) { };}; std::cout << "A(int): " << std::is_constructible<A ,int>::value <<原创 2021-07-13 09:02:39 · 2044 阅读 · 0 评论 -
leetcode 旋转图像
https://leetcode-cn.com/problems/rotate-image/#include <iostream>#include <vector>using namespace std;class Solution {public: void rotate(vector<vector<int>>& matrix) { int i = 0, j = 0; int n = matrix原创 2021-07-07 17:09:46 · 51 阅读 · 0 评论 -
安全拷贝函数safe_memcpy实现分析
一 memcpy缺陷分析1.memcpy原型:#include <string.h>void *memcpy(void *dest, const void *src, size_t n); #函数返回dest指针2.缺陷分析:(1)memcpy容易发生缓冲区溢出,如果dest长度小于待拷贝的字节数n,那么程序将发生缓冲区溢出;(2)如果dest是高地址,src是低地址,拷贝可能发生错误,即源地址和目的地址重合部分在拷贝时被修改。...原创 2021-05-28 16:18:56 · 2216 阅读 · 2 评论 -
代码绑核后调用system占用CPU分析
一 system函数分析1.函数原型:int system(const char *command)参数command一般是一条可被/bin/sh -c执行的字符串,可以是"ls -l"这样的shell命令,也可以是"./Test"这样的二进制路径。如上图所示:(1)某个进程调用system时,就是fork一个子进程,让子进程调用execl执行命令;(2)父进程一直会等子进程执行结束,这个与popen不同;(3)子进程会继续父进程一些属性,如果父进程使用绑核操作,那么sys原创 2021-05-15 15:20:16 · 345 阅读 · 0 评论 -
boost创建scoped_thread
一 概述scoped_thread创建的线程在作用域范围外自动join,不需要手动写join函数,如下代码展示了使用scoped_thread创建线程,并使用bind关联线程函数和相关参数,向文件写入一些字符。二 代码#include <fstream>#include <iterator>#include <algorithm>#include <boost/thread.hpp>#include <boost/thread/sc原创 2021-05-08 16:27:13 · 235 阅读 · 0 评论 -
使用字节拷贝进行转换--bit_cast
一 概述bit_cast是C++20支持的按字节进行转换的方法,如:auto a = std::bit_cast<std::array<char, sizeof(p)>>(p);二 代码分析1.bit_cast.hpp:#pragma once#include <cstring>#include <type_traits>template <class To, class From>typename std::ena原创 2021-04-12 17:00:07 · 1183 阅读 · 0 评论 -
leetcode x的平方根
#include <cmath>#include <iostream>class Solution {public: int mySqrt(int x) { if (x <= 0) { return 0; } return int(sqrt(x)); }};int main() { Solution ss; std::cout << ss.mySqrt.原创 2021-03-08 14:11:58 · 40 阅读 · 0 评论 -
syslog异步发送客户端
https://github.com/wangzhicheng2013/syslog_client原创 2021-02-28 19:53:10 · 192 阅读 · 0 评论 -
使用libpcap解析pcap和pcapng文件
https://github.com/wangzhicheng2013/pcapng_file_process原创 2021-02-06 09:25:52 · 7712 阅读 · 0 评论 -
leetcode两两交换链表节点
#include <iostream>struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x), next(next) {}};class Solution {public: .原创 2021-01-03 18:55:44 · 68 阅读 · 0 评论 -
p0f客户端
https://github.com/wangzhicheng2013/p0f_client原创 2020-12-31 15:42:35 · 167 阅读 · 0 评论 -
ipc socket通信
https://github.com/wangzhicheng2013/socket_ipc原创 2020-12-25 14:39:29 · 182 阅读 · 0 评论 -
环形共享内存队列
https://github.com/wangzhicheng2013/shared_memory_ring_queue原创 2020-11-28 20:01:45 · 391 阅读 · 0 评论 -
mysql客户端
https://github.com/wangzhicheng2013/mysql_client原创 2020-10-29 19:26:51 · 66 阅读 · 0 评论 -
进程锁
https://github.com/wangzhicheng2013/process_mutex原创 2020-10-26 15:13:16 · 337 阅读 · 0 评论 -
kafka客户端
https://github.com/wangzhicheng2013/kafka_client/blob/main/kafka_client.hpp原创 2020-10-23 14:28:37 · 140 阅读 · 0 评论 -
基于libpcap多线程抓包引擎
https://github.com/wangzhicheng2013/libpcap_pcap_engine原创 2020-10-16 19:35:44 · 849 阅读 · 1 评论 -
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 · 1285 阅读 · 1 评论 -
leetcode 环形链表 II
https://github.com/wangzhicheng2013/leetcode原创 2020-10-11 19:39:11 · 52 阅读 · 0 评论 -
ntp客户端
https://github.com/wangzhicheng2013/ntp-client原创 2020-09-21 15:50:34 · 129 阅读 · 0 评论 -
消息分发器模式
https://github.com/wangzhicheng2013/message_dispatch原创 2020-09-09 20:34:30 · 864 阅读 · 0 评论 -
线程类
https://github.com/wangzhicheng2013/thread_class原创 2020-09-09 14:55:51 · 78 阅读 · 0 评论 -
threadpool
https://github.com/wangzhicheng2013/threadpool原创 2020-09-09 11:00:07 · 76 阅读 · 0 评论 -
json工具类
https://github.com/wangzhicheng2013/json_tools原创 2020-09-07 16:59:34 · 58 阅读 · 0 评论 -
log4cxx封装类
https://github.com/wangzhicheng2013/log_system/tree/master/log4cxx原创 2020-09-07 09:53:09 · 207 阅读 · 0 评论 -
libevent封装的https server
https://github.com/wangzhicheng2013/libevent_https_server原创 2020-08-29 11:06:20 · 795 阅读 · 0 评论 -
leetcode--删除链表的倒数第N个节点
#include <iostream>#include <vector>struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) { }};class Solution {public: ListNode *removeNthFromEnd(ListNode *head, int n) { nodes_addr.原创 2020-08-06 15:54:06 · 63 阅读 · 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 · 120 阅读 · 0 评论 -
获取CPU型号
#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-14 18:05:49 · 257 阅读 · 0 评论 -
一次弱智而又隐藏的故障
原创 2020-06-11 20:56:04 · 157 阅读 · 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 · 123 阅读 · 0 评论