why delete operator crashes | | |
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. | | | | 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!!! | | | | 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 | | | | 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. | | | | 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] | | | | 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] | | | | 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] | | | | 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. | | | | 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!!! | | | | 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 | | | | 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. | | | | 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. | | | | 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. | | | | 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] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|