simple problem with delete []

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
 
K

kakaxyl2002

Hello:

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

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

Ivan Vecerina

: 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 = ' ';
: }
: }
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';
 
P

Paolo Maldini

i noticed one line in your code: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 said:
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
 
M

michael

Ivan Vecerina said:
: 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 = ' ';
: }
: }
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';
 
T

Thomas J. Gritzan

Paolo said:
i noticed one line in your code: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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,216
Latest member
DarrelLho

Latest Threads

Top