将具有动态内存的自定义类添加到 vector 中时出现奇怪的错误。
错误是
error for object 0x7fee9ac000e0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
尝试添加带有动态内存的Symbol
会产生错误,在析构函数中删除delete[] 参数
会消除错误,但我认为会导致内存泄漏。
代码如下
//test.cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include "symbol.h"
int main(int argc, char const *argv[])
{
Symbol a('A');
std::cout << a << std::endl;
Symbol b('B',2);
std::cout << b << std::endl;
double p[3] = {1.2, 2.4, 4.8};
Symbol c('C',3,p);
std::cout << c << std::endl;
Symbol* d = new Symbol('D',3,p);
////
std::cout << *d << std::endl;
std::vector<Symbol> axiom;
axiom.push_back(a)
axiom.push_back(b); // This lines produces the error
delete d;
return 0;
}
//symbol.h
#ifndef SYMBOL_H
#define SYMBOL_H
#include <iostream>
class Symbol{
friend std::ostream& operator<<(std::ostream& output, const Symbol& s);
private:
char character;
int numpar;
double* parameters;
public:
Symbol();
Symbol(const char c);
Symbol(const char c, const int n);
Symbol(const char c, const int n, const double p[]);
~Symbol();
bool operator == (const Symbol &other) const;
};
#endif
//symbol.cpp
#include "symbol.h"
Symbol::Symbol()
{
character = 0;
numpar = 0;
}
Symbol::Symbol(const char c)
{
character = c;
numpar = 0;
}
Symbol::Symbol(const char c, const int n)
{
character = c;
if(n > 0)
{
numpar = n;
parameters = new double[numpar];
std::fill(parameters, parameters+numpar, 0.0);
}
else
{
numpar = 0;
}
}
Symbol::Symbol(const char c, const int n, const double p[])
{
character = c;
if(n > 0)
{
numpar = n;
parameters = new double[numpar];
std::copy(p,p+numpar,parameters);
}else{
numpar = 0;
}
}
Symbol::~Symbol()
{
if(this->numpar > 0)
{
delete[] parameters; //If I comment this line the code runs smoothly but I think it produces memory leaks
}
}
bool Symbol::operator==(const Symbol &other) const {
if (character == other.character)
return true;
return false;
}
std::ostream& operator<<(std::ostream& output, const Symbol &s){
output << s.character;
if(s.numpar > 0)
{
output << '(';
for(int i = 0 ; i < s.numpar-1 ; i++)
{
output << s.parameters[i] << ", ";
}
output << s.parameters[s.numpar-1] << ')';
}
return output;
}
最佳答案
您使用的是 vector
所以你实际上有两个 Symbol 实例,它们的指针指向同一个参数数组,当一个被销毁时,另一个尝试释放已经释放的东西。
参见:http://www.tutorialspoint.com/cplusplus/cpp_copy_constructor.htm
关于c++ - 释放内存时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968817/