473,396 Members | 1,814 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,396 software developers and data experts.

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.
Feb 23 '06 #1
13 2026
Nemo wrote:
char* list[2000];
Add this line:

assert(NULL == list[0]);

It might fail.

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


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!!!
Feb 23 '06 #2
On Thu, 23 Feb 2006 15:26:35 -0500, Nemo wrote:
char* list[2000];
while(...)
{
list[MyCounter] = new char [Stringlength];
Should be:

list[MyCounter] = new char [Stringlength+1];
strncpy(list[MyCounter], c_TempFilename, Stringlength);
....since you're sticking a 0 on the end.
list[MyCounter][Stringlength] = '\0';
MyCounter++;
}


- Jay
Feb 23 '06 #3
In article <dt**********@dns3.cae.ca>, "Nemo" <nemo@no_email.com>
wrote:
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';
Off by one error, list[MyCounter] has no element at [Stringlength].
MyCounter++;
}

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

Thanks Everybody in advance.


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.
Feb 23 '06 #4
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" <ph*******@gmail.com> wrote in message
news:D%*****************@newssvr24.news.prodigy.ne t...
Nemo wrote:
char* list[2000];
Add this line:

assert(NULL == list[0]);

It might fail.

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


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!!!

Feb 23 '06 #5
Jay,
that was the problem. Thanks. Now it works.
Regards

"Jay_Nabonne" <jn******@pacbello.net> wrote in message
news:pa****************************@pacbello.net.. .
On Thu, 23 Feb 2006 15:26:35 -0500, Nemo wrote:
char* list[2000];
while(...)
{
list[MyCounter] = new char [Stringlength];


Should be:

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


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


- Jay

Feb 23 '06 #6
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." <po********@earthlink.net> wrote in message
news:po******************************@news.east.ea rthlink.net...
In article <dt**********@dns3.cae.ca>, "Nemo" <nemo@no_email.com>
wrote:
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';


Off by one error, list[MyCounter] has no element at [Stringlength].
MyCounter++;
}

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

Thanks Everybody in advance.


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.

Feb 23 '06 #7
Nemo wrote:
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.


But it compiles new and delete....

--
Ian Collins.
Feb 23 '06 #8
Ian Collins wrote:
Nemo wrote:
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.

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.
But it compiles new and delete....


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!!!
Feb 23 '06 #9
On 2006-02-23 16:09:50 -0500, "Nemo" <nemo@no_email.com> said:
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.
If you're writing C code, then you can't use new or delete either.
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';


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
cl*******@gmail.com

Feb 23 '06 #10
"Nemo" <nemo@no_email.com> wrote in message news:dt**********@dns3.cae.ca...
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';


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.
Feb 23 '06 #11
Nemo wrote:
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.


Please don't top post.

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

--
Ian Collins.
Feb 23 '06 #12
In article <dt**********@dns3.cae.ca>, "Nemo" <nemo@no_email.com>
wrote:
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.


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.
Feb 24 '06 #13
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:dt**********@dns3.cae.ca...
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.

Feb 24 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
5
by: Ruslan Vorobey | last post by:
Hi All. Does anybody saw any C++ new operator implementations? I need just to know does it use malloc? Especially I interesting about standard C++ libs implementation under AIX 5.1L. Thank...
4
by: lothar.behrens | last post by:
Hi, I have problems to delare a delete operator in a class and use it to check for valid pointer. Using release() with an additional validation routine from a separate malloc library avoids...
7
by: Nemo | last post by:
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;...
3
by: ujjwaltrivedi | last post by:
Can I use any method to allocate /deallocate memory other than new/ delete operators? Actually the problem is while using delete operator in my ".sqc" files the application crashes. It seems the...
5
by: Renato | last post by:
I have an array of pointers to class Shape. I create 4 items and display their values. shapes Shape *shapes; shapes = new Shape (p1); shapes = new Triangle (p1); shapes = new Polygon (p2);
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.