M
michael
Hi All,
I have the following:
const int LENGTH = 5;
void limitNameLength(string inText, char *&outText, int outLength){
if(static_cast<int>(inText.length()) > outLength){
inText = inText.substr(0, outLength);
}
strcpy(outText, inText.c_str());
if(static_cast<int>(strlen(outText)) < outLength){
for(int i = strlen(outText); i < outLength; i++){
outText = ' ';
}
}
outText[outLength] = static_cast<char>(NULL);
}
int main(){
char *temp;
string name = "some long name";
temp = new char[LENGTH];
cout << "before : " << name << "\n";
limitNameLength(name, temp, LENGTH);
cout << "after : " << temp << "\n";
delete [] temp;
return 0;
}
if I comment out the call to limitNameLength the delete [] works ok. If I
don't the delete [] never returns.....
Can anyone tell me why? As far as I can see all I have done is pass the
array to another function to manipulate it a bit then delete it. Why does
delete not work?
Thanks for your help
Michael
I have the following:
const int LENGTH = 5;
void limitNameLength(string inText, char *&outText, int outLength){
if(static_cast<int>(inText.length()) > outLength){
inText = inText.substr(0, outLength);
}
strcpy(outText, inText.c_str());
if(static_cast<int>(strlen(outText)) < outLength){
for(int i = strlen(outText); i < outLength; i++){
outText = ' ';
}
}
outText[outLength] = static_cast<char>(NULL);
}
int main(){
char *temp;
string name = "some long name";
temp = new char[LENGTH];
cout << "before : " << name << "\n";
limitNameLength(name, temp, LENGTH);
cout << "after : " << temp << "\n";
delete [] temp;
return 0;
}
if I comment out the call to limitNameLength the delete [] works ok. If I
don't the delete [] never returns.....
Can anyone tell me why? As far as I can see all I have done is pass the
array to another function to manipulate it a bit then delete it. Why does
delete not work?
Thanks for your help
Michael