覆盖全局的operator new,什么时候会分配0个字节?
网上对于全局operator new的重载一般都说有6种形式,最基本的一种是:
void* operator new( size_t size ){ cout<<size<<endl; if(0 == size){//什么时候会分配0字节? 似乎不可能啊 return 0; } void *p = malloc(size); return p;}#include <stdio.h>#include <stdlib.h>int main(){ int *p=NULL; p = (int *)malloc(0); if (p==NULL) { printf("OK\n"); return -1; } else { printf("ERROR\n"); free(p); } return 0;}
[解决办法]
这是网上写的,又不是C++委员会写的,信他得不到永生。