exception handling ( memory allocation)

This is a discussion on exception handling ( memory allocation) within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <new> using std::cout; using std::endl; using std::bad_alloc; int main() { char* pdata = 0; size_t count ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    exception handling ( memory allocation)

    Code:
    #include <iostream>
    #include <new>
    
    using std::cout;
    using std::endl;
    using std::bad_alloc;
    
    int main()
    {
    	char* pdata = 0;
    	size_t count = ~static_cast<size_t>(0) / 3; // if you divide it by two it'll work
    	cout << endl
    		 << count
    		 << endl << count / 3 << endl;
    	try
    	{
    		pdata = new char[count];
    		cout << "Memory allocated." << endl;
    	}
    	catch(bad_alloc &ex)
    	{
    		cout << "Memory allocation failed." << endl
    			<< "The information from the exception object is: "
    			<< ex.what() << endl;
    	}
    
    	delete[] pdata;
    	return 0;
    }
    Look at the bold part of my code,

    When I divide it by 3, the program doesn't responds, but when divide it by 2 the program works and throws an exception but not in the case of 3.

    Why?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you really want to figure out how much memory you can allocate, I would suggest some method like this:

    Code:
    #include <iostream>
    #include <new>
    
    using std::cout;
    using std::endl;
    using std::bad_alloc;
    
    
    size_t absdiff(size_t a, size_t b)
    {
        
        if (a > b)
    	return a-b;
        return b-a;
    }
    
    int main()
    {
        char *pdata;
    
        size_t max = ~0;
        size_t min = 1024;
        size_t size;
        while(absdiff(min,max) > 1)
        {
    	size = (min + max) / 2;
     	try
    	{
    	    cout << "trying size " << size << " min=" << min << " Max=" 
    		 << max << endl;
    	    pdata = new char[size];
    	    cout << "Memory allocated." << endl;
    	    min = size;
    	    delete pdata;
    	}
    	catch(bad_alloc &ex)
    	{
    	    cout << "Memory allocation failed." << endl
    		 << "The information from the exception object is: "
    		 << ex.what() << endl;
    	    max = size;
    	}
        }
        cout << "Max size allocation = " << size << endl;
        return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Well what I wanna know is that why the code provided by me doesn't works,

    It throws an exception when you divide it by 2
    but the programs stops when you try to divide count by 3


    since the number divided by 2 is larger than if that number is divided by 3

    so if it can throw an excption to a larger number then why a not smaller number ?


    I want to know whats causing that program not to respond.
    Last edited by manzoor; 09-15-2008 at 06:38 AM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    O_o

    When you see local 318 attempting to control a burning house do you blame the firemen for the fire?

    Soma

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Works for me in either case (doesn't throw).

    Perhaps your memory is rather fragmented and the OS has to do a lot of work to provide a contiguous block, so it just appears to be not responding?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manzoor View Post
    Well what I wanna know is that why the code provided by me doesn't works,

    It throws an exception when you divide it by 2
    but the programs stops when you try to divide count by 3


    since the number divided by 2 is larger than if that number is divided by 3

    so if it can throw an excption to a larger number then why a not smaller number ?


    I want to know whats causing that program not to respond.
    I have no idea. Running the code you posted unmodified and compiled with gcc, it works fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM
  4. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  5. Memory Allocation :: C/C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2002, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21