![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c
c
hfyinsdu
烂笔头--好记性不如烂笔头
展开
-
_Generic keyword in C
A major drawback of Macro in C/C++ is that the arguments are strongly typed checked i.e. a macro can operate on different types of variables(like char, int ,double,..) without type checking. /...转载 2020-04-24 19:14:48 · 139 阅读 · 0 评论 -
Design a stack that supports getMin()/push/pop in O(1) time
See blog.will figure out why later.转载 2020-04-03 09:25:17 · 144 阅读 · 0 评论 -
swap(a,b)
1. method 1#include <stdio.h> void swap(int* xp, int* yp) { if (xp == yp) // Check if the two addresses are same return; *xp = *xp + *yp; *yp = *xp - *yp; *xp = *xp - *yp; } in...转载 2020-03-30 11:32:40 · 682 阅读 · 0 评论 -
max(a, b) and more
Summary from link1 and link2.The “maximum” function is commonly defined as a macro in standard C as follows:#define max(a,b) ((a) > (b) ? (a) : (b)) But this definition computes either a or...原创 2020-03-30 09:59:27 · 294 阅读 · 0 评论 -
container_of()
Add comments to container_of() wiki to understand its implementation.the Linux kernel uses offsetof() to implement container_of(), which allows something like a mixin type to find the structure that...原创 2020-03-30 08:46:31 · 176 阅读 · 0 评论 -
reentrant & thread safe with examples
Wiki says, In computing, a computer program or subroutine is called reentrant if multiple invocations can safely run concurrently. The concept applies even on a single processor system, where a reentr...原创 2020-03-29 17:31:45 · 108 阅读 · 0 评论 -
offsetof()
Add comments to offsetof() wiki to understand its implementation.The "traditional" implementation of the macro relied on the compiler obtaining the offset of a member by specifying a hypothetical st...原创 2020-03-29 15:46:07 · 228 阅读 · 0 评论 -
运算符优先级和结合性
这篇文章的表总结的很好。这里大概总结一下,便于记忆。运算符的优先级基本符合常识,除了下面5里面的按位运算符,优先级从高到低依次是:1. 取struct 成员的, 后缀++/--, 和函数调用(),例如(. / -> / [])2. 一元操作符, 前缀++/--,例如(+ / - / ! / ~ / * / & / (type) / sizeof )3. 运算符(* /...原创 2020-03-29 11:02:57 · 191 阅读 · 0 评论 -
Type conversions
All come from section 2.7 of the c programming language 2nd edition. BTW, this book is highly recommended for learning C, it's simple and illustrates the very important aspects of C.When an operator...原创 2020-03-29 09:50:34 · 138 阅读 · 0 评论 -
程序输出
1. copy from blogmain(){ int a[5]={1,2,3,4,5}; int *ptr=(int *)(&a+1); printf("%d,%d",*(a+1),*(ptr-1));}输出:2,5*(a+1)就是a[1],*(ptr-1)就是a[4],执行结果是2,5&a+1不是首地址+1,系统会认为加一个a数组的偏移...转载 2020-03-28 16:05:12 · 328 阅读 · 0 评论 -
static variableS with same name
1. 多个c文件,每个文件都包含static int a;2. 一个c文件中,有一个全局static int a;,在某个函数中,也有一个相同定义static int a;这两种情况都是合法的,另外这些变量a都是存放在同一个data段,那么C怎么区分各个变量的呢?Since there can be as many function-local statics with the sam...原创 2020-03-28 15:46:05 · 104 阅读 · 0 评论 -
size_t
According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bit (see sections 7.17 and 7.18.3).size_t 能存储理论上可行的任何类型(包括数组)对象的最大大小。other articles1. https://www.em...原创 2020-03-28 15:14:10 · 150 阅读 · 0 评论 -
malloc(0) & free(NULL)
man malloc / man freeThe malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or...原创 2020-03-28 15:07:38 · 326 阅读 · 0 评论 -
c 常见问题
1. 用预处理指令#define声明一个常数,用以表明1年中有多少秒(忽略闰年问题)#define SEC_YEAR(365*24*60*60UL) 1)C中数据的默认匹配类型顺序(int,long,long long),都不是unsighed;2)int类型在标准中定义为最小16位,long最小32位,所以如果用signged数据类型表示这个常数,会放不下,所以需要使用unsigned...原创 2020-03-28 10:22:07 · 102 阅读 · 0 评论 -
What is the type of a constant in C? (U, UL, ULL, L, F)
Copied from here.When we write expressions like this in C:bool b = 1234567890 > 09876;What are the types of those constants? The number 1234567890 - what is its type? How does C represent i...转载 2020-03-28 09:56:34 · 397 阅读 · 0 评论 -
c struct with array of zero length (1 length or empty length in C99)
details see stackoverflow.struct s { int n; double d[];};int m = /* some value */;struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));This is a way to have variable sizes of...转载 2020-03-28 09:06:39 · 105 阅读 · 0 评论 -
c struct bitfield (结构体位域)
两篇非常棒的文章。1. 再谈c语言位域2. Section bitfields简要总结一下:C99 guarentees that bit-fields will be packed as tightly as possible, provided they don’t cross storage unit boundariesNote that the base type o...原创 2020-03-27 18:00:57 · 649 阅读 · 0 评论 -
why not 'switch case' accept float point in condition?
A switch case example as below. The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed. Why?int main() { int x = 2; switch (...转载 2020-03-23 15:57:45 · 107 阅读 · 0 评论 -
c sort function in library: qsort()
目录qsort() in C libraryexamplereferenceqsort() in C libraryUsually sort is the first step for an algorithm. In C, there is one library function for sort: qsort.It's prototype and introducti...原创 2020-03-21 08:44:52 · 137 阅读 · 0 评论 -
c volatile keyword and memory cache
The 'volatile' keyword is used to tells the compiler to fetch a variable each time from memory rather then use a "compiler-generated" cached version in registers or optimise certain code (like entirel...原创 2020-03-07 17:36:05 · 135 阅读 · 0 评论 -
ud2: illegal opcode, Undefined Instruction
Following is xv6 (rev11) chapter2 exercise 5, Modify xv6 so that when a user program dereferences a null pointer, it will receive a fault. That is, modify xv6 so that virtual address 0 isn’t ma...原创 2020-03-05 12:26:44 · 596 阅读 · 0 评论 -
Shared Libraries: Understanding Dynamic Loading
questions:when a executable starts,1. how to resolve symbols in that are in shared library.2. can we upgrade/replace shared lib when executable is running? (can we upgrade executable when it is ...原创 2020-02-27 18:12:49 · 92 阅读 · 0 评论 -
C function implementation: memcpy and strcpy and others
An often being asked interview question. The key is to handle address overlap.void *memcpys(void *dest, const void *src, size_t n){ int i, l; void *dst; char *d, *s; d = dest; ...原创 2020-02-27 16:17:35 · 89 阅读 · 0 评论 -
'restrict' keyword
restrict is a keyword that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, only the pointer itself or a val...转载 2020-02-27 16:14:05 · 142 阅读 · 0 评论 -
how much malloc() allocate & how free() know how much to free & how much Linux kernel alloc
Most implementations of C memory allocation functions will store accounting information for each block, either in-line or separately.One typical way (in-line) is to actually allocate both a header a...转载 2020-02-27 15:36:13 · 180 阅读 · 0 评论 -
c data type size
copied from wiki:The actual size of the integer types varies by implementation. The standard requires only size relations between the data types and minimum sizes for each data type:The relation r...转载 2020-02-24 10:29:49 · 137 阅读 · 0 评论 -
gcc x86 calling conventions
在<<stacktrace如何实现>>的最后介绍stacktrace的时候,是x86 calling convention决定了ebp/esp(base pointer/stack pointer)的使用,通过ebp/esp得到backtrace. 下图是一个概况:gcc x86 calling convention也规定了参数如何传递1. 32bit规定...原创 2020-02-20 09:26:30 · 200 阅读 · 0 评论 -
printf output interleave
questionsWill following program's output interleave when OUT_STREAM is stdout? What about OUT_STREAM is stderr? why?#include <stdio.h>#include <stdlib.h>#include <sys/types.h>...原创 2020-01-16 14:01:41 · 347 阅读 · 0 评论 -
C Program Compilation Process
refer to for more details:https://medium.com/@earthtojhuang/gcc-main-c-9f6ee2e6894bhttps://www.geeksforgeeks.org/compiling-a-c-program-behind-the-scenes/转载 2020-01-20 16:35:28 · 150 阅读 · 0 评论 -
c variable-length argument lists
a simple program demostrate c variable-length argument lists, and how to refer variable-length argument in c macro.#include <stdio.h>#include <stdlib.h>#include <stdarg.h>//fm...原创 2020-01-21 15:23:01 · 165 阅读 · 0 评论 -
c list sample code
c list sample code. The list interface may varies depends on use case. It seems the last one is more popular. However, i sometimes use the first one.#include <stdio.h>#include <stdlib.h&g...原创 2020-01-21 15:25:58 · 131 阅读 · 0 评论