 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
michael Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: simple problem with delete [] |
|
|
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[i] = ' ';
}
}
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 |
|
| Back to top |
|
 |
Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: simple problem with delete [] |
|
|
Hello:
When I programed this one in MinGW stdio.It worked very well.
And I didn't find any grammer error in yours. |
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: simple problem with delete [] |
|
|
"michael" <spam (AT) begone (DOT) net> wrote in message
news:46455386$0$9097$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
: Hi All,
:
: I have the following:
:
: const int LENGTH = 5;
:
: void limitNameLength(string inText, char *&outText, int outLength){
You can have char* outText - no need to make it a reference
since the function does not change the pointer address itself.
: 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[i] = ' ';
: }
: }
All of the previous can be simply written as:
void limitNameLength( string const& inText
, char *outText, int const outLength)
{
strncpy( outText, inText.c_str(), outLength );
//NB: if outLength<inText.size(), there will be no final '\0'
: outText[outLength] = static_cast<char>(NULL);
why not just: '\0' ?
This effectively relies on outText having a length
of outLength+1 !
: }
:
: 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?
When you allocate an array of size LENGTH, the valid indices
are 0 .. LENGTH-1. limitNameLength writes over outText[LENGTH].
The buffer provided to limitNameLength needs to have 1 more character
than the requested maximum length of the string.
If outLength is to be the maximum buffer size, you could change
the last line to:
outText[outLength-1] = '\0';
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com |
|
| Back to top |
|
 |
Paolo Maldini Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: simple problem with delete [] |
|
|
i noticed one line in your code:
| Quote: | strcpy(outText, inText.c_str());
the length of outText is 5, and also the length of inText is 5, overflow. no |
room to store '\0' in outText.
so the calling sequence should be as below:
int main() {
temp = new char[LENGTH];
......
limitNameLength(name, temp, LENGTH-1);
......
delete temp;
......
}
"michael" <spam (AT) begone (DOT) net> 写入消息新闻:46455386$0$9097$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
| Quote: | 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[i] = ' ';
}
}
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
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|