Uncommonly used Linux/C functions/system interfaces

1. __builtin_popcount

	int __builtin_popcount(unsigned int n)
	int __builtin_popcountl(unsigned long n)
	int __builtin_popcountll(unsigned long long n)

Enter the number n, the number of '1' in the binary representation.

int main() {
    
    
    int num = 3;
    cout << "how many 1s in num: " << __builtin_popcount(num) << endl;

    return 0;
}
// how many 1s in num: 2

2. gcc __builtin_ function

1) gcc provides a lot of built-in functions starting with __builtin_, which are used to handle exceptions, variable-length parameter lists, and system optimization. The first two types are not public.
2) The __builtin_ function is divided into two categories:
(1) The __builtin_ function without the corresponding method of the C standard library, this type of method is always expanded inline, has no entry address, and can only be used in functions (cannot be used in expressions , inline expansion would result in a compilation error).
(2) There is a __builtin_ function corresponding to the C standard library method, and the built-in function and the C library function have exactly the same prototype and entry address. For example: __builtin_printf and printf.
If the built-in function is turned off by the compilation parameter (-fno-builtin), the method passed for compilation at this time: #define strcpy(d, s) __builtin_strcpy ((d), (s))

3. asprintf, vasprintf

	int asprintf(char **strp, const char *fmt, ...);
	int vasprintf(char **strp, const char *fmt, va_list ap);

The function is similar to sprintf and vsprintf, which will allocate enough memory for the string and return the newly allocated memory through the first parameter strp.
The content pointed to by strp needs to be released with the free() function.

4. syscall function

See also: Calling the system interface with the syscall() method on Linux

Guess you like

Origin blog.csdn.net/yinminsumeng/article/details/131168172