Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::声明结构表时出错_C++_C - Fatal编程技术网

C++ std::声明结构表时出错

C++ std::声明结构表时出错,c++,c,C++,C,我宣布结构简单: struct Heap { int size; int *heap_array; }; 当我试图创建表时,k是标准输入的int: Heap *rooms = new Heap[k]; 我得到: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted 按字面意思使用int表示一切正常: Heap *rooms = new Hea

我宣布结构简单:

struct Heap {
  int size;
  int *heap_array;
};
当我试图创建表时,
k
是标准输入的int:

Heap *rooms = new Heap[k];
我得到:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted
按字面意思使用int表示一切正常:

Heap *rooms = new Heap[0];

如何声明这样的表?

stdin
读取的值可能无效。如果读取失败,可能会有一些默认值,如2^32。

stdin
读取的值可能无效。如果读取失败,您可能会有一些默认值,比如2^32。

我看没有什么问题,除非
k
中有一些垃圾。尝试调试
k
的值是多少?很可能您没有正确读取
k
。让它成为一个大数字。这将导致错误的alloc。什么是
k
,读取输入的代码是什么?是的,你是对的,很遗憾:p我看没有什么错误,除非
k
中有一些垃圾。尝试调试
k
的值是多少?很可能您没有正确读取
k
。让它成为一个大数字。这将导致错误的alloc。什么是
k
,读取输入的代码是什么?是的,你是对的,很遗憾:pOr,如果
k
是一个
int
,值可能是负值。或者,如果
k
是一个
int
,值可能是负值。