今天看到dynamic memory,测试如下程序: #include #include #include using namespace std; ifstream fin("test.in"); ofstream fout("test.out"); int main (){ int i = 0; int n = 0; int* p;
if(fin.is_open()){ fin>>n; fout<p = new (nothrow) int[n]; if (p == 0) fout << "Error: memory could not be allocated"<else{ for (i=0; i> p[i]; fin.close(); fout << "You have ent
多谢了哈 我在其他地方也得到一些回复,也贴在这吧。 new int[0]是可以的,当内存足够时,会返回非空指针,且这样new两次仍然会返回不同 的指针。 ISO/IEC 14882:1998(E) 5.3.4p7: When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allo- cate an array with no elements. The pointer returned by the new- expression is non-null. [Note: If the library allocation function is called, the pointer returned is distinct from the pointer to any other object. 这与malloc不同,malloc(0)也是可以的,但返回的是空指针,这并不表示出现了内存 用尽等异常情况。 最新的标准有了一些小的改
【在 j******n 的大作中提到】 : new T[0]; : is allowed. But result of dereferencing the returned is undefined.