Hello,
I was recently looking through some of my code and found what I believe to be a memory leak. It is in a function that appends two strings and I am not sure how to resolve it. Here is the function:
const char *strapp(const char *str,const char *s)
{
int size,strSize,sSize;
for (strSize=0;str[strSize];++strSize);
for (sSize=0;s[sSize];++sSize);
size=strSize+sSize;
char *ret=new char[size+1];
for (size_t i=0; i<strSize; ++i)
ret[i]=str[i];
for (size_t i=0; i<sSize; ++i)
ret[strSize+i]=s[i];
ret[size]=0;
return ret;
}
Note that this includes a new
operator but would leave it up to the untrustworthy user of the function to delete
the resulting pointer (which I didn't believe was possible, since it is a const pointer, but apparently it is). I am wondering if there is a way around this without using the <cstring>
or <string>
libraries (as they are overkill in this situation).