当我在Array中有一个Array时,C ++分段出错

时间:2018-02-19 17:18:53

标签: c++ arrays segmentation-fault

我试图实现我自己的一个名为Array的类,它主要像一个向量,因为我不想使用std。它适用于基本类,但是当我想实例化一个包含数组的类的数组(例如类的场景)时,它会停止程序说“Windows stoped fonctionning”...当我尝试使用break搜索错误时我看到当程序处于析构函数时它会显示“分段错误”。

这是我的课程:

Array.h

#ifndef NAME_ARRAY_H
#define NAME_ARRAY_H

#include <stdexcept>
#include <iostream>
#include "malloc.h"

template <class T>
class Array {
private:
T *m_array;
unsigned int m_tot_size;
unsigned int m_actual_size;
public:
Array(unsigned int size);
Array(Array<T> const& paste);

bool add(T var);
const T& get(unsigned int index);
bool remove(unsigned int index);
void kill();

unsigned int getActualSize() const;
unsigned int getTotalSize() const;
T* getArray() const;

T& operator[](unsigned int index);
Array<T>& operator=(Array<T> const& paste);


~Array();

};

//CONSTRUCTOR
template <class T>
Array<T>::Array(unsigned int size) : m_tot_size(size), m_actual_size(0) {
m_array = (T*) malloc(size * sizeof(T));
}

template <class T>
Array<T>::Array(Array<T> const &paste) : m_tot_size(paste.m_tot_size), 
m_actual_size(paste.m_actual_size) {
m_array = new T(*(paste.m_array));
}

//METHODES PUBLIC
template <class T>
const T & Array<T>::get(unsigned int index) {
if (index >= m_actual_size || index < 0)
    throw std::out_of_range("Index out of range");
return m_array[index];
}

template <class T>
bool Array<T>::remove(unsigned int index) {
if(index < m_actual_size && m_actual_size != 0) {
    m_actual_size--;
    m_array[index] = m_array[m_actual_size];

    return true;
}
return false;
}

template <class T>
bool Array<T>::add(T obj) {
if (m_actual_size >= m_tot_size) {
    T *temp;
    temp = (T*) realloc(m_array,5*sizeof(T));
    m_array = temp;
    m_array[m_actual_size] = obj;
    m_actual_size++;
    m_tot_size += 5;
    return false;
} else {
    m_array[m_actual_size] = obj;
    m_actual_size++;
    return true;
}
}

template <class T>
void Array<T>::kill() {
free(m_array);
delete [] m_array;
m_array = nullptr;

m_actual_size = 0;
m_tot_size = 0;
}

//ACCESSOR

template <class T>
unsigned int Array<T>::getActualSize() const { return m_actual_size; }

template <class T>
unsigned int Array<T>::getTotalSize() const { return m_tot_size; }

template <class T>
T* Array<T>::getArray() const { return m_array; }


//OPERATOR

template <class T>
T& Array<T>::operator[](unsigned int index) { return m_array[index]; }

template <class T>
Array<T>& Array<T>::operator=(Array<T> const& paste) {
if(this != &paste) {
    m_tot_size = paste.m_tot_size;
    m_actual_size = paste.m_actual_size;

    free(m_array);
    delete [] m_array;
    m_array = nullptr;

    m_array = new T(*(paste.m_array));
}
return *this;
}

//DESTRUCTOR
template <class T>
Array<T>::~Array() {
    free(m_array);
    delete [] m_array;
    m_array = nullptr;
}

Scene.cpp:

#include "Scene.h"

Scene::Scene(std::string sceneName) : m_name(sceneName), m_array_position(20) {
}


void Scene::update() {}

void Scene::render() {}


Scene::~Scene() {
//m_array_position.kill();
//m_array_systems.kill();
}

Scene.h

#ifndef NAME_SCENE_H
#define NAME_SCENE_H

#include <string>
#include <unordered_map>
#include "../components/Position.h"
#include "../utils/Array.h"
#include "../systems/System.h"

class Scene {
private:
std::string m_name;
Array<Position> m_array_position;

public:
Scene(std::string sceneName);
void update();
void render();
~Scene();
};

Array<Scene> scenes(1);

我认为问题在于程序会破坏数组,然后尝试销毁数组(位置只是一个结构)但我不确定,我不知道如何纠正它。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

我在很多地方看到这样的代码:

free(m_array);
delete [] m_array;

这对我来说并不好看。分配和释放内存时,必须将分配方法与释放匹配:

  • 如果您使用&#34; malloc&#34;保留记忆,则可以使用&#34;免费&#34;
  • 将其释放
  • 如果您使用&#34; new&#34;保留,则取消分配&#34;删除&#34;
  • 如果您保留&#34; new []&#34;,则可以使用&#34;删除[]&#34;

你应该只使用其中一种方法,或者如果你真的需要混合它们,那么你应该保留一些东西来跟踪释放内存所需的方法。