Thread: Out of memory with new??

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Out of memory with new??

    I have a function that uses calloc to allocate memory in my program. If it runs out of memory if gives an error message.
    The function is as follows:

    Code:
    void *ckalloc(size_t bytes)
    {
        register void *ret;
    
        if ((ret = calloc(bytes, sizeof(char))) == NULL)
        {
            fatal("Out of memory\n");
        }
        else
        {
            return ret;
        }
    
        return ret;
    }
    I am going to change the code to use new instead of calloc. The function calloc returns a null pointer if it cannot allocate memory. How do I know if "new" has run out of memory?

    Also is there a way in C++ using new to reallocate memory (similar to realloc)?

    Thanks
    Mark

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    new will set the pointer to NULL if it cannot allocate enough memory:

    Code:
    ...
    char* pMyP = new char[120];
    if (pMyP == NULL)
    {
    	std::cerr << "Memory Exhausted!" << std::endl;
    	// exit etc here
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Thanks

    Thanks for the reply.

  4. #4
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    I thought new threw an exception and that you have to use 'nothrow' or something like that to get the desired effect

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Standard C++ states that operator new should throw an exception of type std::bad_alloc when it fails. This is different from previous stages of C++ and it means two important things:

    1. If your code does not contain a catch statement to handle an std::bad_alloc exception, your program will crash whenever new fails, even if that exception occurs deep down in some library routine you never knew was there.

    2. Testing whether the pointer returned from new == 0 is completely useless; ... in cases of failure, the thrown exception will abort the current thread of execution right from where it was thrown, so the next lines of code will not be executed
    http://www.devx.com/tips/Tip/5807

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main() {
    	
    	try
    	{
    		double* p = new double[100];
    		//throw bad_alloc();
    	}
    	catch(bad_alloc& rEx)
    	{
    		cerr<<"Memory Exhausted\n";
    	}
    
    	return 0;
    }
    Or:

    Code:
    #include <iostream>
    #include <new>
    using namespace std;
    
    int main() {
    	
    	double* p = new(nothrow) double[100];
    	
    	if(!p)
    	{
    		cerr<<"Memory Exhausted!\n";
    	}
    
    	return 0;
    }
    http://www.informit.com/guides/conte...eqNum=170&rl=1
    Last edited by 7stud; 12-07-2005 at 07:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM