Connecting Tech Pros Worldwide Forums | Help | Site Map

why delete operator crashes

Nemo
Guest
 
Posts: n/a
#1: Feb 23 '06
Hello Folks,
I need to manipulate a list of char strings as follows, but when I want to
delete the pointer created with new at the end, delete operator crashes, any
idea?

char* list[2000];
while(...)
{
list[MyCounter] = new char [Stringlength];
strncpy(list[MyCounter], c_TempFilename, Stringlength);
list[MyCounter][Stringlength] = '\0';
MyCounter++;
}

for (i=0; i<MyCounter;i++)
{
delete [] list[i];
}

Thanks Everybody in advance.



Phlip
Guest
 
Posts: n/a
#2: Feb 23 '06

re: why delete operator crashes


Nemo wrote:
[color=blue]
> char* list[2000];[/color]

Add this line:

assert(NULL == list[0]);

It might fail.

(It also might pass, so don't rely only on it.)
[color=blue]
> for (i=0; i<MyCounter;i++)
> {
> delete [] list[i];
> }[/color]

You are allowed to delete[] a NULL pointer. But you should not delete a
garbage pointer; one containing an undefined value not returned by new.

All uninitialized stack variables and heap variables contain garbage. Always
initialize, at least like this:

char *list[2000] = { NULL };

Now learn to use std::string and stop writing high-risk code with raw
pointers!

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Jay_Nabonne
Guest
 
Posts: n/a
#3: Feb 23 '06

re: why delete operator crashes


On Thu, 23 Feb 2006 15:26:35 -0500, Nemo wrote:
[color=blue]
> char* list[2000];
> while(...)
> {
> list[MyCounter] = new char [Stringlength];[/color]

Should be:

list[MyCounter] = new char [Stringlength+1];
[color=blue]
> strncpy(list[MyCounter], c_TempFilename, Stringlength);[/color]

....since you're sticking a 0 on the end.
[color=blue]
> list[MyCounter][Stringlength] = '\0';
> MyCounter++;
> }
>[/color]

- Jay
Daniel T.
Guest
 
Posts: n/a
#4: Feb 23 '06

re: why delete operator crashes


In article <dtl5pr$pv2$1@dns3.cae.ca>, "Nemo" <nemo@no_email.com>
wrote:
[color=blue]
> Hello Folks,
> I need to manipulate a list of char strings as follows, but when I want to
> delete the pointer created with new at the end, delete operator crashes, any
> idea?
>
> char* list[2000];
> while(...)
> {
> list[MyCounter] = new char [Stringlength];
> strncpy(list[MyCounter], c_TempFilename, Stringlength);
> list[MyCounter][Stringlength] = '\0';[/color]

Off by one error, list[MyCounter] has no element at [Stringlength].
[color=blue]
> MyCounter++;
> }
>
> for (i=0; i<MyCounter;i++)
> {
> delete [] list[i];
> }
>
> Thanks Everybody in advance.[/color]

The above problem is probably clobbering whatever code your delete
operator needs in order to delete the array.

I strongly suggest you use std::string.

std::vector<std::string> list;
while ( ... )
{
list.push_back( std::string( c_TempFilename ) );
}

// no delete needed.


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Nemo
Guest
 
Posts: n/a
#5: Feb 23 '06

re: why delete operator crashes


Phlip,
I wish I could use std::string, but the code that I am trying to fix is a C
code and compiler does not compile it.
the pointers are not garbage, they are initialized with strings:

list[MyCounter] = new char [Stringlength];
strncpy(list[MyCounter], c_TempFilename, Stringlength);
list[MyCounter][Stringlength] = '\0';

Regartds

"Phlip" <phlip2005@gmail.com> wrote in message
news:D%oLf.16780$tb3.294@newssvr24.news.prodigy.ne t...[color=blue]
> Nemo wrote:
>[color=green]
> > char* list[2000];[/color]
>
> Add this line:
>
> assert(NULL == list[0]);
>
> It might fail.
>
> (It also might pass, so don't rely only on it.)
>[color=green]
> > for (i=0; i<MyCounter;i++)
> > {
> > delete [] list[i];
> > }[/color]
>
> You are allowed to delete[] a NULL pointer. But you should not delete a
> garbage pointer; one containing an undefined value not returned by new.
>
> All uninitialized stack variables and heap variables contain garbage.[/color]
Always[color=blue]
> initialize, at least like this:
>
> char *list[2000] = { NULL };
>
> Now learn to use std::string and stop writing high-risk code with raw
> pointers!
>
> --
> Phlip
> http://www.greencheese.org/ZeekLand <-- NOT a blog!!![/color]


Nemo
Guest
 
Posts: n/a
#6: Feb 23 '06

re: why delete operator crashes


Jay,
that was the problem. Thanks. Now it works.
Regards

"Jay_Nabonne" <jnabonne@pacbello.net> wrote in message
news:pan.2006.02.23.20.48.57.969000@pacbello.net.. .[color=blue]
> On Thu, 23 Feb 2006 15:26:35 -0500, Nemo wrote:
>[color=green]
> > char* list[2000];
> > while(...)
> > {
> > list[MyCounter] = new char [Stringlength];[/color]
>
> Should be:
>
> list[MyCounter] = new char [Stringlength+1];
>[color=green]
> > strncpy(list[MyCounter], c_TempFilename, Stringlength);[/color]
>
> ...since you're sticking a 0 on the end.
>[color=green]
> > list[MyCounter][Stringlength] = '\0';
> > MyCounter++;
> > }
> >[/color]
>
> - Jay[/color]


Nemo
Guest
 
Posts: n/a
#7: Feb 23 '06

re: why delete operator crashes


Daniel,
As I mentioned before, I would love to use templates but in this code
compiler does not allow me to use them. So I am stuck with their old style.
thanks.
Regards

"Daniel T." <postmaster@earthlink.net> wrote in message
news:postmaster-D9126F.16081223022006@news.east.earthlink.net...[color=blue]
> In article <dtl5pr$pv2$1@dns3.cae.ca>, "Nemo" <nemo@no_email.com>
> wrote:
>[color=green]
> > Hello Folks,
> > I need to manipulate a list of char strings as follows, but when I want[/color][/color]
to[color=blue][color=green]
> > delete the pointer created with new at the end, delete operator crashes,[/color][/color]
any[color=blue][color=green]
> > idea?
> >
> > char* list[2000];
> > while(...)
> > {
> > list[MyCounter] = new char [Stringlength];
> > strncpy(list[MyCounter], c_TempFilename, Stringlength);
> > list[MyCounter][Stringlength] = '\0';[/color]
>
> Off by one error, list[MyCounter] has no element at [Stringlength].
>[color=green]
> > MyCounter++;
> > }
> >
> > for (i=0; i<MyCounter;i++)
> > {
> > delete [] list[i];
> > }
> >
> > Thanks Everybody in advance.[/color]
>
> The above problem is probably clobbering whatever code your delete
> operator needs in order to delete the array.
>
> I strongly suggest you use std::string.
>
> std::vector<std::string> list;
> while ( ... )
> {
> list.push_back( std::string( c_TempFilename ) );
> }
>
> // no delete needed.
>
>
> --
> Magic depends on tradition and belief. It does not welcome observation,
> nor does it profit by experiment. On the other hand, science is based
> on experience; it is open to correction by observation and experiment.[/color]


Ian Collins
Guest
 
Posts: n/a
#8: Feb 23 '06

re: why delete operator crashes


Nemo wrote:[color=blue]
> Phlip,
> I wish I could use std::string, but the code that I am trying to fix is a C
> code and compiler does not compile it.[/color]

But it compiles new and delete....

--
Ian Collins.
Phlip
Guest
 
Posts: n/a
#9: Feb 23 '06

re: why delete operator crashes


Ian Collins wrote:
[color=blue]
> Nemo wrote:[color=green]
>> Phlip,
>> I wish I could use std::string, but the code that I am trying to fix is a
>> C code and compiler does not compile it.[/color][/color]

I should have caught the issue Daniel T. did. I stopped at the first thing I
didn't see - uninitialized pointers.

You should implement my fix as well. And in general, you should C++ this C
code as much as possible.
[color=blue]
> But it compiles new and delete....[/color]

I can imagine a legitimate C++ compiler compiling legacy C code and getting
stuck on std::string.

(I can also imagine a poster just a little naive to C++, and forgetting to
#include <string>.)

I can't imagine the compiler handling new and delete but not templates. The
suggestions to Nemo are...

- ensure you study C++ independently, in its native environment
- write unit tests
- after this code works, refactor it to extract _little_ functions
- make sure the compiler itself is robust and not causing trouble
- carefully examine all the code for undefined behavior
- fix any excesses, possibly re-using the little extracted functions

Migrating to pure C++ is only possible after this C/C++ hybrid is
_completely_ under control and safe to change.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Clark S. Cox III
Guest
 
Posts: n/a
#10: Feb 23 '06

re: why delete operator crashes


On 2006-02-23 16:09:50 -0500, "Nemo" <nemo@no_email.com> said:
[color=blue]
> Phlip,
> I wish I could use std::string, but the code that I am trying to fix is a C
> code and compiler does not compile it.[/color]

If you're writing C code, then you can't use new or delete either.
[color=blue]
> the pointers are not garbage, they are initialized with strings:
>
> list[MyCounter] = new char [Stringlength];
> strncpy(list[MyCounter], c_TempFilename, Stringlength);
> list[MyCounter][Stringlength] = '\0';[/color]

bzzzzt. There's your error, you've run off the end of the array. What
you've done is essentially the same as:

char foo[5];
foo[5] = '\0';
//foo has 5 elements: [0],[1],[2],[3],[4]
//foo[5] is *not* a valid part of foo


--
Clark S. Cox, III
clarkcox3@gmail.com

Andrew Koenig
Guest
 
Posts: n/a
#11: Feb 23 '06

re: why delete operator crashes


"Nemo" <nemo@no_email.com> wrote in message news:dtl8b0$su1$1@dns3.cae.ca...
[color=blue]
> the pointers are not garbage, they are initialized with strings:
>
> list[MyCounter] = new char [Stringlength];
> strncpy(list[MyCounter], c_TempFilename, Stringlength);
> list[MyCounter][Stringlength] = '\0';[/color]

This code is wrong. If you use "new char[Stringlength]" to initialize a
variable, then the elements of that variable have indices of 0 through
Stringlength-1, inclusive. Therefore, when you execute

list[MyCounter][Stringlength] = '\0';

you are overwriting memory outside the bounds of the array that you
allocated, and the effect is undefined.


Ian Collins
Guest
 
Posts: n/a
#12: Feb 23 '06

re: why delete operator crashes


Nemo wrote:[color=blue]
> Daniel,
> As I mentioned before, I would love to use templates but in this code
> compiler does not allow me to use them. So I am stuck with their old style.[/color]

Please don't top post.

I'm curious, which compiler is this, Embedded C++ maybe?

--
Ian Collins.
Daniel T.
Guest
 
Posts: n/a
#13: Feb 24 '06

re: why delete operator crashes


In article <dtl984$46f$1@dns3.cae.ca>, "Nemo" <nemo@no_email.com>
wrote:
[color=blue]
> Daniel,
> As I mentioned before, I would love to use templates but in this code
> compiler does not allow me to use them. So I am stuck with their old style.
> thanks.[/color]

Then implement a vector and string class yourself, without using
templates. It's not that hard... Just a thought, are you using C++ or C?
Maybe you are in the wrong group?


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Nemo
Guest
 
Posts: n/a
#14: Feb 24 '06

re: why delete operator crashes


Guys,
Sorry, you are all right, my mistake. I was testing my function with a C++
compiler and I did not notice that the C compiler that compiles my main code
does not compile new and delete. But you were right about the problem in my
code, I was trying to access invalide place because of wrong size. Thanks
every body, I learned something.
Regards
Nemo


"Nemo" <nemo@no_email.com> wrote in message news:dtl5pr$pv2$1@dns3.cae.ca...[color=blue]
> Hello Folks,
> I need to manipulate a list of char strings as follows, but when I want to
> delete the pointer created with new at the end, delete operator crashes,[/color]
any[color=blue]
> idea?
>
> char* list[2000];
> while(...)
> {
> list[MyCounter] = new char [Stringlength];
> strncpy(list[MyCounter], c_TempFilename, Stringlength);
> list[MyCounter][Stringlength] = '\0';
> MyCounter++;
> }
>
> for (i=0; i<MyCounter;i++)
> {
> delete [] list[i];
> }
>
> Thanks Everybody in advance.
>
>[/color]


Closed Thread


Similar C / C++ bytes