随笔分类 -  C/C++

1 2 下一页

C语言 实现BKDR hash算法
摘要:BKDR hash算法用于计算字符串的hash值。 #include <stdio.h> unsigned long long BKDRHash(char *str) { unsigned long long hash = 0, seed = 31; for (int i = 0; str[i]; 阅读全文

posted @ 2025-01-15 08:46 王景迁 阅读(28) 评论(0) 推荐(0)

C语言 使用goto语句
摘要:goto语句可以直接完成跳转,在Linux内核代码中频繁出现。匹配上goto的代码块,最后需要加个return,不然会执行后面的代码块。 #include <stdio.h> int main() { printf("1\n"); goto case1; printf("2\n"); case1: 阅读全文

posted @ 2024-09-20 08:28 王景迁 阅读(38) 评论(0) 推荐(0)

C语言 比较mac
摘要:cilium 1.15.1 把单个mac拆分成2个整数,做减法比较。 #include <stdio.h> union macaddr { struct { __uint32_t p1; __uint16_t p2; }; __uint8_t addr[6]; }; static __always_ 阅读全文

posted @ 2024-06-08 11:22 王景迁 阅读(43) 评论(0) 推荐(0)

C语言 子进程段错误后变成僵尸进程
摘要:空指针获取首元素时出现段错误,子进程异常退出,父进程没有处理。 #include <stdio.h> #include <unistd.h> int main() { pid_t pid; pid = fork(); if (pid > 0) { printf("father process is 阅读全文

posted @ 2024-05-05 15:05 王景迁 阅读(32) 评论(0) 推荐(0)

C语言 根据子串切割字符串
摘要:#include <stdio.h> #include <string.h> int main() { char url[2][20]; if (sscanf("https://www.baidu.com", "%[^//]//%s", url[0], url[1]) == -1) { printf 阅读全文

posted @ 2024-05-05 10:47 王景迁 阅读(18) 评论(0) 推荐(0)

C语言 父子进程不能共享全局变量
摘要:父子进程不能共享全局变量。父子进程中的任何一方修改了全局变量,只是修改了副本,只对自己可见,对另一方不可见。C语言中即使加了static也不行。 #include <stdio.h> #include <unistd.h> // 初始值是0 int flag; int main() { pid_t 阅读全文

posted @ 2024-05-05 10:10 王景迁 阅读(157) 评论(0) 推荐(0)

C++类指针未初始化导致访问成员变量时报段错误
摘要:# 安装gcc和g++ yum install gcc yum install gcc-c++.x86_64 // a.cpp #include <iostream> #include <unistd.h> using namespace std; class Test { public: void 阅读全文

posted @ 2024-01-28 10:04 王景迁 阅读(105) 评论(0) 推荐(0)

C语言 指针偏移量计算
摘要:#include <stdio.h> struct s { int a; int b; }; int main() { struct s s1; // (struct s*)0表示0x0作为struct s首地址 // &((struct s*)0)->a代表a地址 // 因为struct s首地址 阅读全文

posted @ 2024-01-11 08:36 王景迁 阅读(229) 评论(0) 推荐(0)

C语言 不定长数组
摘要:#include <stdio.h> #include <malloc.h> struct student { int age; }; struct data { int len; // 不占用空间 struct student students[0]; }; int main() { struct 阅读全文

posted @ 2023-12-19 21:15 王景迁 阅读(112) 评论(0) 推荐(0)

C语言 函数数组
摘要:#include <stdio.h> // float代表函数返回值 // my_func_name代表函数地址 // int代表函数参数 typedef float (*my_func_name)(int); float a(int i) { return 1.0 + i; } float b(i 阅读全文

posted @ 2023-12-19 20:42 王景迁 阅读(23) 评论(0) 推荐(0)

C语言 避免重复定义宏
摘要:#ifndef _A_ #define _A_ 1 #endif 阅读全文

posted @ 2023-12-14 13:15 王景迁 阅读(57) 评论(0) 推荐(0)

C语言 init和exit
摘要:#include <stdio.h> #define __init __attribute__((constructor)) #define __exit __attribute__((destructor)) // 文件加载时初始化 void __init my_init(void) { prin 阅读全文

posted @ 2023-12-13 08:58 王景迁 阅读(34) 评论(0) 推荐(0)

C语言 实现双向链表
摘要:#include <stdio.h> #define LIST_HEAD_INIT(name) { &(name), &(name) } #define list_for_each_entry(obj, head, list) for (obj = (typeof(*obj) *)((char *) 阅读全文

posted @ 2023-12-12 22:13 王景迁 阅读(30) 评论(0) 推荐(0)

C语言 使用bool
摘要:方法一:引入stdbool.h #include <stdio.h> #include <stdbool.h> int main() { bool f = false; if (!f) { printf("f is false\n"); } return 0; } 输出结果是f is false 相 阅读全文

posted @ 2023-12-11 22:02 王景迁 阅读(123) 评论(0) 推荐(0)

C语言 命令行接收参数
摘要:#include <stdio.h> int main(int argc, char* argv[]) { int i; while (argc-- > 0) { printf("%s\n", *argv++); } return 0; } 阅读全文

posted @ 2023-12-09 15:20 王景迁 阅读(35) 评论(0) 推荐(0)

C语言 主机字节序和网络字节序互换
摘要:#include <stdio.h> #include <arpa/inet.h> int main() { // 10.11.12.13 uint32_t host_ip = 168496141; uint32_t network_ip = htonl(168496141); // 13.12.1 阅读全文

posted @ 2023-11-29 21:18 王景迁 阅读(47) 评论(0) 推荐(0)

C语言 mmap完成文件读写
摘要:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <fcntl.h> #include <unistd.h> int main() { // 打开文件进行读写 int f 阅读全文

posted @ 2023-10-18 21:47 王景迁 阅读(176) 评论(0) 推荐(0)

C语言 static和extern
摘要:extern头文件定义变量或者函数默认是extern,在整个程序的所有源文件里都可以访问和修改。 static头文件static修饰变量后对其他源文件不可见并持久化,修饰函数后对其他源文件不可见。 阅读全文

posted @ 2023-10-17 22:00 王景迁 阅读(16) 评论(0) 推荐(0)

C语言 通过union共存体释放常量指针指向的堆空间
摘要:union共存体中所有成员占用相同的内存空间。因为free函数参数是void *,常量指针是const void *,所以free函数释放常量指针时会因类型不同而失败。 #include <stdio.h> #include <malloc.h> #include <string.h> typede 阅读全文

posted @ 2023-10-15 10:31 王景迁 阅读(55) 评论(0) 推荐(0)

C语言 strdup函数把字符串复制到新空间
摘要:头文件是string.h。根据传入的字符串参数,malloc分配空间并复制,返回首地址,该地址通过free来释放。 #include <stdio.h> #include <malloc.h> #include <string.h> int main() { char a[20] = "123"; 阅读全文

posted @ 2023-10-15 10:22 王景迁 阅读(33) 评论(0) 推荐(0)

1 2 下一页

导航