Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, under windows you can link with nothrownew.obj to have new return null instead of throw an execption. Works for the lib as well since linking with a version of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    11

    Does linux have a nothrownew.obj equivalent?


    Hello,

    under windows you can link with nothrownew.obj to have new return null instead of throw an execption. Works for the lib as well since linking with a version of new that doesn't throw. Does GCC or Linux have something like nothrownew.o to link with?

    I'm converting thousands of core library routines and apps from Windows/DOS to Linux, those turn off exceptions (I did find -fno-execptions for GCC), but also new needs to return NULL (all the code expects failed allocations to return NULL and needed in some cases where failed allocations are expected). I really don't want to try and find all new's and add nothrow to them as I could miss a lot and not sure how older compilers under dos will handle it (I could "#define nothrow" maybe?), but the easy way is the MS solution of providing a object version of new that doesn't throw. I really hope it exists for Linux/GCC?

    TIA!!

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    If you want C++ new to return a null pointer, use the nothrow argument. Here is a link with more details: nothrow - C++ Reference
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Sep 2011
    Posts
    11
    I'm aware of the nothrow form of new, but is there a better solution such as linking with an .o file that overrides new to not throw? Even MS provides that.

    Maybe I'd have to define a new new operator that overrides it and provide that in an .o file? I've had to do that doing driver development using C++ on Windows.

    C++ should have defaulted nothrow and required a "throw" for those wanting exception handling, I used a C++ compiler before the standard was completed and nothrow didn't even exist.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    When I confronted a similar problem (porting old C++ code to standard C++) I found that the easiest thing was to create operator new methods for my most frequently used classes, and utilized the nothrow operator under the covers, which eliminated a lot of ancillary code changes. In any case, this article may help: Windows to UNIX porting, Part 2: Internals of porting C/C++ sources
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Sep 2011
    Posts
    11
    thanks I ended up overloading the global versions of new. I'd copy/paste it here but because Linux is runing in virtual box, when I ctrl-c, it seems to copy, but ctrl-v doesn't work. I basically did #include <new> then for each version of new/delete overloaded it to use malloc and free.

Posting Permissions

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