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. catch: out of memory in c++, process killed in lunux, any solution?

catch: out of memory in c++, process killed in lunux, any solution?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++linuxdata-structuresperformance
7 Posts 2 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.
  • G Offline
    G Offline
    Gofur Halmurat
    wrote on last edited by
    #1

    Hello guru developers, I have a project which i have to use a lot of memory allocation, the project works fine except when there are a lot of data the process is killed by Linux, i don't know why? For example this example: it should exit when there is no free memory, but it is killed after some time when i run this program, Does anyone has any idea how to solve this problem in Linux? thanks in advance

    #include
    #include
    #include
    #include
    #include
    #include

    using namespace std;

    struct tree_node
    {
    list<tree_node*> leaf;
    char data;
    short IsEndPoint;
    };

    int main()
    {

    try
    {
    	
    	for (int i = 0 ; i < 9999000; i++ )
    	{
    		tree\_node\* t = new tree\_node\[1000\];
    	        printf("id: %d \\r ", i);	
    	}	
    

    }
    catch(...)
    {
    cout<<"test"<< endl;
    exit(0);
    }

    cin.get();
    return 0;
    }

    It is never late to learn

    S 1 Reply Last reply
    0
    • G Gofur Halmurat

      Hello guru developers, I have a project which i have to use a lot of memory allocation, the project works fine except when there are a lot of data the process is killed by Linux, i don't know why? For example this example: it should exit when there is no free memory, but it is killed after some time when i run this program, Does anyone has any idea how to solve this problem in Linux? thanks in advance

      #include
      #include
      #include
      #include
      #include
      #include

      using namespace std;

      struct tree_node
      {
      list<tree_node*> leaf;
      char data;
      short IsEndPoint;
      };

      int main()
      {

      try
      {
      	
      	for (int i = 0 ; i < 9999000; i++ )
      	{
      		tree\_node\* t = new tree\_node\[1000\];
      	        printf("id: %d \\r ", i);	
      	}	
      

      }
      catch(...)
      {
      cout<<"test"<< endl;
      exit(0);
      }

      cin.get();
      return 0;
      }

      It is never late to learn

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      I never programmed with gcc in linux so I might be wrong. I am guess that new operator does not throw an exception but terminated the program when cannot allocate memory. -Saurabh

      G 1 Reply Last reply
      0
      • S Saurabh Garg

        I never programmed with gcc in linux so I might be wrong. I am guess that new operator does not throw an exception but terminated the program when cannot allocate memory. -Saurabh

        G Offline
        G Offline
        Gofur Halmurat
        wrote on last edited by
        #3

        how could you catch exception memory is full? is there any way in c++ for windows? not for linux, it must be same in linux

        It is never late to learn

        S 1 Reply Last reply
        0
        • G Gofur Halmurat

          how could you catch exception memory is full? is there any way in c++ for windows? not for linux, it must be same in linux

          It is never late to learn

          S Offline
          S Offline
          Saurabh Garg
          wrote on last edited by
          #4

          Well you already have the code to catch the exception...using a catch block. When new operator cannot allocate memory then it throws std::bad_alloc exception. Also the value of returned pointer is NULL. -Saurabh

          G 1 Reply Last reply
          0
          • S Saurabh Garg

            Well you already have the code to catch the exception...using a catch block. When new operator cannot allocate memory then it throws std::bad_alloc exception. Also the value of returned pointer is NULL. -Saurabh

            G Offline
            G Offline
            Gofur Halmurat
            wrote on last edited by
            #5

            Thanks for reply, I run this code under Visual c++ 2005, it does not throw any exception, it crashes computer, do u know why it crashes computer? Thanks

            It is never late to learn

            S 1 Reply Last reply
            0
            • G Gofur Halmurat

              Thanks for reply, I run this code under Visual c++ 2005, it does not throw any exception, it crashes computer, do u know why it crashes computer? Thanks

              It is never late to learn

              S Offline
              S Offline
              Saurabh Garg
              wrote on last edited by
              #6

              If my calculation is correct then you are requesting about 260.74GB of memory!!! Which of course cannot be allocated. I don't think that your program should crash, instead you should get nice error from windows stating Windows is running low on memory so termination your program. Just out of curiosity what are you trying to do? -Saurabh

              G 1 Reply Last reply
              0
              • S Saurabh Garg

                If my calculation is correct then you are requesting about 260.74GB of memory!!! Which of course cannot be allocated. I don't think that your program should crash, instead you should get nice error from windows stating Windows is running low on memory so termination your program. Just out of curiosity what are you trying to do? -Saurabh

                G Offline
                G Offline
                Gofur Halmurat
                wrote on last edited by
                #7

                I took such a large memory on purpose to test bad allocation exception, i figure out how to do that,here is my final code, this works,

                # include # include # include #include struct tree_node
                {
                std::list<tree_node*> leaf;
                char data;
                short IsEndPoint;
                };

                void MyHandler()
                {
                std::cout << "MyHandler Called... " << std::endl;
                throw std::bad_alloc();
                }

                int main()
                {
                std::set_new_handler( MyHandler );

                char\* ch;
                try
                {
                	for( unsigned long Idx( 0 ); Idx < 0xffffffff; ++Idx )
                	{
                		new tree\_node\[10000\];
                		printf("id: %d \\r",Idx);
                	}
                }
                catch( const std::bad\_alloc& e )
                {
                	std::cout << e.what() << std::endl;
                }
                
                std::cin.get();
                return 0;
                

                }

                It is never late to learn

                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