Thread: Max array size question

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    Question Max array size question

    Hello,

    I need to construct an array containing 2^27=134217728 doubles, which should be about 1g (8*(2^27)=1.073.741.824 bytes), which should be possible on a Win32 machine with 2g of RAM. However my program crashed when the array is initialized, e.g.:

    Code:
    unsigned int n = 27;
    unsigned int nc = 1 << n;
    
    static double p[ nc ] = {0.0};
    Does it have anything to do with the stack size?

    Can you help?
    SK

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yes, you will need to dynamically allocate that array. The stack is not large enough to hold that much data.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by rags_to_riches View Post
    Yes, you will need to dynamically allocate that array. The stack is not large enough to hold that much data.
    Ok, can you show how? I am new to cpp and (real) programming in general.

    Thanks a lot!
    SK

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A simple solution is to use a std::vector<double>, e.g.,
    Code:
    std::vector<double> numbers(1 << 27);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    To dynamically allocate your variables, you have to declare them as pointers and put them on the heap. e.g:

    Code:
    unsigned int *n = new int;
    ... but don't forget to free the memory that you allocated!

    EDIT:
    Quote Originally Posted by laserlight View Post
    A simple solution is to use a std::vector<double>, e.g.,
    Code:
    std::vector<double> numbers(1 << 27);
    Laserlight, the OP said that he was new to cpp and programming in general, vectors are advanced stuff yes?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Vectors are simpler than manual memory management.
    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).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by legit
    Laserlight, the OP said that he was new to cpp and programming in general, vectors are advanced stuff yes?
    No, vectors are newbie stuff. Managing memory manually is advanced stuff.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by anon View Post
    Vectors are simpler than manual memory management.
    Quote Originally Posted by laserlight View Post
    No, vectors are newbie stuff. Managing memory manually is advanced stuff.
    Really? All of the tutorials and books that i've read say that vectors are advanced and powerful tools :S

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    115
    Thanks for your help! So I do:

    Code:
    unsigned int i;
    
    double *p = NULL;
    p = new double[ nc ];
    
    for ( i = nc; i--; ) { p[ i ] = 0.0; }
    
    // WORK WITH THE ARRAY
    
    delete [] p;
    p = NULL;
    Do I have to initialize the array values to work with it?

    SK

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by legit View Post
    Really? All of the tutorials and books that i've read say that vectors are advanced and powerful tools :S
    In the opinion of me, several other old members of this board, and several significant members of the C++ community (including Bjarne Stroustrup), those tutorials and books are wrong. Vectors are far easier to understand and deal with than manual memory management.
    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

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by legit
    Really? All of the tutorials and books that i've read say that vectors are advanced and powerful tools
    That makes them advanced to implement, not advanced to use. Consequently, it stands to reason that doing without vectors requires something advanced.

    Quote Originally Posted by serge
    Do I have to initialize the array values to work with it?
    If you want to zero initialise the elements of the dynamic array, write:
    Code:
    double* p = new double[nc]();
    But here's the catch: you have to be absolutely certain that "WORK WITH THE ARRAY" does not cause an exception to be thrown, otherwise you will have a memory leak. If you use a std::vector<double>, you need have no such worry.

    EDIT:
    Gah, I keep on forgetting if that syntax actually works for zero initialisation... and empirical evidence keeps on telling it does not. For a C++ish solution, std::fill or std::fill_n would be the next best alternative, but there is also memset() in this case.
    Last edited by laserlight; 06-15-2009 at 07:53 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Gah, I keep on forgetting if that syntax actually works for zero initialisation...
    It should. I'm not sure how reliably compilers implement it, though.
    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

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't see why either method would be considered advanced?
    When you use new, just remember to delete.
    When you use vector, lookup the various member functions available to you.
    Both pretty simple.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I don't see why either method would be considered advanced. When you use new, just remember to delete.
    When functions throw exceptions or return in random places, it is easy to forget to free the memory in every case. Plus I can't count the number of times I've seen this bug:
    Code:
    int* some_array = new int[x];
    // ...
    delete some_array;

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I don't see why either method would be considered advanced? When you use new, just remember to delete.

    Consider this bit of code:

    Code:
    class foo 
    {
    	public:
    	
    	foo( int n )
    	{
    		d1 = new int[ n ];
    		d2 = new int[ n ];
    	}
    	
    	virtual ~foo( void )
    	{		
    		delete [ ] d1;
    		delete [ ] d2;
    	}
    
    	int
    		* d1, 
    		* d2;		
    };
    That may look OK to an inexperienced eye, but it obviously doesn't take into account the effects of the default copy constructor on these objects nor the implications of an exception being thrown in the constructor.

    RAII is the way to go, methinks.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  2. Replies: 2
    Last Post: 11-24-2005, 01:30 AM
  3. Replies: 42
    Last Post: 12-19-2004, 08:59 AM
  4. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM