Advertisement

new hangs me program

Started by May 27, 2000 05:55 AM
16 comments, last by zyzzy 24 years, 7 months ago
I think it is my compiler which breaks it. I single-stepped through code that had a new statement which worked. After that it has stopped working :/
ErikPost: I don't see any mention of an exception being thrown from new() in MSDN 6.0a. I had to search for std::bad_alloc because it wasn't even in the index of the docs.

In the following program, new() returns NULL because it can't allocate memory, but it doesn't throw an exception.


// Test6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include &ltexception>
#include &ltmemory>
#include &ltiostream.h>


int main(int argc, char* argv[])
{
try {
char* p = new char[64000000000];
delete[] p;
}

catch( ... ) {
cout << "allocation failed\n";
}

return 0;
}



BTW, your example code will not throw an exception either.



- null_pointer
Sabre Multimedia


Edited by - null_pointer on May 28, 2000 8:12:40 AM
Advertisement
null_pointer: did you try compiling with /GX? Or try including <new> instead of <memory>.

I tested your code with BCC 5.5, and it works like it should: it throws a bad_alloc exception. I''m not sure how to get it to work in VC++ 6, because I don''t have it, but it has to work because there''s even an STL sample program in the MSDN docs that shows how it works! I know VC++ isn''t one of the best standard-conforming compilers, but this should be in it.

My code indeed won''t throw an exception, because the amount of memory being allocated is small enough that under normal circumstances it won''t throw.

Erik
I tried including the "new" header, and removing the "memory" header, but it still doesn''t throw any exceptions. The /GX switch seems to be in a VC 6.0 project by default, so that isn''t causing the problem.

Check this out:

quote: Excerpt from the STL docs

new operator (STL Sample)

The sample code below illustrates how to use the new operator.

Required Header:
&ltnew>

Prototype:


void *operator new(size_t n)
void *operator new(size_t n, const nothrow&)
void *operator new[](size_t n);



That''s a special STL version of the new() operator. It''s not the default language version. New does not throw an exception unless you override it, like STL does. In fact, as I said before, I had to search for std::bad_alloc manually, and it was only found in 4 places from about 800 MB worth of docs. 2 of the 4 articles were from the KB, one from STL, and the other from a bug in VC 4.2. That''s pretty poor documentation in MSDN for such a commonly used operator as new(). The delete() operator, by contrast, turns up about 500 links. The typeid() operator turns up 25.

What''s more, the STL sample doesn''t compile without warnings about new(). I copied it right out of the docs, and new() doesn''t throw an exception. I even stepped through it in the debugger, and the new() used is the standard run-time operator new(). Not only that, but building the STL sample crashes VC after a few times. Are you absolutely sure that the C++ standard says that the default operator new() throws an exception? Or is it STL?


- null_pointer
Sabre Multimedia
I don''t have the C++ standard, but I''m pretty sure that new should throw a bad_alloc exception when allocation of the correct amount of memory fails. The C++ FAQ Lite (see http://www.cerfnet.com/~mpcline/c++-faq-lite/freestore-mgmt.html#[16.5]) seems to suggest the same thing.

It''s probably the VC++ 6 compiler that''s the culprit here. With BCC5.5, I even don''t have to include any header file to let new throw an exception when allocating too much memory. If I don''t use try/catch around the new expression, the program doesn''t execute anything that comes after the new expression, but instead terminates with an ''abnormal program termination'', indicating that an uncaught exception happened.

It could be that the debug version of operator new that VC++ uses doesn''t conform. Is the behaviour the same when you do a release build? And what warnings are you getting when compiling the STL new sample? Do you have VC++ Service Pack 3 installed?

Erik
I''m with Erik on this. new is supposed to throw an exception on failure, not return NULL. Understandably, many compilers don''t comply with this as they don''t want to break old code from before the standard behaviour was agreed upon.
Advertisement
Here''s the warning:

C:\Program Files\Microsoft Visual Studio\MyProjects\Test6\Test6.cpp(106) : warning C4291: ''void *__cdecl operator new(unsigned int,const struct std::nothrow_t &)'' : no matching operator delete found; memory will not be freed if initialization throws
an exception
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\new(36) : see declaration of ''new''


I don''t know what to make of it, except a linker error. I have VC 6.0 SP2, but not SP3. The Release version does not change anything. The STL sample was copied right out of the documentation. I would guess VC is the problem but let me check MSDN...it appears this came about in VC 4.2. That''s quite a long ways, and I''m surprised it hasn''t been fixed since then. I''ll look for it in VC 7.0, when it comes out.

Anyway, thanks for the info -- I''ve never had new() throw an exception but everyone talks as if it does, so I was wondering. BTW, it''s in the KB -- try looking up article #Q167733.


- null_pointer
Sabre Multimedia
Seems VC6 is indeed the culprit here, since that knowledge base article you mention says as much

The warning you get is in no way a linker error. It simply says that there''s no matching placement delete for a placement new. So, storage will not be deleted when an exception occurs during initialisation.

And this is also the problem with C++: although it should be portable, there are almost no compilers that implement the C++ standard correctly, which makes it very difficult to write source code that compiles on every C++ compiler. Luckily, the situation can only get better, now that the C++ standard has been approved.

Erik

This topic is closed to new replies.

Advertisement