关于typedef和struct 参考回答:http://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c
remove trailing '' (fgets) buf[strcspn(buf, '')] = 0;
strdup & strdupa 比 malloc;strcpy 更简洁
extern IS OPTIONAL on function
uint32_t defined in stdint.h
size_t defined in stddef.h
strspn(const char s, const char accept); 找到s中全由accept中字符组成的连续长度
strtok split string
strpbrk - search a string for any of a set of bytes
goto label, label only have function scope
restrict c99 keyword, intend for compiler, its' programmer promise of elimiating pointer aliasing
libevent 不处理,只打印消息。
我觉得, 既然malloc失败了(也就是OOM),这个时候程序只能退出!区别在于退出的姿势,是主动退出呢?还是core掉。 从友好性来讲,应该主动退出; 但是core掉应该也是没有啥问题的吧。
不结合比较简单,出问题应该主要从log判断
======= * time
time_t - c standard did't specify the time_t, but almost implementations define it as int - seconds from epoch, it may fail on 2038. time_t time(time_t *arg);
clock_t - representing the processor time used by a process. clock_t clock(void); CLOCKS_PER_SEC
NOTE 统计进程使用的时间clock_gettime的精度更好 http://en.cppreference.com/w/c/chrono/clock
struct timespec { time_t tv_sec; /* seconds / long tv_nsec; / nanoseconds */ };
int clock_gettime(clockid_t clock_id, struct timespec *tp); CLOCK_PROCESS_CPUTIME_ID
int timespec_get( struct timespec ts, int base) #define TIME_UTC / implementation defined */
tm -> ascii char* asctime( const struct tm* time_ptr ); errno_t asctime_s(char buf, rsize_t bufsz, const struct tm time_ptr);
time_t -> tm struct tm localtime( const time_t time ); struct tm localtime_s(const time_t restrict time, struct tm *restrict result);
time_t -> ascii char* ctime( const time_t* time ); errno_t ctime_s(char buffer, rsize_t bufsz, const time_t time);
time_t -> tm struct tm gmtime( const time_t time ); struct tm gmtime_s(const time_t restrict time, struct tm *restrict result);
tm -> time_t time_t mktime( struct tm *time );
总结:这么多的时间,都没有精度高的calender time
int gettimeofday(struct timeval tv, struct timezone tz); 能获取us精度的时间,但是注意这个时间是可以被系统管理员修改的!
能够获取us精度的时间,这个时间不会被修改
does it make sense to strncpy(dst, src, strlen(src) + 1) ????
ansi c boolean type is int
snprintf(char buf, size_t size, const char format, ...) 是一个复杂的函数:
几个问题:
如果size > 0; 如果size = 0不会对buf有任何操作
linux平台的snprintf总是null-terminated的;windows平台只有_snprintf,不是null-terminated
表示如果buf足够大,encode到buf的字符数(不包括)
注意,如果被truncated,返回值可能与一些programmer预期的结果不一致,因此另外一个函数
_scnprintf(char buf, size_t size, const char format, ...)
scnprintf与snprintf的行为一致,除了scnprintf返回的是真正写入到buf的字节数(不包括)
返回值 >= size
int main() { char buf[5]; int ret;
ret = snprintf(buf, sizeof(buf), "%s", "hello");
printf("%d: %s\n", ret, buf);
return 0;
}
输出
5 hell ```
是不是dst总是null-terminated?
NO! 如果src的前n个字节没有,dst将不是null-terminated (c string). >>>>>>> e8cfc83926167c3fc0ecbcebc23cf4c258b44ac1