473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
May 12 '07 #1
5 1749
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
"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Westcoast Sheri | last post by:
Any way to do a simple delete from array? In other words, what would be the *easiest* (and fastest php runtime) way to delete "banana" from the following array: $my_array = array( "apple",...
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
6
by: Scott Niu | last post by:
Hi, I have this following simple c++ program, it will produce memory leak ( see what I did below ). My observation also showed that: There will be a mem leak when all the 3 conditions are true:...
13
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
2
by: Les Juby | last post by:
I've used a simple javascript for some time (no entries required up in the <head> tag) that asks for a confirmation before deleting. ie. <a href="/delete.asp?which=345 %>"...
3
by: Bore Biko | last post by:
Dear, I don't have enought money to by a original Visual C++, so I use Visual C++ 6.0 Enterprise edition, this version doesen't have a HELP. Most of my friends programmers praise C++, as a...
3
by: simon | last post by:
I get from database something like this: DAY HOUR PRICE --------------------------------------------- MONDAY 10 100 MONDAY 11 ...
1
by: E.T. Grey | last post by:
I have been busting my nut over this for pretty much most of the day and it is driving me nuts. I posted this to an mySQL ng yesterday and I have not had any response (I'm pulling my hair out...
4
by: Dmytro Bablinyuk | last post by:
I came across several possible ways of allocating memory for objects, for example: 1. malloc(sizeof(T)*3)/free - raw memory 2. new T/delete - buffer would be initialized to...
2
by: dave | last post by:
Hi, I have searched for the answer for this error message without success. I have seen the question many times though:) I create an ASP.NET project (VS 2005, C#), and use a very simple .mdf...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.