-4

I have an integer array list and I want to make left right pointer array list to access this. But for more than 690.000 elements (for example 700.000) the program stops and says `Segmentation fault (core dumped). I have used std::vector before but I have to use simple array list because of speed. And This code is not about my program it is very simple way to tell my problem with code. And I have to use global variable because this code run in a class methods.

int k=698900;


int n1=k/2;
int n2=k/2;
int *left[n1];
int *right[n2]; 
int list[k];

for(int i=0; i<k; i++){
    list[i] = i;
}

for(int i=0; i<n1; i++){
    left[i] = &list[i];
}

for(int i=0; i<n2; i++){
    right[i] = &list[i];
}

for(int i=0; i<n2; i++){
    cout << *right[i] << endl;
}
  • 1
    Hard to tell from your code fragment, but my guess is that you made these all local variables and so you are getting a classic stack overflow. – Paul R Oct 19 '13 at 15:31
  • @PaulR which leads us to the 2 possible solutions of either increasing the stack size or allocating the arrays on the heap (by using a `std::vector` for example). Edit: or the third one @PaulR mentioned :P – PeterT Oct 19 '13 at 15:34
  • Or make the arrays static/global. – Paul R Oct 19 '13 at 15:36

1 Answers1

1

As @PaulR already said in the comments your problem is a stack overflow. You can solve this problems a few ways. If you're categorically against changing your code then you can adjust the stack size in your compiler. How exactly that is done depends on your compiler (gcc example and VS example). You can also adjust it at run-time but that's an OS dependent function, so I'm not going to go into details about that.

The solution that I would prefer is allocate the memory on the heap. This can be done by either allocating the arrays on the heap int **left = new int*[n1]; but then you have to remember to call delete []left; when you don't need the memory anymore. The far more elegant solution would be to use a std::vector instead.

I challenge you to compile this with all optimizations on and tell me that it's significantly slower than your code.

int k=698900;


int n1=k/2;
int n2=k/2;
//not that I condone storing raw pointers in a vector
//admittedly this is a rather dangerous thing to do
//storing indices would be smarter because upon adding
//elements the memory could be relocated
std::vector<int *> left(n1);  
std::vector<int *> right(n2); 
std::vector<int> list(k);

for(int i=0; i<k; i++){
    list[i] = i;
}

for(int i=0; i<n1; i++){
    left[i] = &list[i];
}

for(int i=0; i<n2; i++){
    right[i] = &list[i];
}

for(int i=0; i<n2; i++){
    cout << *right[i] << endl;
}

Like @PaulR mentioned you can also allocate the arrays statically (static int *left[n1];) or globally (outside of any function or class declaration/definition)

Community
  • 1
  • 1
PeterT
  • 7,981
  • 1
  • 26
  • 34
  • Thank you for reply that is what I want :) int **left = new int*[n1]; I wonder what is the difference between this (int **left = new int*[n1];) and this ( int *left[n1];) – user2879337 Oct 19 '13 at 16:29
  • the difference is very well explained [in other places](http://stackoverflow.com/questions/6713637/heap-vs-stack-allocation). Like I said, one is called stack allocation and the other is heap allocation. – PeterT Oct 19 '13 at 16:31
  • I see but I wonder difference between stack allocation and the other :) anyway thanks you I can google it. – user2879337 Oct 19 '13 at 16:42