Thread: memory allocation exception

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    memory allocation exception

    Hi, I'm writing program that performs some calculations. User needs to enter a number of coeficients, and then program will calculate coeficients according some algorithm.
    Basically I have this problem:
    Code:
    #include<new>
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int* arr;
    	
    	try
    	{
    		arr=new int[0x7fffffff];
    	}
    	catch(bad_alloc &ba)
    	{
    		cout<<ba.what();
    	}
    	catch(...)
    	{
    		cout<<"Something went wrong!";
    	}
    }
    User can enter enormous number of wanted coefficients and allocation may fail.When I compile this code I get warning:
    warning C4307: '*' : integral constant overflow.
    And this simply doesn't work. I get exception in dbgheap.c
    Actually my program is much more complicated (it is dialog based MFC application) but I decided to go step by step.

    By the way I found this code in MSDN and it works:
    Code:
    #include<new>
    #include<iostream>
    using namespace std;
    
    class MyClass 
    {
    public:
       MyClass( )
       {
       };
    
       ~MyClass( )
       {
       };
       int imember[30];
    };
    
    int main( ) 
    {
       MyClass* fPtr;
       try
       {
          fPtr = new MyClass[0x7fffffff];
          delete[ ] fPtr;
       }
       catch( bad_alloc &ba)
       {
          cout << ba.what( ) << endl;
       };
    }
    If someone can explain me why?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    May be this will work correctly.

    May be this will work correctly.
    Regards.


    Code:
    #include<new>
    #include<iostream>
    
    using namespace std;
    
    class MyClass 
    	{
    	public:
    		MyClass( )
    			{
    			};
    
    		~MyClass( )
    			{
    			};
    		int imember[30];
    	};
    
    int main( ) 
    	{
    	MyClass* fPtr;
    	try
    		{
    		fPtr = new MyClass;//BIG MEMORY ALLOCATED HERE
    		delete fPtr; // BIG TROUBLE HERE, OMITED []
    		}
    	catch( bad_alloc &ba)
    		{
    		cout << ba.what( ) << endl;
    		};
    	}

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I highly doubt you would need that many ints in your array. Is the user really going to input 2 billion numbers? Try picking a smaller number for your array size, or even better, use a vector or other container that grows automatically.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Well I have too much code to change it all so the only option is how to prevent my program crashes under these conditions!

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Catching a bad_alloc exception is the correct way to handle an out of memory error, although on some compilers (e.g. MSVC++ 6.0) new returns null, so you would want to check that if you are using that compiler. If you are just trying to make sure that you are handling your error correctly, then use a smaller number in your test app. I just did this the other day, but unfortunately I don't have the code in front of me so I don't remember how I was able to get around that warning.

    If your program is out of memory, you are going to have a lot more problems than just catching the right exception. Hopefully you can recover gracefully if you ever run into that problem.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You are attempting to allocate ((2^31)-1)*4 bytes, that's about 8 Gigabytes of RAM. This is not even technically possible on a 32-bit CPU.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Like I said, I found that code in MSDN for MyClass and I'm interested why it's not working with array of int numbers.
    Why I'm getting exception at some assert in dbgheap.c
    Anyway my application is Dialog based MFC so I'm not sure how to perform appropriate cleanup and exit application when probelm with memory allocation happens

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    It seems I'll need to leave it up to user and hope memory allocation will never fail. I'm really interested to see how these issues are handled in real world programming

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Fat too often it's not handled at all, I think. The only place where it's handled is where an out-of-memory is likely, that is, where a lot of memory is allocated.
    Exceptions make proper handling of out-of-memory a lot easier.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. exception handling ( memory allocation)
    By manzoor in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2008, 06:43 AM
  3. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  5. memory allocation ptr to array? how?
    By kokopo2 in forum C Programming
    Replies: 8
    Last Post: 09-01-2005, 05:06 PM