C
Headers
include <sys/*.h>
corresponding to /usr/include/x86_64-linux-gnu/sys/*.h
;
include <linux/*.h
corresponding to /usr/include/linux/*.h
What's the difference between them?
C initialization position
The declaration at the start of a { }
block is part of the C89 standard; the block doesn't have to be a function.
Variable declaration placement in C - Stack Overflow
Designated Initializer
This initializer is used when we want to initialize a range with the same value. This is used only with GCC compilers.
[first…last] = value;
int num[5]={ [0 . . . 4 ] = 3 }; // num = { 3, 3, 3, 3, 3}
We may also ignore the size of array:
int num[ ]={ [0 . . . 4 ] = 3 }; // num = { 3, 3, 3, 3, 3}
or not a range:
int foo[10] = { [3] = 1, [5] = 2 };
Here is an example in KVM:
/*
* The exit handlers return 1 if the exit was handled fully and guest execution
* may resume. Otherwise they set the kvm_run parameter to indicate what needs
* to be done to userspace and return 0.
*/
static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
[EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
[EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
// ...
[EXIT_REASON_NOTI1FY] = handle_notify,
};
An array of functions
void (*functions[256])();
Which is an array of function pointers. Note, however, that void func()
isn't a "function that takes no arguments and returns nothing." It is a function that takes unspecified numbers or types of arguments and returns nothing. If you want "no arguments" you need this:
void (*functions[256])(void);
What does dot (.) mean in a struct initializer?
c - What does dot (.) mean in a struct initializer? - Stack Overflow
These are C99's designated initializers.
Unsigned
unsigned
really is a shorthand for unsigned int
, and so defined in standard C.
c - what is the unsigned datatype? - Stack Overflow
Void pointer in C
The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer.
Opaque pointer name in C
from a dictionary definition: opaque: adjective; not able to be seen through; not transparent
Enum
enum tokens are global, for example:
enum State {
IDLE_ST,
START_ST,
RUNNING_ST,
STOPPED_ST,
};
enum State g_current_state = IDLE_ST;
here, you can use the IDLE_ST
directly rather than specifing the scope.
Pragma once
#pragma once
serves the same purpose as include guards, but with several advantages, including: less code, avoidance of name clashes, and sometimes improvement in compilation speed.
Break in curly brace
switch (foo) {
case 1: {
// stuff
break;
}
default: {
break;
}
}
switch (foo) {
case 1: {
// stuff
} break;
default: {
// stuff
} break;
}
// the above two are the SAME!!!
c - 'break' statement when using curly braces in switch-case - Stack Overflow
Three dots (ellipsis)?
case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
if (!vmx_pt_mode_is_host_guest() ||
(index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
PT_CAP_num_address_ranges)))
return 1;
if (index % 2)
msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
else
msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
break;
Extern
When we write extern some_data_type some_variable_name
; no memory is allocated. Only property of variable is announced.
Extern variable says to compiler "go outside my scope and you will find the definition of the variable that I declared."
When a function is declared or defined, the extern keyword is implicitly assumed.
When we declared/defined a function, we saw that the extern keyword was present implicitly. But this isn’t the case with variables. If it were, memory would never be allocated for them. Therefore, we need to include the extern keyword explicitly when we want to declare variables without defining them.
Understanding "extern" keyword in C - GeeksforGeeks
Macros
__percpu
Unsigned
Unsigned left shift
1 will be 10, not 11.
Unsigned plus
if either operand is unsigned, the other shall be converted to unsigned.
c++ - Why int plus uint returns uint? - Stack Overflow
Negative sign before an unsigned number
取反加一。