gpt4 book ai didi

c++ - 大小为 4 的数组写入无效

转载 作者:行者123 更新时间:2023-11-28 07:08:58 25 4
gpt4 key购买 nike

<分区>

我正在用 C++ 创建一个 Vector 类,但遇到了一些问题。

==23391== Invalid write of size 4
==23391== at 0x401CAB: IntVector::IntVector(int, int) (IntVector.cpp:35)
==23391== by 0x401009: main (main.cpp:36)
==23391== Address 0x4c25218 is 0 bytes after a block of size 200 alloc'd
==23391== at 0x4A07152: operator new[](unsigned long) (vg_replace_malloc.c:363)
==23391== by 0x401C7F: IntVector::IntVector(int, int) (IntVector.cpp:32)
==23391== by 0x401009: main (main.cpp:36)



IntVector::IntVector(int size, int value){
capacity = INITIAL_CAPACITY;
while(size >= capacity)
{
capacity = capacity*2;
}
if(size < 0)
{
count = 0;
array = new int[capacity];
}
else
{
count = size;
array = new int[capacity]; // line 32
for(int i = 0; i < count; i++)
{
array[i] = value; //line 36
}
}

为什么我可以读取或写入数组?在 main 中,我声明了 v1(5,0)initial capacity = 50 并且循环只转到 4,所以它不可能读取或写入超过 50!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com