关于new分配空间时size = 0

仙境之桥2046 2013-04-16 10:21:30
如题
最近遇到了这个问题,不知道这个size为零的情况,给分配空间吗?

我自己做的程序测试没有报错.

linux C++;

int *pInt = new(int[0]);
if(NULL == pInt)
{
fprintf(stderr,"new pInt memory failure\n");
}
printf("pInt = %d ; pIntsize =%d ; *pInt = %p\n", *pInt , sizeof(pInt), pInt);

结果
pInt = 0 ; pIntsize =4 ; *pInt = 0x82b1008

这个内存地址可以用吗?大小是0啊?


...全文
172 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamnobody 2013-04-16
  • 打赏
  • 举报
回复
引用 1 楼 mingliang1212 的回复:
没报错不代表没有错. 不可以为0
查实,,可以为0 ,返回指向一个空数组的指针.
starytx 2013-04-16
  • 打赏
  • 举报
回复
引用 6 楼 smallnat 的回复:
从测试上看,指针是是一个非零指针,指向一个int类型。可以对指针内容赋值。效果上和int* pInt= new(int)没什么区别。 引用 5 楼 starytx 的回复: 此时new返回有效地非零指针,与其他指针不同,该指针不能进行解引用操作,因为它没有指向任何元素,可以对该指针进行比较运算,因此可以在循环中使用;在该指针上加0或者减去本身得0值。 ------------------……
指针的解引用操作就是 *p,也就是取指针指向的对象的值,刚才试了一下,*p = 1;这样的操作好像不会出错,但是之后的delete会报异常,如果中间没有解引用操作,那么delete正常,由此可见,这样的指针还是不可用的,否则无法正常释放
hznat 2013-04-16
  • 打赏
  • 举报
回复
从测试上看,指针是是一个非零指针,指向一个int类型。可以对指针内容赋值。效果上和int* pInt= new(int)没什么区别。
引用 5 楼 starytx 的回复:
此时new返回有效地非零指针,与其他指针不同,该指针不能进行解引用操作,因为它没有指向任何元素,可以对该指针进行比较运算,因此可以在循环中使用;在该指针上加0或者减去本身得0值。 ------------------以上是c++ primer中的内容
这里说的“不能进行解引用操作”指的是什么啊?
starytx 2013-04-16
  • 打赏
  • 举报
回复
此时new返回有效地非零指针,与其他指针不同,该指针不能进行解引用操作,因为它没有指向任何元素,可以对该指针进行比较运算,因此可以在循环中使用;在该指针上加0或者减去本身得0值。 ------------------以上是c++ primer中的内容
csd1208 2013-04-16
  • 打赏
  • 举报
回复
printf("pInt = %d ; pIntsize =%d ; *pInt = %p\n", *pInt , sizeof(pInt), pInt); 你看清楚 你这个 pInt的值,打印的是什么啊,是*pInt啊,这个是地址上面的数据啊
pathuang68 2013-04-16
  • 打赏
  • 举报
回复
语法没错,语义没有。
Qyee16 2013-04-16
  • 打赏
  • 举报
回复
iamnobody 2013-04-16
  • 打赏
  • 举报
回复
没报错不代表没有错. 不可以为0
FrankHB1989 2013-04-16
  • 打赏
  • 举报
回复
明确UB。 至于没有明确是不是失败,是很老的一个defect了,在C++03前解决,见http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2000/n1216.pdf。 ISO C++98 3.7.3.1/2 ... If the size of the space requested is zero, the value returned shall not be a null pointer value (4.10). The results of dereferencing a pointer returned as a request for zero size are undefined.32) 32) The intent is to have operator new() implementable by calling malloc() or calloc(), so the rules are substantially the same. C++ differs from C in requiring a zero request to return a nonnull pointer. ISO C++03 3.7.3.1/2 ... If the request succeeds, the value returned shall be a nonnull pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete. The effect of dereferencing a pointer returned as a request for zero size is undefined.32) __________________ 32) The intent is to have operator new() implementable by calling malloc() or calloc(), so the rules are substantially the same. C + + differs from C in requiring a zero request to return a non-null pointer. ISO C++11 3.7.4.1/2 .... Even if the size of the space requested is zero, the request can fail. If the request succeeds, the value returned shall be a non-null pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete. The effect of dereferencing a pointer returned as a request for zero size is undefined.35 35) The intent is to have operator new() implementable by calling std::malloc() or std::calloc(), so the rules are substantially the same. C++ differs from C in requiring a zero request to return a non-null pointer. 到N3485换了点wording,没实质区别。 WG21/N3485 3.7.4.1/2 ... Even if the size of the space requested is zero, the request can fail. If the request succeeds, the value returned shall be a non-null pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete. The effect of indirecting through a pointer returned as a request for zero size is undefined.35 35) The intent is to have operator new() implementable by calling std::malloc() or std::calloc(), so the rules are substantially the same. C++ differs from C in requiring a zero request to return a non-null pointer.

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧