释放内存引起的问题
//别人问我,我问别人。
//谁能给分析下 那里出问题了.
#include <string.h>
#include <iostream>
struct CStrData
{
int nDataLength;
int nAllocLength;
char* data()
{ return (char*)(this+1); }
};
class CStr
{
public:
CStr();
CStr( char * str);
CStr( CStr & obj );
~CStr();
private:
void AllocBuffer( int nlen ) ;
void Init();
CStrData* GetCStrData() const;
void ReleaseBuffer();
int GetReferenceCount();
int GetDataLength() const;
public:
operator char * () const { return ( char * ) m_pchData ; }
CStr& operator = ( const CStr& stringSrc );
char & operator []( const int & index );
int GetLen() const;
int GetAllocLength() const;
char * GetBuffer();
protected:
char * m_pchData;
};
bool operator == ( CStr & CStrObj1, CStr & CStrObj2 );
using namespace std;
CStr::CStr()
{
Init();
}
CStr::CStr( char * str )
{
int len = strlen( str ) + 1 ;
Init();
AllocBuffer( len );
strcpy( GetBuffer(), (char *)str);
}
CStr::CStr( CStr & obj )
{
char * ObjBuffer = obj.GetBuffer();
Init();
AllocBuffer( obj.GetLen()+1 );
strcpy( GetBuffer(),(char *)ObjBuffer);
*GetCStrData() = *obj.GetCStrData();
}
CStr::~CStr()
{
ReleaseBuffer();
}
void CStr::ReleaseBuffer()
{
int i = 0;
if( m_pchData )
{
m_pchData = ( char * )GetCStrData();
delete[] m_pchData;
}
m_pchData = NULL;
}
char * CStr::GetBuffer()
{
return m_pchData;
}
void CStr::Init()
{
m_pchData = NULL;
//ReleaseBuffer();
}
void CStr::AllocBuffer( int nLen )
{
if( nLen == 0 )
Init();
else
{
CStrData* pData;
if( nLen <= 64 )
{
pData = ( CStrData* )
new char[ sizeof(CStrData) + ( 64 )*sizeof( char )];
pData->nAllocLength = 64;
}
else if( nLen <= 128 )
{
pData = ( CStrData* )new
char[ sizeof(CStrData) + ( 128 )*sizeof( char )];
pData->nAllocLength = 128;
}
else if( nLen <= 256 )
{
pData = ( CStrData* )new
char[ sizeof(CStrData) + ( 256 )*sizeof( char )];
pData->nAllocLength = 256;
}
else if( nLen <= 512 )
{
pData = ( CStrData* )new
char[ sizeof(CStrData) + ( 512 )*sizeof( char )];
pData->nAllocLength = 512;
}
else
{
pData = ( CStrData* )new
char[ sizeof(CStrData) + ( nLen )*sizeof( char )];
pData->nAllocLength = nLen;
}
pData->data()[nLen] = '\0';
pData->nDataLength = nLen;
m_pchData = pData->data();
}
}
CStrData* CStr::GetCStrData() const
{
return ( CStrData* ) m_pchData - 1 ;
}
int CStr::GetReferenceCount()
{
return ( ( CStrData* ) m_pchData - 1 )->nDataLength;
}
int CStr::GetDataLength() const
{
return( ( CStrData* ) m_pchData - 1 )->nDataLength;
}
int CStr::GetAllocLength() const
{
return m_pchData ? ( ( CStrData* ) m_pchData - 1 )->nAllocLength : 0;
}
int CStr::GetLen() const
{
return GetDataLength()- 1;
}
char & CStr::operator [] ( const int & index )
{
return m_pchData[ index ];
}
bool operator == ( CStr & CStrObj1, CStr & CStrObj2 )
{
if( CStrObj1.GetLen() == CStrObj2.GetLen() )
{
if ( !strcmp( CStrObj1.GetBuffer(), CStrObj2.GetBuffer() ) )
return true;
else
return false;
}
else
return false;
}
CStr& CStr::operator=( const CStr& stringSrc )
{
if( GetAllocLength() != stringSrc.GetAllocLength() )
{
ReleaseBuffer();
//???????????????????????????
//改写成AllocBuffer( stringSrc.GetAllocLength() );
//或者是64的倍数就报错?????AllocBuffer( 64*n );
////???????????????????????????
AllocBuffer( 64 );
*GetCStrData() = *stringSrc.GetCStrData();
}
strcpy( GetBuffer(), stringSrc.m_pchData );
return *this;
}
void main()
{
CStr str1(" word");
CStr str3;
str3 = "hello ";
}