Hi,
An exercise from my book is to implement my own version of the strdup function, which returns a copy of a string allocated on the freestore.
This is my code:
My question is that when I create name, I allocate memory on the free store. When I return this, I take it that it is still allocated on the freestore and when whatever has called it finishes using it, that is when I call delete[]? Is that correct?Code:char *strdup(const char* ch)
{
const char* p = ch;
int size = 0;
while(*p){
++size;
++p;
}
int index = 0;
char* name = new char[size];
while(*ch){
name[index] = *ch;
cout << *ch;
++ch;
++index;
}
return name;
}
BTW, the exercise states that I cannot use any standard library function, hence my own version of size.
Thanks.