malloc and free in a class
Hi,
I am trying to make few class similar to VB.
I thought that I will start with making String Class.
Code:
cString::~cString()
{
free(strStr);
}
void cString::operator =(char *strNew)
{
if (strStr != NULL)
{
free(strStr);
strStr = (char *)malloc(sizeof(char)* strlen(strNew));
}
else
{
strStr = (char *)malloc(sizeof(char)* strlen(strNew));
}
strcpy((char *)strStr,strNew);
}
cString &cString::operator+=(cString strNew)
{
char * strTmp;
strTmp = (char *) malloc(sizeof(char) * this->Len());
strcpy(strTmp,(char *)strStr);
strStr = (char *)malloc(sizeof(char)*strNew.Len());
strcpy((char *)strStr,strTmp);
strcat((char *)strStr,strNew.Value());
free(strTmp);
return *this;
}
Code:
class cString
{
private:
void *strStr;
public:
cString()
{
strStr = NULL;
}
~cString();
long Len()
{
return strlen((char *)strStr);
}
char *Value()
{
return (char *)strStr;
};
cString &operator+=(cString strNew);
void operator =(char *strNew);
};
I am unable to free the maemory allocated by malloc.
I am able to realloc but not free.. How am I supposed to do it..
Thanks,
Pradeep