Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: C++ program crashes

  1. #1
    Join Date
    Jun 2012
    Beans
    10

    C++ program crashes

    hi,
    I am just learning about arrays. I know this isnt real world usage, but how come when i create a variable with 30 million arrays the program crashes? Shouldnt the kernel first go to the swap file before crashing? My laptop has 6GB ram and 12GB swap. Or is this just a c++ limitation?

  2. #2
    Join Date
    Feb 2009
    Beans
    1,469

    Re: C++ program crashes

    That is pretty aggressive in terms of memory usage, and there are usually plenty of ways to avoid using something that large. There are probably options you can pass to the compiler to allow your program to handle such large objects, but that's almost certainly solving the wrong problem.

    It's a bit like the fellow who complained, "Hey! I hate these Microsoft guys! What a rotten compiler! It only accepts 16,384 local variables in a function!" Limits like that, and like the one you're running into (whatever exactly it may be), aren't often encountered in the wild because smart programmers don't often write programs that exceed them.

    tl;dr -- "Well, don't do that then."

  3. #3
    Join Date
    Mar 2010
    Location
    South Africa
    Beans
    347
    Distro
    Ubuntu

    Re: C++ program crashes

    I do not mean this in offensive way , but i read your question over three times to make sure i wasnt reading it wrong, 30 million is huge , are you talking about using 30 million arrays or putting 30 million elements in an array?
    Registered Linux user: #543082
    Registered Ubuntu user: #34219
    http://dvisagie.com/


  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: C++ program crashes

    Quote Originally Posted by Lymphocyte View Post
    hi,
    I am just learning about arrays. I know this isnt real world usage, but how come when i create a variable with 30 million arrays the program crashes? Shouldnt the kernel first go to the swap file before crashing? My laptop has 6GB ram and 12GB swap. Or is this just a c++ limitation?
    The program crashed, not the kernel... Care to show us the source?

    Btw, the day your computer starts using all that swap, you better find a good book because it's going to crawl.

  5. #5
    Join Date
    Jul 2012
    Location
    Vancouver, Canada
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C++ program crashes

    Quote Originally Posted by Lymphocyte View Post
    hi,
    I am just learning about arrays. I know this isnt real world usage, but how come when i create a variable with 30 million arrays the program crashes? Shouldnt the kernel first go to the swap file before crashing? My laptop has 6GB ram and 12GB swap. Or is this just a c++ limitation?
    chances are that your problem is not with Linux and its swap file but with your source code and the way you define and use your array variables. In C++ you have less protection than in C# and it could be that you are overriding a boundary which causes the program to crash.

    If you can post the source code (at least all the areas where you define the array and set values to the array) it will be very helpful to help you identify the problem.

    I also concur with the previous replies - there are probably much better ways to accomplish the task without using a 30 million item array. although doable, it is extremely inefficient.

  6. #6
    Join Date
    Nov 2009
    Location
    The Netherlands
    Beans
    239
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C++ program crashes

    How are you creating that variable?
    If you are allocating it on the stack, your program is going to fail. IIRC swap memory is never used for extra stack space.
    If you did this, you could try allocating it on the heap.

  7. #7
    Join Date
    Jun 2012
    Beans
    10

    Re: C++ program crashes

    ok, i am creating it like this. Segmentation fault (core dumped). Now i thought the kernel would go to the swap file instead of seqmentation fualt.


    //test
    #include <iostream>
    using namespace std;

    int main()
    {
    cout << "I will now create an array of 3 million elements" << endl;

    int crash[3000000];
    return 0;
    }

  8. #8
    Join Date
    Sep 2009
    Beans
    217

    Re: C++ program crashes

    Quote Originally Posted by Lymphocyte View Post
    ok, i am creating it like this. Segmentation fault (core dumped). Now i thought the kernel would go to the swap file instead of seqmentation fualt.
    Your array is being created on the stack. There simply isn't that much space... so it crashes. The stack is where your program actually lives, so using it up means your program can't do anything (and trying to fails as you noticed.)

  9. #9
    Join Date
    Jun 2012
    Beans
    10

    Re: C++ program crashes

    so if i allocate it on the heap it wont crash then right?

  10. #10
    Join Date
    Sep 2009
    Beans
    217

    Re: C++ program crashes

    Maybe. You can overflow the heap too... In all probability, you'd probably not run into troubles. There are limits to the size of the heap, but you're not going to run into them anytime soon.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

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