CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: new and nothrow

  1. #1
    Join Date
    Jul 2002
    Location
    Connecticut, U.S.
    Posts
    275

    new and nothrow

    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:
    Code:
    // 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

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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?
    Code:
    //--------------------------------------------------------------------------------
    // 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
    Last edited by PaulWendt; December 13th, 2002 at 02:09 PM.

  3. #3
    Join Date
    Jul 2002
    Location
    Connecticut, U.S.
    Posts
    275
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured