简体   繁体   English

C++:分段错误(核心转储)

[英]c++: segmentation fault (core dumped)

I am trying to make a dynamic array implementation in C++ using pointers and templates so that I can accept all types.我正在尝试使用指针和模板在 C++ 中实现动态数组,以便我可以接受所有类型。 The code worked fine with int but using string gives an error.该代码与int一起工作正常,但使用string会出错。 I tried online other SO questions but found nothing about my scenario.我在网上尝试了其他 SO 问题,但对我的场景一无所知。

Code:代码:

#include <iostream>
#include <string>
using namespace std;

template <typename T>
class dynamicIntArray
{
private:
    T *arrPtr = new T[4]();
    int filledIndex = -1;
    int capacityIndex = 4;

public:
    // Get the size of array
    int size(void);

    // Insert a data to array
    bool insert(T n);

    // Show the array
    bool show(void);
};

template <typename T> 
int dynamicIntArray<T>::size(void)
{
    return capacityIndex + 1;
}

template <typename T> 
bool dynamicIntArray<T>::insert(T n)
{
    if (filledIndex < capacityIndex)
    {
        arrPtr[++filledIndex] = n;
        return true;
    }
    else if (filledIndex == capacityIndex)
    {
        // Create new array of double size
        capacityIndex *= 2;
        T *newarrPtr = new T[capacityIndex]();

        // Copy old array
        for (int i = 0; i < capacityIndex; i++)
        {
            newarrPtr[i] = arrPtr[i];
        }

        // Add new data
        newarrPtr[++filledIndex] = n;
        arrPtr = newarrPtr;

        return true;
    }
    else
    {
        cout << "ERROR";
    }
    return false;
}

template <typename T> 
bool dynamicIntArray<T>::show(void)
{
    cout << "Array elements are: ";
    for (int i = 0; i <= filledIndex; i++)
    {
        cout << arrPtr[i] << " ";
    }
    cout << endl;

    return true;
}

int main()
{
    dynamicIntArray<string> myarray;

    myarray.insert("A");
    myarray.insert("Z");
    myarray.insert("F");
    myarray.insert("B");
    myarray.insert("K");
    myarray.insert("C");

    cout << "Size of my array is: " << myarray.size() << endl;

    myarray.show();
}

Error:错误:

segmentaion fault (core dumped)

Classic Off-by-one error :经典的一对一错误

if (filledIndex < capacityIndex)
{
    arrPtr[++filledIndex] = n;

Before you insert your 5th item filledIndex is 3 < 4 ( capacityIndex ).在插入第 5 个项目之前, filledIndex3 < 4 ( capacityIndex )。 This leads to arrPtr[4] to be accessed (out of bound access as its range is currently [0..3]).这导致arrPtr[4]被访问(越界访问,因为它的范围当前是 [0..3])。

Fix it by initially setting filledIndex to 0 and change arrPtr[++filledIndex] = n;通过最初将filledIndex设置为0并更改arrPtr[++filledIndex] = n;来修复它arrPtr[++filledIndex] = n; to arrPtr[filledIndex++] = n;arrPtr[filledIndex++] = n;

You should note that your code suffers from serious defects though: leaking memory, questionable names and style, etc. You might want to post its fixed version to https://codereview.stackexchange.com/ .您应该注意到您的代码存在严重缺陷:内存泄漏、可疑的名称和样式等。您可能希望将其修复版本发布到https://codereview.stackexchange.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM