由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请问一个入门级 dynamic memory 的问题
相关主题
Why no output file generate? What is wrong?关于文件读取的C++ 问题?
fstream 扫盲,谢谢!a very simple c++ question
C++ problemhow to read a sentence into a vector of string?
读取数据求教C++ Q 99-102 (转载)
What is wrong with the code?fstream不能做类的成员吗
问一个C++文件读取的问题why do we still use dynamic allocation?
一个极简单的程序求教ofstream and cout question
C++: Static initialization dependency[合集] 为什么10进制输出是负的?
相关话题的讨论汇总
话题: fout话题: int话题: fin话题: memory话题: include
进入Programming版参与讨论
1 (共1页)
D******4
发帖数: 47
1
今天看到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<<"n is: "< 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
j******n
发帖数: 271
2
new T[0];
is allowed. But result of dereferencing the returned is undefined.
D******4
发帖数: 47
3
多谢了哈
我在其他地方也得到一些回复,也贴在这吧。
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.

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] 为什么10进制输出是负的?What is wrong with the code?
C++默认的copy constructor的疑惑问一个C++文件读取的问题
输入输出流,stl,api精通各需要多长时间?一个极简单的程序求教
C++里用Blas/Lapack的问题 (转载)C++: Static initialization dependency
Why no output file generate? What is wrong?关于文件读取的C++ 问题?
fstream 扫盲,谢谢!a very simple c++ question
C++ problemhow to read a sentence into a vector of string?
读取数据求教C++ Q 99-102 (转载)
相关话题的讨论汇总
话题: fout话题: int话题: fin话题: memory话题: include