释放内存(主题模板)时出现问题
Problem freeing memory (subject Template)
Set.h
#ifndef Set_h
#define Set_h
#include <iostream>
using namespace::std;
template <class T>
class Set {
T* group;
int size_group;
public:
Set():group(NULL),size_group(0){};
Set(T*,int);
Set(const Set<T>&);
~Set();
bool isThere(const T&)const;
void getType()const;
Set<T>& operator =(const Set<T>&);
bool operator ==(const Set<T>&) const;
void operator +=(const T);
void operator -=(const T&);
Set<T>& operator +(Set<T>&);
Set<T>& operator -(const Set<T>&);
bool operator >(const Set<T>&)const;
friend ostream& operator <<(ostream&,const Set<T>&);
};
//Realizations
template <class T>
Set<T>::Set(T* gro,int size):group(gro),size_group(size){}
template <class T>
Set<T>::Set(const Set<T>& obj){ // c'Ctor (copy)
this->size_group = obj.size_group;
this->group = new T[this->size_group];
for (int i = 0; i < size_group; i++) {
group[i] = obj.group[i];
}
}
template <class T>
void Set<T>::getType() const{
cout << "The type is: " << typeid(group).name() << endl;
}
template <class T>
Set<T>& Set<T>::operator = (const Set<T>& obj){
this->size_group = obj.size_group;
this->group = new T[size_group];
for (int i = 0; i < size_group; i++) {
this->group[i] = obj.group[i];
}
return *this;
}
template <class T>
bool Set<T>::operator==(const Set<T>& obj) const{
int count = 0;
T temp;
if(this->size_group != obj.size_group)
return false;
for (int i = 0; this->size_group; i++) {
temp = this->group[i];
count = 0;
for (int j = 0; j < obj.size_group; j++) {
if(temp == obj.group[j])
count++;
}
if(count != 1)
return false;
}
return true;
}
template <class T>
void Set<T>::operator+=(const T var){
if(!isThere(var)){
T* Temp = new T[this->size_group+1];
for (int i = 0; i < this->size_group; i++)
Temp[i] = this->group[i];
Temp[size_group] = var;
delete [] this->group;
this->size_group++;
this->group = Temp;
}
}
template <class T>
Set<T>& Set<T>::operator+(Set<T>& obj){ //
if(obj.size_group > this->size_group){
for (int i = 0; i < obj.size_group; i++){
for (int j = 0 ; j < this->size_group; j++){
if(!this->isThere(obj.group[i]))
*this += obj.group[i];
}
}
return *this;
}else{
for (int i = 0; i < this->size_group; i++){
for (int j = 0 ; j < obj.size_group; j++){
if(!obj.isThere(this->group[i]))
obj += this->group[i];
}
}
return obj;
}
}
template <class T>
bool Set<T>::isThere(const T& var) const{
for (int i = 0; i < this->size_group; i++) {
if(this->group[i] == var)
return true;
}
return false;
}
template <class T>
Set<T>::~Set() {
delete [] group;
}
#endif /* Set_h */
主要:
#include "Set.h"
int main() {
int arr[] = {3,7,8,1};
int arr2[] = {1,2,5};
Set<int> j(arr,4),l(arr2,3),k;
k = j + l;
}
我正在尝试合并两个数组,但我的问题在于方法void Set<T>::operator+=(const T var)
和这一行delete [] this-> group;
我收到此错误:
模板和异常 (8421,0x1000dadc0( malloc:对象 0x7ffeefbff400 的 *** 错误:指针为星期五未分配 模板和异常 (8421,0x1000dadc0( malloc:*** 在malloc_error_break中设置断点以进行调试
您的Set(T*,int)
构造函数没有为group
分配new[]
内存,这意味着当operator+=
(以及~Set()
和operator=
(尝试delete[]
group
指向的内存时,它们将在运行时失败,在您的示例中是main()
拥有并传递给j
和l
对象的本地堆栈内存。
您的Set(T*,int)
构造函数必须new[]
group
数组并将输入gro
数组的内容复制到其中,类似于复制构造函数的方式,例如:
template <class T>
Set<T>::Set(T* gro, int size) : group(NULL), size_group(0)
{
size_group = size;
group = new T[size];
for (int i = 0; i < size; i++) {
group[i] = gro[i];
}
}
附带说明一下,您的operator=
正在泄漏内存,因为它在分配新的group
数组之前不会delete[]
当前group
数组。 它应该创建一个临时数组,类似于您的operator+=
所做的,例如:
template <class T>
Set<T>& Set<T>::operator=(const Set<T>& obj)
{
if (this != &obj){
T* Temp = new T[obj.size_group];
for (int i = 0; i < obj.size_group; i++)
Temp[i] = obj.group[i];
delete [] group;
group = Temp;
size_group = obj.size_group;
}
return *this;
}
或者更好的是,通过复制交换习惯用法使用复制构造函数:
template <class T>
Set<T>& Set<T>::operator=(const Set<T>& obj)
{
if (this != &obj){
Set<T> temp(obj);
std::swap(group, temp.group);
std::swap(size_group, temp.size_group);
}
return *this;
}
此外,您的operator+
实施完全错误。 它需要返回一个新的Set
,即*this
数组和obj
数组的串联,根本不修改obj
或*this
。 因此,它需要看起来更像这样:
template <class T>
Set<T> Set<T>::operator+(const Set<T>& obj) const
{
Set<T> res(*this);
for (int i = 0; i < obj.size_group; i++) {
res += obj.group[i];
}
return res;
}
相关文章:
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 包含矢量指针的结构的内存释放问题
- 在函数中分配内存时出现问题
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 使用 NTAllocateVirtualMemory 和 GetProcAddress 的内存分配问题不起作用
- 释放内存(主题模板)时出现问题
- C++ 中 std::vector 的内存问题
- C++ DLL 堆内存分配问题
- 在我的以下代码中获取 MLE(内存限制错误).尝试解决 ROUND C 2019(问题 A-摆动行走)启动问题
- 字符 * 未从重载运算符或内存管理问题正确返回
- delete[]有问题,如何部分删除内存
- 内存泄漏问题
- 指向动态内存中结构中的变量时出现问题
- c++中的内存管理问题
- C++堆栈内存管理问题
- 为什么我的C 代码在Linux上运行,而在Windows上不运行,是否有RAM内存的问题
- JNA 参数问题:内存访问无效
- boost的问题::内存映射~500MB,外部USB文件