Thread: Don't have enough memory

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Don't have enough memory

    Hi to everyone. I have a little program and my compiler desn't allocate enough memory for a matrix.

    Code:
    include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
        int i, j, a[1001][1001], x, y, t1, t2, s1, s2, s3, s4, r1, r2, r3, r4, k, n;
    
        fstream f("ai.in.TXT", ios :: in);
    
        f>>n>>t1>>t2>>s1>>s2>>s3>>s4>>r1>>r2>>r3>>r4>>k;
    
        cout<<n<<t1<<t2<<s1<<s2<<s3<<s4<<r1<<r2<<r3<<r4<<k;
    
    }
    I'm using Code :: Blocks. What can i do for gain more memory ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use a std::vector instead, either as a vector of vector, or by having a huge vector that you divide into chunks.
    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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    @laserlight how can i do that ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For the former, it means using:
    Code:
    std::vector<std::vector<int> > a(1001, std::vector<int>(1001));
    Remember to #include <vector>. You can then use the same array index syntax that you have been using. The idea is that the memory allocated will likely have a larger upper limit than if you used a local array.

    By the way, you should give your variables descriptive names.
    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
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    I will have to read more about your idea. Thank you for the advice, but is a way to set my compiler to allocate more memory for that matrix ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No one in their right mind dumps a 4MB array on the stack.

    Yes there is a way to increase the default stack size, but it is VERY dependent on your operating system. In some environments, you're simply not allowed to increase the default stack size, so picking a better method is just an all-round good idea.

    static int arr[1000][1000];
    would take it off the stack, but would still be a big waste of space.

    Ideally, you find out how big it really needs to be at run-time, and allocate accordingly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention there is a limit to how much the OS and compiler will allow you to put on the stack, and it's rather low. I think I can fairly well predict you can't get more than 10 MB--maximum. You will probably get much less than that usually. The default stack size in Windows is 1 MB.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  2. Replies: 12
    Last Post: 04-11-2010, 07:14 AM
  3. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  4. Finding memory address using memory pattern
    By MindWorX in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2008, 07:20 AM
  5. Replies: 2
    Last Post: 09-28-2006, 01:06 PM

Tags for this Thread