printf %.*s
小数点.后“*”表示输出位数,超出部分将被截去
printf("%.5s\n", "1234567890"); 输出 12345
printf("%.*s\n", 4, "1234567890"); 输出 1234
alloca
在栈上分配空间,函数结束后自动释放,不用也不能调用 free
int* a = alloca(sizeof(int)*n);
宏
#define ColorTable\
COLOR(BackgroundColor, RGB(0,0,0), "背景颜色")\
COLOR(TitleColor, RGB(100,0,0), "标题颜色")\
COLOR(TextColor, RGB(0,100,0), "文本颜色")
/*
下面的代码 我便定义了
stringBackgroundColor ="BackgroundColor";
string TitleColor ="TitleColor";
string TextColor ="TextColor";
*/
#define COLOR(key, val, desc) string key = #key;
ColorTable
#undef COLOR
/*
我把颜色保存到 map 中
m["BackgroundColor"] =RGB(0,0,0);
m["TitleColor"] =RGB(100,0,0);
m["TextColor"] =RGB(0,100,0);
*/
#define COLOR(key, val, desc) m[key] = val;
ColorTable
#undef COLOR
打印16进制
static bool to_hexchar(const uint8_t* from, size_t n, char* to, size_t to_n)
{
if (n*2 > to_n)
return false;
for (size_t i = 0; i < n; ++i, to += 2)
snprintf(to, 3, "%02x", from[i]);
return true;
}
// print_hexchar(buf, buflen, "", "");
static void print_hexchar(const uint8_t* p, uint32_t n, const char* head, const char* tail)
{
size_t sz=2*n+1;
char* hexchar = malloc(sz);
bzero(hexchar, sz);
to_hexchar(p, n, hexchar, sz);
printf("%s%s%s", head, hexchar, tail);
free(hexchar);
}
const char* to_hex(char* s, uint32_t u32) {
int i;
static char hex[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
for (i=0;i<8;i++) {
s[i] = hex[(u32 >> ((7-i) * 4))&0xf];
}
s[i] = '\0';
return s;
}
安装动态库,如果还是提示找不到
sudo ldconfig
查看程序动态库
readelf -d a.out
如果已经写好了 malloc, 编译的时候定义 -D JEMALLOC_MANGLE
并在根部头文件中加入
#include <jemalloc/jemalloc.h>