[英]How to expand dynamically allocated array in C++?
I have tried to implement solution to dynamical expansion of array as user is entering new data in main as shown: 当用户在main中输入新数据时,我试图实现数组动态扩展的解决方案,如下所示:
ComplexNumber** nizKompl = new ComplexNumber*[1];
bool unos = true;
while(unos){
cout << "Unesite novi kompleksni broj(realni imaginarni): ";
if(cin >> re >> im){
nizKompl[brojUnesenih] = new ComplexNumber(re,im);
++brojUnesenih;
ComplexNumber** nizTemp = new ComplexNumber*[brojUnesenih+1];
copy(nizKompl, nizKompl+brojUnesenih, nizTemp);
for(int i=0; i<brojUnesenih; ++i){
delete nizKompl[i];
}
delete [] nizKompl;
nizKompl = nizTemp;
} else {
cout << endl << endl;
unos = false;
}
}
ComplexNumber is custom type. ComplexNumber是自定义类型。 ComplexNumber works fine but I am having problem with double free or corruption error as I am entering new data and creating new ComplexNumbers in program.
ComplexNumber工作正常但我遇到双重免费或损坏错误的问题,因为我正在输入新数据并在程序中创建新的ComplexNumbers。
Is there some better way to achieve this? 有没有更好的方法来实现这一目标? I need to use my own dynamical array instead of std::vector because It is for educational purpose.
我需要使用自己的动态数组而不是std :: vector,因为它是出于教育目的。
As I understand double free error occurs when you try to free memory which is already free'd. 据我所知,当您尝试释放已经免费的内存时,会发生双重自由错误。 But It seems that I just can't resolve the issue, as I can see no double free should happen.
但似乎我无法解决这个问题,因为我看不到双重自由应该发生。
Is something happening in memory of which I have no knowledge? 记忆中发生的事情我不知道吗? Is it problem with std::copy as I am copying array of pointers of pointers?
这是std :: copy的问题,因为我正在复制指针指针数组?
I would really appreciate any help or suggestion. 我真的很感激任何帮助或建议。 Thank you!
谢谢!
If you really need not to use std::vector
(for educational reasons), then your problem is here: 如果你真的不需要使用
std::vector
(出于教育原因),那么你的问题就在这里:
for(int i=0; i<brojUnesenih; ++i){
delete nizKompl[i];
}
you just copied those pointers to your new, slightly bigger array. 你刚刚将这些指针复制到你新的,稍微大一点的数组。 If you delete the objects they point to, your new array is full of unusable dangling pointers.
如果删除它们指向的对象,则新阵列中充满了无法使用的悬空指针。
The proximate fixes are: 最近的修复是:
OR if you really want to delete the originals, you need to copy them first - this means a deep copy of the object rather than a shallow copy of the pointer, so your new array should be populated like 或者,如果您确实要删除原件,则需要先复制它们 - 这意味着对象的深层副本而不是指针的浅层副本,因此您的新数组应该像
// shallow copy: // copy(nizKompl, nizKompl+brojUnesenih, nizTemp); // deep copy: transform(nizKompl, nizKompl+brojUnesenih, nizTemp, clone);
with the function 与功能
ComplexNumber* clone(ComplexNumber *original) { return new ComplexNumber(*original); }
NB. NB。 I can't think of any good reason to do this, it's just wasteful.
我想不出有任何理由这样做,这只是浪费。
Use std::vector<ComplexNumber>
and use push_back to allocate memory dynamically. 使用
std::vector<ComplexNumber>
并使用push_back动态分配内存。
Your code will look something like this: 您的代码看起来像这样:
std::vector<ComplexNumber> nizKompl;
while(true) {
cout << "Unesite novi kompleksni broj(realni imaginarni): ";
if(cin >> re >> im){
nizKompl.push_back({re,im});
} else {
cout << endl << endl;
break;
}
}
As already mentioned, using std::vector
would be the right approach. 如前所述,使用
std::vector
将是正确的方法。
But wrt your exact question: 但是你的确切问题是:
for(int i=0; i<brojUnesenih; ++i){
delete nizKompl[i];
}
This is what causes the double delete, since you copy the same pointers to your new array and also delete them. 这是导致双重删除的原因,因为您将相同的指针复制到新数组并删除它们。 When entering the first number, you'll create an array of size one and create an object.
输入第一个数字时,您将创建一个大小为1的数组并创建一个对象。 On the second number, you'll allocate a new array of size two, copy the first element over to the new array, but also delete it.
在第二个数字上,您将分配一个大小为2的新数组,将第一个元素复制到新数组,但也删除它。 And once you add the third element, you'll create a new array of size three, copy the first two pointers and again try to delete them, but the first pointer was already deleted.
一旦你添加第三个元素,你将创建一个大小为3的新数组,复制前两个指针并再次尝试删除它们,但第一个指针已被删除。
It is unclear why you are not using the standard class std::vector
and using pointers instead of objects themselves. 目前还不清楚为什么你不使用标准类
std::vector
并使用指针代替对象本身。
Nevertheless it seems you mean something like the following 然而,似乎你的意思是如下
#include <iostream>
#include <algorithm>
#include <memory>
struct ComplexNumber
{
ComplexNumber() : ComplexNumber( 0, 0 ) {}
ComplexNumber( int re, int im ) : re( re ), im( im ) {}
int re;
int im;
};
int main()
{
ComplexNumber **nizKompl = nullptr;
size_t brojUnesenih = 0;
bool unos;
do
{
std::cout << "Unesite novi kompleksni broj(realni imaginarni): ";
int re, im;
if( ( unos = !!( std::cin >> re >> im ) ) )
{
ComplexNumber **nizTemp = new ComplexNumber * [brojUnesenih + 1];
std::copy( nizKompl, nizKompl + brojUnesenih, nizTemp );
nizTemp[brojUnesenih] = new ComplexNumber( re, im );
++brojUnesenih;
delete [] nizKompl;
nizKompl = nizTemp;
}
} while ( unos );
for ( size_t i = 0; i < brojUnesenih; i++ )
{
std::cout << "{ " << nizKompl[i]->re
<< ", " << nizKompl[i]->im
<< " } ";
}
std::cout << std::endl;
if ( nizKompl )
{
std::for_each( nizKompl, nizKompl + brojUnesenih, std::default_delete<ComplexNumber>() );
}
delete [] nizKompl;
return 0;
}
The program output might look like 程序输出可能看起来像
Unesite novi kompleksni broj(realni imaginarni): 1 1
Unesite novi kompleksni broj(realni imaginarni): 2 2
Unesite novi kompleksni broj(realni imaginarni): 3 3
Unesite novi kompleksni broj(realni imaginarni): 4 4
Unesite novi kompleksni broj(realni imaginarni): 5 5
Unesite novi kompleksni broj(realni imaginarni): a
{ 1, 1 } { 2, 2 } { 3, 3 } { 4, 4 } { 5, 5 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.