二维指针的new和delete
下面的这个类是与数据库相关的,但这个类会出现内存泄露,不知道怎么修改,对指针的理解不太深刻,忘各位能帮个忙,帮小弟修改一下,最好来点注释...
ClassDataSet.h
- C/C++ code
#include "stdafx.h"#include <tchar.h>#include <string>using namespace std;#ifndef ClassDataSet__H_H#define ClassDataSet__H_Hclass ClassDataSet{private : string **value; long NumCol; long NumRow;public: ClassDataSet(); void ClearValue(); void InitialValue(long TotalRow,long TotalCol); void InputVaule(string v,long EachRow,long EachCol); long RowCount(); long ColCount(); string ReValue(long SelectRow,long SelectCol);};#endifClassDataSet.cpp
- C/C++ code
#include"ClassDataSet.h"#include <stdlib.h>using namespace std;ClassDataSet::ClassDataSet(){ this->NumCol=0; this->NumRow=0;}long ClassDataSet::RowCount(){ return this->NumRow;}long ClassDataSet::ColCount(){ return this->NumCol;}void ClassDataSet::InitialValue(long TotalRow,long TotalCol){ const long rowTemp=TotalRow; const long colTemp=TotalCol; this->value=new string*[rowTemp];//new 了之后没有在析构函数当中delete,不知如何修改这个类. for(long k=0;k<rowTemp;++k) { this->value[k]=new string[colTemp]; } this->NumRow=TotalRow; this->NumCol=TotalCol;}void ClassDataSet::InputVaule(string v,long CurrentRow,long CurrentCol){ this->value[CurrentRow][CurrentCol]=v;}string ClassDataSet::ReValue(long CurrentRow,long CurrentCol){ if(CurrentRow>=this->NumRow||CurrentCol>=this->NumCol) { return "Overflow"; } else { return this->value[CurrentRow][CurrentCol]; }}void ClassDataSet::ClearValue(){ this->value=new string*[0]; this->value[0]=new string[0]; this->NumCol=0; this->NumRow=0; delete[] *this->value;}在另一个类中定义了类型为ClassDataSet的一个成员变量
- C/C++ code
class MyControlSqlServer//连接数据库类 连接之前需要打开 {public: MyControlSqlServer(_bstr_t SqlCon);//构造函数,配置数据库 ClassDataSet Select(_bstr_t Action);//Select语句 bool WriteDatebase(_bstr_t Action);//Insert,Update,delete,create语句 bool Open(); _ConnectionPtr m_pConnection;//连接方式 _RecordsetPtr m_pRecordset;//虚拟表 void close(); //bool Update(_bstr_t Action);//Update语句private: _bstr_t strConnect; ClassDataSet obj;};- C/C++ code
ClassDataSet MyControlSqlServer::Select(_bstr_t Action){ try { _variant_t temp; if(m_pConnection==NULL) { this->obj.ClearValue(); return this->obj;//链接不上 } _bstr_t bstrSQL(Action); m_pRecordset.CreateInstance(__uuidof(Recordset)); // 取得表中的记录 m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); long ColCount=m_pRecordset->GetFields()->Count;//获得一条记录的字段数,列总数FieldsPtr long RowCount=0;//行总数 while(!m_pRecordset->adoEOF)//用循环获得行总数 { RowCount++; m_pRecordset->MoveNext();///移到下一条记录 } this->obj.InitialValue(RowCount,ColCount); m_pRecordset->MoveFirst(); long CurrentRow=0; long CurrentCol=0; while(!m_pRecordset->adoEOF) { for(long CurrentCol=0;CurrentCol<ColCount;++CurrentCol) { temp=m_pRecordset->GetCollect((long)CurrentCol);///取得第i列的值,从0开始计数, string a; if(temp.vt==1) a="NULL"; else a=(_bstr_t)temp; this->obj.InputVaule(a,CurrentRow,CurrentCol); } m_pRecordset->MoveNext();///移到下一条记录 ++CurrentRow; } return this->obj; } catch(_com_error e) { //cout<<(string)(_bstr_t)e.Description()<<endl; this->obj.ClearValue(); return this->obj; }}
使用的时候
MyControlSqlServer dbmanger;
dbmanger.Select();
在调试窗口提示内存泄露.
[解决办法]
二维指针的new和delete
New:
A** ga = new A*[m];
for(int i = 0; i < m; i++)
{
ga[i] = new A[n];
}
Delete:
for(int i = 0; i < m; i++)
{
delete []ga[i];
}
delete []ga;
应此
- C/C++ code
void ClassDataSet::ClearValue(){ for(long k=0;k<NumRow;++k) { delete[] this->value[k]; } delete[] this->value; this->NumCol=0; this->NumRow=0;}
[解决办法]
按LS的改,另外你可能还得写拷贝构造函数和operator=。
[解决办法]
先MARK..
[解决办法]
[解决办法]
好像没见到 析构函数。 开了空间没释放吧。
[解决办法]
- C/C++ code
ClassDataSet::~ClassDataSet(){ for(long k=0;k<NumRow;++k) { delete[] this->value[k]; } delete[] this->value; this->NumCol=0; this->NumRow=0;}