c语言用法记录 宏、 printf %.*s 等

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

 

jemalloc在linux上从安装到使用

如果已经写好了 malloc,  编译的时候定义  -D JEMALLOC_MANGLE

并在根部头文件中加入

#include <jemalloc/jemalloc.h>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值