摘要: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];
阅读全文
摘要:goto语句可以直接完成跳转,在Linux内核代码中频繁出现。匹配上goto的代码块,最后需要加个return,不然会执行后面的代码块。 #include <stdio.h> int main() { printf("1\n"); goto case1; printf("2\n"); case1:
阅读全文
摘要:cilium 1.15.1 把单个mac拆分成2个整数,做减法比较。 #include <stdio.h> union macaddr { struct { __uint32_t p1; __uint16_t p2; }; __uint8_t addr[6]; }; static __always_
阅读全文
摘要:空指针获取首元素时出现段错误,子进程异常退出,父进程没有处理。 #include <stdio.h> #include <unistd.h> int main() { pid_t pid; pid = fork(); if (pid > 0) { printf("father process is
阅读全文
摘要:#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
阅读全文
摘要:父子进程不能共享全局变量。父子进程中的任何一方修改了全局变量,只是修改了副本,只对自己可见,对另一方不可见。C语言中即使加了static也不行。 #include <stdio.h> #include <unistd.h> // 初始值是0 int flag; int main() { pid_t
阅读全文
摘要:# 安装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
阅读全文
摘要:#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首地址
阅读全文
摘要:#include <stdio.h> #include <malloc.h> struct student { int age; }; struct data { int len; // 不占用空间 struct student students[0]; }; int main() { struct
阅读全文
摘要:#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
阅读全文
摘要:#ifndef _A_ #define _A_ 1 #endif
阅读全文
摘要:#include <stdio.h> #define __init __attribute__((constructor)) #define __exit __attribute__((destructor)) // 文件加载时初始化 void __init my_init(void) { prin
阅读全文
摘要:#include <stdio.h> #define LIST_HEAD_INIT(name) { &(name), &(name) } #define list_for_each_entry(obj, head, list) for (obj = (typeof(*obj) *)((char *)
阅读全文
摘要:方法一:引入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 相
阅读全文
摘要:#include <stdio.h> int main(int argc, char* argv[]) { int i; while (argc-- > 0) { printf("%s\n", *argv++); } return 0; }
阅读全文
摘要:#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
阅读全文
摘要:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <fcntl.h> #include <unistd.h> int main() { // 打开文件进行读写 int f
阅读全文
摘要:extern头文件定义变量或者函数默认是extern,在整个程序的所有源文件里都可以访问和修改。 static头文件static修饰变量后对其他源文件不可见并持久化,修饰函数后对其他源文件不可见。
阅读全文
摘要:union共存体中所有成员占用相同的内存空间。因为free函数参数是void *,常量指针是const void *,所以free函数释放常量指针时会因类型不同而失败。 #include <stdio.h> #include <malloc.h> #include <string.h> typede
阅读全文
摘要:头文件是string.h。根据传入的字符串参数,malloc分配空间并复制,返回首地址,该地址通过free来释放。 #include <stdio.h> #include <malloc.h> #include <string.h> int main() { char a[20] = "123";
阅读全文