new, mallocでサイズ0の領域を確保するとどうなるかの実験
実行環境はVC10, Windows7 x64
#include <iostream>
#include <stdlib.h>
int main()
{
using namespace std;
try
{
// mallocでサイズ0の領域を確保する
unsigned char *buffMalloc = (unsigned char *)malloc(0);
cout << "malloc ptr => " << (intptr_t)buffMalloc << endl;
// newでサイズ0の領域を確保する
unsigned char *buffNew = new unsigned char[0];
cout << "new ptr => " << (intptr_t)buffNew << endl;
}
catch (const exception &ex) { cout << ex.what(); }
catch (...) {}
cin.get();
return 0;
}
実行結果 サイズ0が確保されて、そのポインタが返される。
malloc ptr => 5187096 new ptr => 5188072
0 Kommentarer:
コメントを投稿