(转)C宏技巧汇总
C 宏 1,防止一个头文件被重复包含 #ifndef BODYDEF_H #define BODYDEF_H //头文件内容 #endif 2,得到指定地址上的一个字节或字 #define MEM_B( x ) ( *( (byte *) (x) ) ) #define MEM_W( x ) ( *( (word *) (x) ) ) 3,得到一个field在结构体(struct)中的偏移量...
View ArticleC++多重继承导致delete引起堆错误、智能指针设计陷阱
首先一个原则是这样:被用来delete的指针,一定是new出来的。在设计智能指针的时候发现,如果采用delete this机制,到最后可能会有严重的问题。假设有这样的多重继承:class CA{};class CB{};class CC{};class CABC : public CA, public CB, public CC{}; CABC* pAbc = new CABC; CA* pA =...
View Article接口用std::string const std::string& const char*的情况
string传值方式效率肯定是有问题的,如果使用引用方式,则必须提供原生指针接口,否则会有异常void Test(const std::string& strParam){ strParam.c_str();}void Test(const char* szParam){}void Test(){ Test(nullptr); int i = 0; i++; std::string strA...
View Articlewarning C4005: 'WINVER' : macro redefinition 的解决方法
Windows下使用VS2008编译时经常遇到macro redefinition警告或错误,如: c:\programme\microsoft sdks\windows\v6.0a\include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition 解决方法:打开编译选项 /showIncludesUse the...
View Article《转知乎》如何利用 C++ 数组指针中的数组大小信息?
int *p = new int[10]; delete [] p; 既然 delete 时无需提供数组大小,那么显然编译器是知道数组的大小(或者说界限的)。 那么 编译器是如何知道数组大小的?(比如维护了一张表、按特定格式存储……) 既然知道数组大小,编译器能否自动检查数组越界的情况?不检查是否是为了性能? 编程时如何利用这份信息?...
View Article二维数组的内存结构
#define __LEN_NAME 8#define __COUNT 4struct tagTem{ char nameList[__COUNT][__LEN_NAME];};#include <cstdlib>#include <cstring>int main(){ tagTem tem; memset(&tem, NULL, sizeof(tem)); int...
View Articlelambda表达式变量声明周期简单分析
// lambda_test.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>#include <functional>// 利用rtii观察堆栈生命周期class StackLifeTimeWatching{public:...
View Articlecstring qstring string copy on write 跨线程传递安全性
不探讨多个线程持有同一个string对象的场景,只分析copy on write特性引起的,拷贝一个新对象发送到另外的线程后引起的安全问题。 假设cow基于reference counting实现,那么ref为2,其中一个线程开始修改,需要找复制,然后减少引用计数,然后操作自己的副本。这种场景下是安全的。但是不是所有的视线rec都是原子操作,所以,,,Enic 2017-05-08 22:52 发表评论
View Article