Click to See Complete Forum and Search --> : new and nothrow


jflegert
December 13th, 2002, 12:04 PM
I was trying to use new and nothrow (using VC7) and could not get it to compile. I tried using the following example from MSDN:
// new_and_delete.cpp
// compile with: /EHsc
#include <stdio.h>
#include <new.h>
#include <limits.h>
int main()
{
int * i_arr;
try {
i_arr = new int[UINT_MAX]; // vector new
}
catch(...) {
printf("caught exception\n");
}

int * k_arr;
k_arr = new (std::nothrow) int[UINT_MAX]; // allocation will fail

delete[] i_arr; // vector delete
delete[] k_arr;
}

and I get the following compilation errors:

c:\AIT\vc\test\test\testDlg.cpp(242) : warning C4307: '*' : integral constant overflow
c:\AIT\vc\test\test\testDlg.cpp(249) : error C2061: syntax error : identifier 'nothrow'.

Any suggestions?

I don't really have a specific reason for using nothrow, I just came across it while reading.

Thanks,
John Flegert

PaulWendt
December 13th, 2002, 01:03 PM
I have no idea; I don't get any errors with your code [though I do
get the integral constant overflow warnings]. I also have Visual
Studio .NET. I #include'd the old files [with the .h extensions] as
well as the new header files [sans extensions] and got no errors
with either method. What does the following do for you?

//--------------------------------------------------------------------------------
// new_and_delete.cpp
// compile with: /EHsc
#include <iostream>
#include <new>
#include <cstdio>
using namespace std;
//#include <new.h>
//#include <stdio.h>
int main()
{
int* pInt = new (std::nothrow) int(4);
delete pInt;

try
{
while (1)
{
int* pArrayInt = new (std::nothrow) int [1024*1024];
}
}
catch (...)
{
fprintf(stderr, "We caught it.\n");
}
}


Is it possible that you have a different version of the C++
standard from dinkumware or do you have any other headers
installed? I'm assuming not, but I don't know what the problem
could be [unless it's a compiler option I'm using ... hmmmm].

Edit: I started a totally new, fresh project and I got the compiler
to barf on nothrow whenever I don't put std:: before it, no matter
what header files I used. I can only think that your problem is a
compiler switch or header file problem or something....

--Paul

jflegert
December 13th, 2002, 01:17 PM
Paul,

I'm very sorry. I should have double checked the code before I posted. The error I was getting was on some code I modified (I didn't include the std on std::nothrow.

Thank you for the help,
John