Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. memory allocation failure

memory allocation failure

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancequestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    If a memory allocation with new fails in C++, should it return NULL or throw an exception? I have this code:

    #include
    #include
    #include
    using namespace std;

    int main() {
    try{
    char *p = new char[44'321'833'923];

     if (!p) {
        cout << "memory allocation failed";
        exit(1);
        
    }
    
    }
    catch(exception&e)
    {
        cout << "caught it." << e.what();
        exit(1);
    }
    catch (...)
    {
        cout << "caught exception;";
        exit(1);
    }
    
    
    return 0;
    

    }

    The class I'm taking says it returns NULL, but the code seems to throw an exception. Anyone know? Thanks.

    M C 2 Replies Last reply
    0
    • M mike7411

      If a memory allocation with new fails in C++, should it return NULL or throw an exception? I have this code:

      #include
      #include
      #include
      using namespace std;

      int main() {
      try{
      char *p = new char[44'321'833'923];

       if (!p) {
          cout << "memory allocation failed";
          exit(1);
          
      }
      
      }
      catch(exception&e)
      {
          cout << "caught it." << e.what();
          exit(1);
      }
      catch (...)
      {
          cout << "caught exception;";
          exit(1);
      }
      
      
      return 0;
      

      }

      The class I'm taking says it returns NULL, but the code seems to throw an exception. Anyone know? Thanks.

      M Offline
      M Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      I don’t know who teaches the class you are taking but I hope you don’t pay much for it. new has been throwing std::bad_alloc for the past 20+ years. Also, while I don’t mind at all answering your questions, why don’t you try to googling? A search for “c++ new allocation failure” gives you many results and undoubtedly can teach you more than my limited answers. Edit: Using exactly the search I suggested, I found this good article that discusses the issue: C++ Exceptions and Memory Allocation Failure[^].

      Mircea

      M 1 Reply Last reply
      0
      • M mike7411

        If a memory allocation with new fails in C++, should it return NULL or throw an exception? I have this code:

        #include
        #include
        #include
        using namespace std;

        int main() {
        try{
        char *p = new char[44'321'833'923];

         if (!p) {
            cout << "memory allocation failed";
            exit(1);
            
        }
        
        }
        catch(exception&e)
        {
            cout << "caught it." << e.what();
            exit(1);
        }
        catch (...)
        {
            cout << "caught exception;";
            exit(1);
        }
        
        
        return 0;
        

        }

        The class I'm taking says it returns NULL, but the code seems to throw an exception. Anyone know? Thanks.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        The call used in your code would throw. See the documentation: operator new, operator new[] - cppreference.com[^].

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        1 Reply Last reply
        0
        • M Mircea Neacsu

          I don’t know who teaches the class you are taking but I hope you don’t pay much for it. new has been throwing std::bad_alloc for the past 20+ years. Also, while I don’t mind at all answering your questions, why don’t you try to googling? A search for “c++ new allocation failure” gives you many results and undoubtedly can teach you more than my limited answers. Edit: Using exactly the search I suggested, I found this good article that discusses the issue: C++ Exceptions and Memory Allocation Failure[^].

          Mircea

          M Offline
          M Offline
          mike7411
          wrote on last edited by
          #4

          The class is right here: https://www.edx.org/learn/computer-programming/iitbombay-programming-basics Here is some code from the class:

          int main()
          {
          int numStudents;
          int * A;
          cin >> numStudents;
          A = new int[numStudents];
          if (A != NULL) { A[0] = 10; A[1] = 15;}
          return 0;
          }

          L M 2 Replies Last reply
          0
          • M mike7411

            The class is right here: https://www.edx.org/learn/computer-programming/iitbombay-programming-basics Here is some code from the class:

            int main()
            {
            int numStudents;
            int * A;
            cin >> numStudents;
            A = new int[numStudents];
            if (A != NULL) { A[0] = 10; A[1] = 15;}
            return 0;
            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            As this is supposed to be a C++ class, the code should be using nullptr rather than the old Windows NULL definition. And what happens if numStudents is only 1?

            1 Reply Last reply
            0
            • M mike7411

              The class is right here: https://www.edx.org/learn/computer-programming/iitbombay-programming-basics Here is some code from the class:

              int main()
              {
              int numStudents;
              int * A;
              cin >> numStudents;
              A = new int[numStudents];
              if (A != NULL) { A[0] = 10; A[1] = 15;}
              return 0;
              }

              M Offline
              M Offline
              Mircea Neacsu
              wrote on last edited by
              #6

              Colour me not impressed! As long as you take the free option, I guess you didn't loose much, except maybe some time. Also there is the question of getting bad programming habits, but those can be fixed over time. These days there are many free online course from very reputable sources like MIT OpenCourseWare | Free Online Course Materials[^]. You might want to take a look at some of them.

              Mircea

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups