[英]C++ segmentation fault using dynamic arrays of strings
My string-dynamic-array.cpp file我的 string-dynamic-array.cpp 文件
#include <iostream>
#include <string>
class DynamicArray
{
public:
DynamicArray()
: mCapacity(1), mNumberOfElements(0)
{
mArray = new std::string[mCapacity];
}
DynamicArray(int size)
: mCapacity(size), mNumberOfElements(0)
{
mArray = new std::string[mCapacity];
}
DynamicArray(const DynamicArray& array)
: mCapacity(array.getCapacity()), mNumberOfElements(array.length())
{
mArray = new std::string[mCapacity];
for (size_t i = 0; i < mCapacity; ++i)
{
mArray[i] = array.get(i);
}
}
~DynamicArray()
{
delete[] mArray;
}
void add(std::string element)
{
if (mNumberOfElements >= mCapacity)
{
expand();
}
mArray[mNumberOfElements++] = element;
}
std::string get(int index) const
{
if (index > mNumberOfElements)
{
std::string exception = std::to_string(index) + " index is out of bounds.";
std::cout << exception;
return std::string();
}
if (index < 0)
{
if (mNumberOfElements + index < 0)
{
std::string exception = std::to_string(index) + " index result in " + std::to_string(mNumberOfElements + index) + " which is out of bounds.\n";
std::cout << exception;
return std::string();
}
return mArray[mNumberOfElements + index];
}
return mArray[index];
}
int length() const
{
return mNumberOfElements;
}
int getCapacity() const
{
return mCapacity;
}
private:
int mCapacity;
int mNumberOfElements;
std::string* mArray;
void initialize(int from)
{
for (size_t i = from; i < mCapacity; ++i)
{
mArray[i] = std::string();
}
}
void expand()
{
mCapacity *= 2;
std::string* temporaryArray = new std::string[mCapacity];
for (size_t i = 0; i < mCapacity; ++i)
{
temporaryArray[i] = mArray[i];
}
delete[] mArray;
mArray = temporaryArray;
initialize(mNumberOfElements);
}
};
int main()
{
DynamicArray strings;
strings.add("Hello");
strings.add("World");
for (size_t i = 0; i < strings.length(); ++i)
{
std::cout << strings.get(i) << std::endl;
}
}
My output我的 output
$ clang++ tests/string-dynamic-array.cpp -o tests/string-dynamic-array && ./tests/string-dynamic-array
[1] 14950 segmentation fault (core dumped) ./tests/string-dynamic-array
I get a segmentation fault.我得到一个分段错误。
The issue as far as I've found is in the code where I expand the array, in the expand()
function.据我发现,问题在于我展开数组的代码中,在
expand()
function 中。 I think it's in the for loop because the index of for loop is out of bounds of the original array.我认为它在 for 循环中,因为 for 循环的索引超出了原始数组的范围。
I've tried this with int
, it seems to work fine.我已经用
int
试过了,它似乎工作正常。 How can I do this with strings?我怎样才能用字符串做到这一点?
As commented by UnholySheep.正如 UnholySheep 所评论的那样。 I changed my expand function a bit so the loop only runs till the size of the old array.
我稍微改变了我的扩展 function ,所以循环只运行到旧数组的大小。
Updated expand function:更新扩展 function:
void expand()
{
mCapacity *= 2;
std::string* temporaryArray = new std::string[mCapacity];
for (size_t i = 0; i < mNumberOfElements; ++i)
{
temporaryArray[i] = mArray[i];
}
delete[] mArray;
mArray = temporaryArray;
initialize(mNumberOfElements);
}
In “expand” method you have deleted mArray, then, you copied the temporaryArray to it, while as far as I know, mArray's handle is not valid anymore.在“expand”方法中,您已经删除了 mArray,然后,您将temporaryArray 复制到其中,而据我所知,mArray 的句柄不再有效。 You need to pass temporaryArray's handle to mArray so you could use it as a new expanded array.
您需要将temporaryArray 的句柄传递给mArray,以便您可以将其用作新的扩展数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.