C/C++拾撷

来源:互联网 发布:国家安全网络宣传周 编辑:程序博客网 时间:2024/04/28 19:12

浮点型和整型一起运算,整型先转为浮点型,在进行运算。


VC中的interface关键字定义:

#define interface struct


结构体实现单字节对齐:

#pragma pack(push, 1)struct{    ......};#pragma pack(pop)


GUID(全局唯一标识符)的定义,包含在头文件guiddef.h中:

#ifndef GUID_DEFINED#define GUID_DEFINED#if defined(__midl)typedef struct {    unsigned long  Data1;    unsigned short Data2;    unsigned short Data3;    byte           Data4[ 8 ];} GUID;#elsetypedef struct _GUID {    unsigned long  Data1;    unsigned short Data2;    unsigned short Data3;    unsigned char  Data4[ 8 ];} GUID;#endif#endif

__midl 预定义符解释如下:

When the MIDL compiler processes the input IDL and ACF files, __midl is defined by default and is used for conditional compilation to attain consistency throughout the build. This phases out the use of define statements in the header files, such asMIDL_PASS, and replaces them with a consistent flag. The value of the __midl constant indicates the compiler versionmajor.minor according to the formulamajor * 100 +minor; for example MIDL ver. 6.0.x has the __midl defined as 600.

GUID具体参考:

http://zh.wikipedia.org/zh-cn/%E5%85%A8%E5%B1%80%E5%94%AF%E4%B8%80%E6%A0%87%E8%AF%86%E7%AC%A6#cite_note-1

 

一个简单的调试打印宏:

//调试打印宏#ifdef _DEBUG    #define debug_msg printf("%s[%d]:",__FILE__,__LINE__);printf#else    #define debug_msg#endif //_DEBUG

这个调试宏在函数并发时,打印出的内容顺序会乱,不方便查看。

类的初始化的顺序:类成员变量的初始化不是按照初始化表的顺序被初始化的,而是按照在类中声明的顺序被初始化的。

 

VC内存填充

0xCCCCCCCC:在栈上分配的内存,未初始化

0xCDCDCDCD:在堆区分配的内存,未初始化

0xDDDDDDDD:堆上已经释放的内存

0xFEEEFEEE:同0xDDDDDDDD,VC编译器的行为

0xFDFDFDFD:动态分配内存,前后各一个边界标识符

选择这些填充值的原因:

1.大数。若当作指针会越界。

2.奇数。指针通常指向偶数地址,原因:内存对齐。

3.非0。不会与NULL字符混淆。

更多参考:

http://www.cnblogs.com/pcchinadreamfly/archive/2012/04/26/2471317.html

http://www.programlife.net/debugger-magic-number.html

 

NULL的定义,包含在头文件stddef.h中:

//stddef.h/* Define NULL pointer value */#ifndef NULL#ifdef __cplusplus#define NULL    0#else#define NULL    ((void *)0)#endif#endif

 

宏offsetof的定义,包含在头文件stddef.h中:

//stddef.h/* Define offsetof macro */#ifdef __cplusplus#ifdef  _WIN64#define offsetof(s,m)   (size_t)( (ptrdiff_t)&reinterpret_cast<const volatile char&>((((s *)0)->m)) )#else#define offsetof(s,m)   (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))#endif#else#ifdef  _WIN64#define offsetof(s,m)   (size_t)( (ptrdiff_t)&(((s *)0)->m) )#else#define offsetof(s,m)   (size_t)&(((s *)0)->m)#endif#endif/* __cplusplus */

 

注释代码。一段代码现在不要,可能后面的版本又要,建议用预定义宏:

#if 0......#endif
原创粉丝点击