new/delete prop
Jeg folkens,Jeg har følgende klasse:
// SharpeModel.h: interface for the SharpeModel class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SHARPEMODEL_H__48AA2D96_FDCD_446C_A46E_B9E365BCE6CE__INCLUDED_)
#define AFX_SHARPEMODEL_H__48AA2D96_FDCD_446C_A46E_B9E365BCE6CE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class SharpeModel
{
public:
double *CovarianceMatrix;
double *StockReturns;
double *PortfolioWeights;
double *lbWeights;
double *ubWeights;
double *onesVektor;
double *dummy;
double SharpeRatio;
long Assets;
bool allocated;
void SetUp(long Assets);
SharpeModel();
virtual ~SharpeModel();
};
#endif // !defined(AFX_SHARPEMODEL_H__48AA2D96_FDCD_446C_A46E_B9E365BCE6CE__INCLUDED_)
// SharpeModel.cpp: implementation of the SharpeModel class.
//
//////////////////////////////////////////////////////////////////////
#include \"SharpeModel.h\"
void SharpeModel::SetUp(long lAssets)
{
int i;
int j;
this->Assets=lAssets;
if(allocated==true){
delete CovarianceMatrix;
delete PortfolioWeights;
delete StockReturns;
delete lbWeights;
delete ubWeights;
delete onesVektor;
delete dummy;}
CovarianceMatrix = new double[Assets*Assets];
PortfolioWeights=new double[Assets];
StockReturns=new double[Assets];
lbWeights=new double[Assets];
ubWeights=new double[Assets];
onesVektor=new double[Assets];
dummy=new double[250000];
SharpeRatio=0;
allocated=true;
for(i=0; i<Assets;i++)
{
lbWeights[i]=0;
ubWeights[i]=100;
onesVektor[i]=1;
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SharpeModel::SharpeModel()
{
allocated=false;
}
SharpeModel::~SharpeModel()
{
if(allocated==true){
delete CovarianceMatrix;
delete PortfolioWeights;
delete StockReturns;
delete lbWeights;
delete ubWeights;
delete onesVektor;
delete dummy;}
}
Sagen er den at hvis jeg prøver at fyre en delete på en af mine pointere så brager skidtet ned i dbgheap.c.
Compileren er MS VC++ 6.
Hvordan kan jeg lave en skudsikker metode, således at når SharpeModel::Setup bliver kaldet, at jeg så får frigjort evt. \"gamle\" pointere og reallokeret pointerne efter den nye størrelse (lAssets)?
Klassen fungerer fint hvis jeg dropper alt om delete, men tilgengæld kan jeg se at programmet æder af min heap hver gang jeg kalder SharpeModel::Setup.. Da jeg kan forvente op til 1000 assets vil det betyde at den æder ca. 4mb af min heap hver gang jeg kalder SharpeModel::Setup.
(og ja, dummy har jeg kun tilføjet af testhensyn :o) ).
/Jennemaan