备忘
温金辉新浪博客:http://blog.sina.com.cn/ilte
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
(1)PNG编解码实现(LodePNG):
http://lodev.org/lodepng/
(2)_msize(void *) 函数
查看通过动态内存申请的指针对应堆内存区的大小,内存区必须是使用calloc、malloc、realloc申请的。见http://www.cnblogs.com/absolute8511/archive/2009/01/14/1649612.html
例如:
#include <malloc.h> void test__msize() { char *p; p = (char *)malloc(99*sizeof(char)); if(p != 0) { printf("msize: %d\n", _msize(p)); free(p); } }
输出:
msize: 99
(3)__FILE__和__LINE__标签
编译时编译器会自动替换为当前所处文件名和当前行号,在C中对应使用"%s"和"%d"输出。
(4)函数指针:
typedef double (*ArithmeticHandler)(double a, double b); double add(double a, double b) { return a + b; } double sub(double a, double b) { return a - b; } double mul(double a, double b) { return a * b; } double div(double a, double b) { if (b == 0) { return 0x7fffffff; } return a / b; } void test_function_pointer() { ArithmeticHandler methods[] = { // function pointer array add, sub, mul, div}; printf("1 + 3 = %.2lf\n", methods[0](1, 3)); printf("1 - 3 = %.2lf\n", methods[1](1, 3)); printf("1 * 3 = %.2lf\n", methods[2](1, 3)); printf("1 / 3 = %.2lf\n", methods[3](1, 3)); }
(5)不定长参数宏定义
代码中为了提供统一的输出方式,一般需要定义边长参数宏,比如定义一个Log宏用于统一输出Log信息。
#define LOG(format, ...) printf(format, ##__VA_ARGS__) void test_VA_ARGS() { printf("********test_VA_ARGS*******\n"); LOG("File:%s\tLine:%d\n", __FILE__, __LINE__); }
(6)time.h
头文件time.h定义了一些与时间相关的结构和函数,首先看一些定义:
1 typedef long clock_t; 2 typedef long time_t; 3 4 struct tm 5 { 6 int tm_sec; /* Seconds: 0-59 (K&R says 0-61?) */ 7 int tm_min; /* Minutes: 0-59 */ 8 int tm_hour; /* Hours since midnight: 0-23 */ 9 int tm_mday; /* Day of the month: 1-31 */ 10 int tm_mon; /* Months *since* january: 0-11 */ 11 int tm_year; /* Years since 1900 */ 12 int tm_wday; /* Days since Sunday (0-6) */ 13 int tm_yday; /* Days since Jan. 1: 0-365 */ 14 int tm_isdst; /* +1 Daylight Savings Time, 0 No DST, 15 * -1 don't know */ 16 };
几个函数定义:
1 clock_t clock (void); 2 time_t time (time_t*); 3 double difftime (time_t, time_t); 4 time_t mktime (struct tm*); 5 6 char* asctime (const struct tm*); 7 char* ctime (const time_t*); 8 struct tm* gmtime (const time_t*); 9 struct tm* localtime (const time_t*);
(1)clock_tclock (void);
返回程序运行到当前的时间间隔,精度1ms,可用于计算代码执行时间。
clock_t clock (void);