Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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++ 读取大小为4且数组大小递增的数据无效_C++_Arrays_Valgrind - Fatal编程技术网

C++ 读取大小为4且数组大小递增的数据无效

C++ 读取大小为4且数组大小递增的数据无效,c++,arrays,valgrind,C++,Arrays,Valgrind,我在运行Valgrind时遇到4个错误,尝试增加我用于模板堆栈的数组(最初大小为10),Valgrind在尝试向数组添加第11个元素时(正好是在推送过程中必须增加维度时)出现4个错误: 类堆栈{ 私人: int _size=10; T*_数据; 国际顶级; int _计数=0; 公众: //使用的构造器 堆栈(int-s){ 此->\u大小=s; _数据=新的T[_大小]; 这个->\u top=-1; } .... 无效推力(T v){ 如果(_count_top++); 这个->\u数据[\

我在运行Valgrind时遇到4个错误,尝试增加我用于模板堆栈的数组(最初大小为10),Valgrind在尝试向数组添加第11个元素时(正好是在推送过程中必须增加维度时)出现4个错误:

类堆栈{
私人:
int _size=10;
T*_数据;
国际顶级;
int _计数=0;
公众:
//使用的构造器
堆栈(int-s){
此->\u大小=s;
_数据=新的T[_大小];
这个->\u top=-1;
}
....
无效推力(T v){
如果(_count_top++);
这个->\u数据[\u top]=v;
这个->\u count++;
}否则{
//这里的堆错误++++++++
_大小++;
T*temp=新T[_尺寸];
对于(int i=0;i<\u top;i++){
温度[i]=_数据[i];
}
删除[]_数据;
_数据=温度;
_数据[_top]=v;
这个->_top++;
这个->\u count++;
}
我不确定新操作符和else语句之后的delete[]是否正确,我想我在那里遇到了问题,但我想不出其他解决方法

在main.cpp中,我仅推送模板堆栈中的11个元素:

stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
s.push(7);
s.push(8);
s.push(9);
s.push(10);
s.push(11);
stacks;
s、 推(1);
s、 推(2);
s、 推(3);
s、 推(4);
s、 推(5);
s、 推(6);
s、 推(7);
s、 推(8);
s、 推(9);
s、 推(10);
s、 推(11);
这是valgrind的输出

 ==4178== Invalid write of size 4
 ==4178==    at 0x10921E: stack<int>::push(int) (stack.h:85)
 ==4178==    by 0x108ED2: main (main.cpp:32)
 ==4178==  Address 0x5b82ca8 is 0 bytes after a block of size 
 40 alloc'd
 ==4178==    at 0x4C3089F: operator new[](unsigned long) (in 
 /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
 ==4178==    by 0x109144: stack<int>::stack() (stack.h:28)
 ==4178==    by 0x108B8D: main (main.cpp:9)
  HEAP SUMMARY:
 ==4178==     in use at exit: 0 bytes in 0 blocks
 ==4178==   total heap usage: 4 allocs, 4 frees, 73,808 bytes 
 allocated
 ==4178== 
 ==4178== All heap blocks were freed -- no leaks are possible
 ==4178== 
 ==4178== For counts of detected and suppressed errors, rerun with: -v
 ==4178== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 
 0)
==4178==大小为4的无效写入
==4178==0x10921E:stack::push(int)(stack.h:85)
==4178==0x108ED2:main(main.cpp:32)
==4178==地址0x5b82ca8是大小块后的0字节
40分
==4178==在0x4C3089F处:运算符新[](无符号长)(in)
/usr/lib/valgrind/vgpreload(memcheck-amd64-linux.so)
==4178==by 0x109144:stack::stack()(stack.h:28)
==4178==0x108B8D:main(main.cpp:9)
堆摘要:
==4178==在出口处使用:0个块中有0个字节
==4178==总堆使用率:4个alloc,4个free,73808字节
分配
==4178== 
==4178==所有堆块都已释放--不可能存在泄漏
==4178== 
==4178==对于检测到的和抑制的错误计数,请使用:-v重新运行
==4178==错误摘要:来自4个上下文的4个错误(已抑制:来自0个上下文)
0)

谢谢。

如Dave S所述,
if(\u count如Dave S所述,
if(\u count您所说的第11次推送导致错误。原因似乎是
您所说的第11次推送导致错误。原因似乎是
数组基于0,包含n个元素,而不是n+1。因此
int foo[10]
仅对foo[0]有效…foo[9]而不是foo[10],这是一个bug:
如果(\u count Please,编辑你的问题并显示一个使用容器和/或智能指针。不是手动内存管理(
新建
/
删除
)-这些东西很容易出错,而且现在大部分都过时了。[OT]:
top+1
count
似乎总是相等的,所以其中一个似乎是多余的。传统的术语是
size
capacity
顺便说一句。我曾经引入另一个索引变量以避免一个bug造成的off-by-by-bug。然后,我有两倍于off-by-by-bug的源。这不是我有过的最好的想法。(不幸的是,也不是最糟糕的。)数组是基于0的,包含n个元素,而不是n+1。因此
int-foo[10]
仅对foo[0]…foo[9]而不是foo[10]有效,这是一个错误:
如果(\u count请编辑您的问题并显示一个Use容器和/或智能指针。不是手动内存管理(
new
/
delete
)-这些东西很容易出错,而且现在大部分都过时了。[OT]:
top+1
count
似乎总是相等的,所以其中一个似乎是多余的。传统的术语是
size
capacity
顺便说一句。我曾经引入另一个索引变量以避免一个bug造成的off-by-by-bug。然后,我有两倍于off-by-by-bug的源。这不是我有过的最好的想法。(不幸的是,这也不是最糟糕的。)实际上我也尝试过这个条件,但在添加第11个元素后它会打印_count=0好的,我在delete语句后面放了_count+,现在它工作正常了。谢谢。实际上我也尝试过这个条件,但在添加第11个元素后它会打印_count=0好的,我在delete语句后面放了_count+,现在它工作了好的,谢谢。
 ==4178== Invalid write of size 4
 ==4178==    at 0x10921E: stack<int>::push(int) (stack.h:85)
 ==4178==    by 0x108ED2: main (main.cpp:32)
 ==4178==  Address 0x5b82ca8 is 0 bytes after a block of size 
 40 alloc'd
 ==4178==    at 0x4C3089F: operator new[](unsigned long) (in 
 /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
 ==4178==    by 0x109144: stack<int>::stack() (stack.h:28)
 ==4178==    by 0x108B8D: main (main.cpp:9)
  HEAP SUMMARY:
 ==4178==     in use at exit: 0 bytes in 0 blocks
 ==4178==   total heap usage: 4 allocs, 4 frees, 73,808 bytes 
 allocated
 ==4178== 
 ==4178== All heap blocks were freed -- no leaks are possible
 ==4178== 
 ==4178== For counts of detected and suppressed errors, rerun with: -v
 ==4178== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 
 0)