From 57b7560a46e344b5b8f12b441dbe68d4594d790d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Leclaire-Fournier?= Date: Thu, 25 Jan 2024 21:25:08 -0500 Subject: [PATCH] =?UTF-8?q?R=C3=A9pare=20la=20d=C3=A9sallocation.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GPA675Lab1GOL/GridTeamH.cpp | 11 +++++++---- GPA675Lab1GOL/GridTeamH.h | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/GPA675Lab1GOL/GridTeamH.cpp b/GPA675Lab1GOL/GridTeamH.cpp index 1809385..6689443 100644 --- a/GPA675Lab1GOL/GridTeamH.cpp +++ b/GPA675Lab1GOL/GridTeamH.cpp @@ -44,6 +44,9 @@ GridTeamH& GridTeamH::operator=(GridTeamH const& cpy) mHeight = cpy.mHeight; mAliveCount = cpy.mAliveCount; + mData = new CellType[mWidth * mHeight]; + mIntermediateData = new CellType[mWidth * mHeight]; + memcpy(mData, cpy.mData, cpy.size() * sizeof(CellType)); memcpy(mIntermediateData, cpy.mIntermediateData, cpy.size() * sizeof(CellType)); } @@ -81,9 +84,7 @@ GridTeamH::~GridTeamH() // Mutateur modifiant la taille de la grille et initialise le contenu par la valeur spécifiée. void GridTeamH::resize(size_t width, size_t height, CellType initValue) { - // On exige que si mData soit null, mIntermediateData le soit aussi. - if (mData) - dealloc(); + dealloc(); mWidth = width; mHeight = height; @@ -94,12 +95,14 @@ void GridTeamH::resize(size_t width, size_t height, CellType initValue) fill(initValue, true); } -void GridTeamH::dealloc() const +void GridTeamH::dealloc() { if (mData && mIntermediateData) { delete[] mData; delete[] mIntermediateData; } + mData = nullptr; + mIntermediateData = nullptr; } // Accesseur retournant la valeur d'une cellule à une certaine coordonnée. diff --git a/GPA675Lab1GOL/GridTeamH.h b/GPA675Lab1GOL/GridTeamH.h index 53eef4e..204a3fc 100644 --- a/GPA675Lab1GOL/GridTeamH.h +++ b/GPA675Lab1GOL/GridTeamH.h @@ -96,7 +96,7 @@ private: std::uniform_real_distribution<> mDistribution; void fillBorderManipulations(DataType ptr, CellType value) const; - void dealloc() const; + void dealloc(); }; #endif GRIDTEAMH_H \ No newline at end of file