Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 668943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:05:48+00:00 2026-05-14T00:05:48+00:00

As I’m coding for both Windows and Linux, I have a whole host of

  • 0

As I’m coding for both Windows and Linux, I have a whole host of problems.
Microsoft Visual C++ has no stdint header, but for that I wrote my own.

Now I found out that MS C++’s new operator does not throw an exception, so I want to fix this a quickly as possible. I know I can define a Macro with Parameters in Parenthesis, can I define a macro that replaces

MyClass x = new MyClass();

with

#ifdef MSC_VER
if(!(MyClass x = new MyClass())
{
    throw new std::bad_alloc();
}
#else
MyClass x = new MyClass();
#endif

(or something equivalent), AND works in BOTH MS C++ and G++ ?

Or alternatively if that is not possible, a batch file to run over the Code to do this?
I have become rather dependent on this exception being thrown.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:05:48+00:00Added an answer on May 14, 2026 at 12:05 am

    The simplest alternative is to write your own implementation of operator new, overriding the compiler’s default implementation (if your version of MSVC supports it). This is what Roger also suggested in his linked answer.

    The following program illustrates the attempt. operator new allocates memory from the heap using malloc and free. If you’re doing conditional compilation for G++ and MSVC, you can also use HeapAlloc and HeapFree instead of malloc and free for a more WinAPI-oriented approach.

    #include <iostream>
    #include <cstdlib>
    
    void *operator new(unsigned int size) throw (std::bad_alloc) {
        void *ptr = malloc(size);
    
        if (!ptr)
            throw std::bad_alloc();
        return ptr;
    }
    
    void operator delete(void *ptr) throw () {
        free(ptr);
    }
    
    // Sample code to test the implementation.
    int main(int argc, char **argv) {
        int *values[10];
    
        for (int i = 0; i < 10; ++i)
            values[i] = new int(i);
        for (int i = 0; i < 10; ++i)
            std::cout << *values[i] << std::endl;
        for (int i = 0; i < 10; ++i)
            delete values[i];
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 473k
  • Answers 473k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, just use the same syntax when exporting them -… May 16, 2026 at 4:02 am
  • Editorial Team
    Editorial Team added an answer You can use Orbited (which is a comet server based… May 16, 2026 at 4:02 am
  • Editorial Team
    Editorial Team added an answer The type you return from the controller should match the… May 16, 2026 at 4:02 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.