求助,关于new []的参数为0的问题。

wangyangkobe 2011-08-15 02:27:06
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
char arr[0];
int arr1[0];

cout<<sizeof(arr)<<endl;

printf("%p\n", &arr);
printf("%p\n", &arr1);

char *pArr = new char[0];
*pArr = 'v';
cout<<sizeof(pArr)<<endl;
cout<<*pArr<<endl;
printf("%p\n", &pArr);


int *pInt = new int[0];
*pInt = 1;
cout<<*pInt<<endl;
printf("%p\n", &pInt);
return 0;
}
...全文
148 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fallening 2011-08-15
  • 打赏
  • 举报
回复
gcc 扩展支持这种形式,不过是用来做链表用的
一叶之舟 2011-08-15
  • 打赏
  • 举报
回复
这叫不定长数组
机智的呆呆 2011-08-15
  • 打赏
  • 举报
回复
When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements.
*pInt = 1;这个我觉得应该是未定义的。
If the constant-expression(5.19) is present, it shall be an integral constant expression and its value shall be greater than zero
c++规定数组的长度要大于0
qq120848369 2011-08-15
  • 打赏
  • 举报
回复
一种类似的东西如下:

struct Test
{
int size;
char data[];
};

data不占内存,意味着Test可以在一个int后跟随任意大小内存.
机智的呆呆 2011-08-15
  • 打赏
  • 举报
回复
char arr[0];
int arr1[0];
按照c++标准上面代码都是错误的
int *pInt = new int[0];是正确的

64,648

社区成员

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

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