摘要: strncpy可以替代strcpy来防止缓冲区越界。但是如果还要考虑运行效率的话,也许strlcpy是一个更好的方式。1. strcpy我们知道,strcpy 是依据 /0 作为结束判断的,如果 to 的空间不够,则会引起 buffer overflow。strcpy 常规的实现代码如下(来自 OpenBSD 3.9):char *strcpy(char *to, const char *from){ char *save = to; for (; (*to = *from) != '/0'; ++from, ++to); return(save);}但通常,我们的 from 阅读全文
posted @ 2014-01-17 11:52 单调的幸福 阅读(224) 评论(0) 推荐(0) 编辑
摘要: 计算一个十进制数的位数 (int)log10((double)i) + 1#include #include unsigned int a[32000], s[32000];void table(){ int i; a[1] = 1; s[1] = 1; for(i = 2; i < 31270; i++) { a[i] = a[i-1] + (int)log10((double)i) + 1; //第i组数字的个数,120算是三个 s[i] = s[i-1] + a[i]; /... 阅读全文
posted @ 2014-01-17 11:26 单调的幸福 阅读(219) 评论(0) 推荐(0) 编辑