CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Posts
    9

    C++ memory issues

    I am trying to write a function which attempts to allocate memory 3d vectors and returns false if it is unable to allocate the memory. It is not working. I have pasted the code below.



    Code:
    # include <iostream>
    # include <fstream>
    # include <string>
    # include <cstring>
    # include <sstream>
    # include <time.h>
    # include <iomanip>
    # include <vector>
    
    using namespace std;
    
            template <class T>
    bool SafeAlloc(vector<T> &v, int Size)
    {
            bool MemoryAllocated=true;
            try
            {
                    v.resize(Size, T(0));
            }
            catch(bad_alloc&)
            {
                    MemoryAllocated=false;
            }
            return MemoryAllocated;
    }
    
            template <class T>
    bool Create3DArray(vector< vector< vector<T> > > &array, int size1, int size2, int size3)
    {
            bool MemoryAllocated=true;
            int i;
            vector<T> v;
            vector< vector<T> > matrix;
    
            array.clear();
            try
            {
                    v.resize(size3, T(0));
                    matrix.resize(size2, v);
                    array.resize(size1, matrix);
            }
            catch (bad_alloc&)
            {
                    MemoryAllocated=false;
            }
            return MemoryAllocated;
    }
    
    int main(int argc, char * argv[])
    {
    	int big, small, big1d;
            vector<double> big_array, small_array;
    	vector< vector< vector<double> > > big_array3d, small_array3d;
    
            big1d=100000000;
    	big=500;
    	small=10;
    
    	if (!SafeAlloc(small_array, small))
    	{
    		cout <<"Unable to allocate memory for small array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for small array"<<endl;
    	}
    
    	if (!SafeAlloc(big_array, big1d))
    	{
    		cout <<"Unable to allocate memory for big array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for big array"<<endl;
    	}
    	
            if (!Create3DArray(small_array3d, small, small, small))
    	{
    		cout <<"Unable to allocate memory for small 3d array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for small 3d array"<<endl;
    	}
    	
            if (!Create3DArray(big_array3d, big1d, big1d, big1d))
    	{
    		cout <<"Unable to allocate memory for big 3d array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for big 3d array"<<endl;
    	}
    	
            if (!Create3DArray(big_array3d, big, big, big))
    	{
    		cout <<"Unable to allocate memory for big 3d array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for big 3d array"<<endl;
    	}
    	
            return EXIT_SUCCESS;
    }
    The output I get is:

    Allocated memory for small array
    Unable to allocate memory for big array
    Allocated memory for small 3d array
    Unable to allocate memory for big 3d array
    Killed

    The program crashes when it tries to allocate a large 3d vector, but works when it tries to allocate memory for an extremely large 3d vector or a small 3d vector. My function for doing the same thing with 1d vectors always works. It seems that the bad alloc is only caught when it is the first memory allocation in the try block. I am very confused. Thanks in advance for any help.
    Last edited by BrainInVat; August 31st, 2010 at 01:04 PM.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: C++ memory issues

    Your posted program does not compile.

    I took the time to correct it enough to run. I saw nothing that could lead to the behavior you are speaking of, nor have I experienced it.

    You can post a minimal but complete example that duplicates this behavior, and we'll see what we can do from there.

    Note:
    Small arrays will immediatly allocate.
    Huge arrays will immediatly fail
    Medium Arrays (2 to 6 GB) will not fail, but make massive use of your swap. This will make it look like your program has "hung" or "crashed", but this is not the case. It takes about 1 minute on my computer to allocate an 1000xCube 3D vector.

    Is this what you are experiencing?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ memory issues

    Quote Originally Posted by BrainInVat View Post
    I am trying to write a function which attempts to allocate memory 3d vectors and returns false if it is unable to allocate the memory. It is not working. I have pasted the code below.
    You didn't paste one of the most important parts of your program -- what is "Real" in your program?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jan 2007
    Posts
    9

    Re: C++ memory issues

    My apologies for posting code that does not work. The code I posted was in two source files, and I thought it would be more convenient to post them as one file, but I copied and pasted the wrong version of SafeAlloc. Also I forgot to post the contents of TypeDef.h

    which is

    Code:
    #ifndef _TypeDef_included_
    #define _TypeDef_included_ 
    
    typedef double Real;
    
    #endif
    Here is a version of the code which compiles.

    Code:
    # include <iostream>
    # include <fstream>
    # include <string>
    # include <cstring>
    # include <sstream>
    # include <time.h>
    # include <iomanip>
    # include <vector>
    
    using namespace std;
    
            template <class T>
    bool SafeAlloc(vector<T> &v, int Size)
    {
            bool MemoryAllocated=true;
            try
            {
                    v.resize(Size, T(0));
            }
            catch(bad_alloc&)
            {
                    MemoryAllocated=false;
            }
            return MemoryAllocated;
    }
    
            template <class T>
    bool Create3DArray(vector< vector< vector<T> > > &array, int size1, int size2, int size3)
    {
            bool MemoryAllocated=true;
            int i;
            vector<T> v;
            vector< vector<T> > matrix;
    
            array.clear();
            try
            {
                    v.resize(size3, T(0));
                    matrix.resize(size2, v);
                    array.resize(size1, matrix);
            }
            catch (bad_alloc&)
            {
                    MemoryAllocated=false;
            }
            return MemoryAllocated;
    }
    
    int main(int argc, char * argv[])
    {
    	int big, small, big1d;
            vector<double> big_array, small_array;
    	vector< vector< vector<double> > > big_array3d, small_array3d;
    
            big1d=100000000;
    	big=500;
    	small=10;
    
    	if (!SafeAlloc(small_array, small))
    	{
    		cout <<"Unable to allocate memory for small array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for small array"<<endl;
    	}
    
    	if (!SafeAlloc(big_array, big1d))
    	{
    		cout <<"Unable to allocate memory for big array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for big array"<<endl;
    	}
    	
            if (!Create3DArray(small_array3d, small, small, small))
    	{
    		cout <<"Unable to allocate memory for small 3d array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for small 3d array"<<endl;
    	}
    	
            if (!Create3DArray(big_array3d, big1d, big1d, big1d))
    	{
    		cout <<"Unable to allocate memory for big 3d array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for big 3d array"<<endl;
    	}
    	
            if (!Create3DArray(big_array3d, big, big, big))
    	{
    		cout <<"Unable to allocate memory for big 3d array"<<endl;
    	}
    	else 
    	{
    		cout <<"Allocated memory for big 3d array"<<endl;
    	}
    	
            return EXIT_SUCCESS;
    }
    The program does indeed crash. The output is:

    Allocated memory for small array
    Unable to allocate memory for big array
    Allocated memory for small 3d array
    Unable to allocate memory for big 3d array
    Killed

    The output I expect is:

    Allocated memory for small array
    Unable to allocate memory for big array
    Allocated memory for small 3d array
    Unable to allocate memory for big 3d array
    Unable to allocate memory for big 3d array

    It does take it about a minute to crash after printing Unable to allocate memory for big 3d array. The behavior of this program is dependent on the computer memory. I am running this on a 32-bit machine with 1 GB of RAM. If this program does not reproduce the output I posted here maybe you can try adjusting the sizes of the vectors. Thanks again.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ memory issues

    Quote Originally Posted by BrainInVat View Post
    My apologies for posting code that does not work.
    Please mention the compiler and version of the compiler that you used.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 31st, 2010 at 04:06 PM.

  6. #6
    Join Date
    Jan 2007
    Posts
    9

    Re: C++ memory issues

    I use g++ version 4.1.

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: C++ memory issues

    Again, does your program create an actual illegal operation and crash? Or does it just quit responding, and the OS gives you the option to kill it?

    As long as your program does not actually provoke illegal operations, than it is still running, even if it is not responding.

    There is nothing wrong with your program (as far as can see), but there are some sizes of memory allocation which are under the threshold, but reside mostly in swap. On my machine, this allocation takes about 2 minutes. Are you sure this is not what you are experiencing?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured