By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

simple problem with delete []

P: n/a
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
May 12 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hello:

When I programed this one in MinGW stdio.It worked very well.

And I didn't find any grammer error in yours.

May 12 '07 #2

P: n/a
"michael" <sp**@begone.netwrote in message
news:46**********************@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

May 12 '07 #3

P: n/a
i noticed one line in your code:
>>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" <sp**@begone.net写入消息新闻:46******************* ***@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){
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

May 12 '07 #4

P: n/a

"Ivan Vecerina" <_I*******************@ivan.vecerina.comwrote in message
news:a5***************************@news.hispeed.ch ...
"michael" <sp**@begone.netwrote in message
news:46**********************@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:
ummm... no it can't
you will notice that I am padding the output string with spaces so it is
always the same length. strncpy() will not do this for me.
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].
yeah, thanks for that........case of looking but not seeing :-)
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

May 12 '07 #5

P: n/a
Paolo Maldini wrote:
i noticed one line in your code:
>>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() {
char*
temp = new char[LENGTH];
......
limitNameLength(name, temp, LENGTH-1);
......
delete temp;
delete[] temp;
......
}
Also, please don't top-post. See my signature.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
May 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.