0

When I call the constructor for my SegTree struct in the code below, I keep getting a non-zero exit code. When I comment out the line that initializes the struct, the program runs with no issue. Can someone explain why this is happening and how to fix my code?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <vector>
#include <string.h>

using namespace std;

struct SegTree{
    int N;
    long long tree [1<<20], arr [1<<20];
    SegTree(int x){ N = x; }
};

int main(){
    SegTree st(len);
    return 0;
}

Please help, and thanks in advance!

EDIT: My issue is not the size of the arrays, as I have mentioned in the comments. I am able to make the arrays and run the code when they are placed outside the struct.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
vamaddur
  • 65
  • 1
  • 8
  • 3
    `stdio.h`, `stdlib.h` and `string.h` are C standard libraries, not C++ standard libraries. C++ standard library headers never have the suffix `.h`. [`using namespace std;` is a bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Also, modern C++ way of initializing class members would be [member list initialization](http://en.cppreference.com/w/cpp/language/initializer_list). – tambre Jun 28 '17 at 07:50
  • 1
    How much memory do you think `long long tree [1<<20], arr [1<<20];` will take? With automatic storage duration? How big is your call stack? – StoryTeller - Unslander Monica Jun 28 '17 at 07:50
  • 1
    `long long tree [1<<20], arr [1<<20];` becomes fairly big on the stack. Likely to overflow. I'd recommend to use `std::vector tree (1<<20), arr (1<<20);` instead. – πάντα ῥεῖ Jun 28 '17 at 07:51
  • I tried to initialize the array without the struct, and it works just fine. – vamaddur Jun 28 '17 at 07:52
  • @tambre I don't understand the need to comment on my style of coding instead of actually answering my question. – vamaddur Jun 28 '17 at 07:54
  • @YouKnowMe well, suggesting improvements to help you get better at it. That's why it's a comment and not an answer :) – Quentin Jun 28 '17 at 07:55
  • 1
    Possible duplicate of [Segmentation fault on large array sizes](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) – mch Jun 28 '17 at 07:56
  • 1
    @tambre: My copy of the C++ standard specifies `stdio.h` etc in exactly as much detail as `cstdio`. In what sense are they "not C++ standard libraries"? (Note that *in practise*, `cstdio` has always put the names in the global namespace as well `std::`, and this is now permitted by the standard.) – Martin Bonner supports Monica Jun 28 '17 at 07:58
  • I think you have not even tried to compile your code after modifying(shortening) it. It have compilation errors. Please correct and update it. – cse Jun 28 '17 at 08:23

1 Answers1

7

Wow. That's a big structure:

struct SegTree{
    int N;
    long long tree [1<<20], arr [1<<20];

1<<20 is 1 Meg. long long is typically 8 bytes, so your structure is 16 Mbytes ... and you are allocating it on the stack. Typically, programs allocate 1Mbyte for the stack ... so it won't fit!

The solution is to change the arrays into vectors. The array will then be allocated on the heap, and you should be fine:

    std::vector<long long> tree = {1<<20};
    std::vector<long long> arr  = {1<<20};

(once you are using vectors, you may well be able to do much better than allocating the memory all at once at some maximum size in the constructor).

  • As I said in one of the earlier comments, the size of the arrays is not the problem. I was able to successfully run the code without the struct. – vamaddur Jun 28 '17 at 07:58
  • 1
    @YouKnowMe : Which compiler are you using? It wouldn't surprise me if gcc sees `long long array[1<<20]` on the stack and turns it into the moral equivalent of `long long *array = malloc(1<<23);`. It *is* the array size which is the problem. Try with `1<<10` or the vector replacement and I bet it works. – Martin Bonner supports Monica Jun 28 '17 at 08:01
  • Either one works. :) However, I am still confused as to why I am able to assign such large array sizes outside of a struct... – vamaddur Jun 28 '17 at 08:04
  • 1
    @YouKnowMe - The compiler can optimize the arrays into the data segment if you place them directly into main. The two cases need not be comparable. – StoryTeller - Unslander Monica Jun 28 '17 at 08:05
  • Ah, I understand. I'll just change my code to use a vector. – vamaddur Jun 28 '17 at 08:31