0

This is a simple code to get an array of a random walk. If I increase the array size anymore it gives me a segmentation fault. I need it to be larger and 2D (like a matrix). How can I do that without getting the error?

#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

int main(){

    srand (time(NULL));

    int iter = 1000000;
    int x[iter]; x[0] = 0;
    int p = 50;
    int r;

    for(int i=1; i<iter; i++){
        r = rand() % 101;
        if(r<p){
            x[i+1] = x[i] - 1;
        } else if(r>p){
            x[i+1] = x[i] + 1;
        } else if(r=p){
            x[i+1] = x[i];
        }
    }

    ofstream myFile("walk.csv");
    for(int j=0; j<iter; j++){
        myFile << x[j] << "\n";
    }

    myFile.close();

    return 0;
}

2 Answers2

3

To use such a big array, you will need to use dynamic memory. Large array like that cannot be on the stack, it will overflow the stack.

One of the best tool for that is a std::vector:

#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>

using namespace std;

int main(){

    srand (time(NULL));

    int iter = 1000000;
    vector<int> x(iter);

    // This line is a bit redundant since vectors
    // are initialized with zeros by default
    x[0] = 0;
    int p = 50;
    int r;

    for(int i=1; i<iter; i++){
        r = rand() % 101;
        if(r<p){
            // Be careful, here you index outside the array
            // i will go to iter, but you are doing i+1
            // you should spell your for like this:
            // for(int i = 1; i < iter - 1; i++)
            x[i+1] = x[i] - 1;
        } else if(r>p){
            x[i+1] = x[i] + 1;
        } else if(r=p){
            x[i+1] = x[i];
        }
    }

    ofstream myFile("walk.csv");
    for(int j=0; j<iter; j++){
        myFile << x[j] << "\n";
    }

    myFile.close();

    return 0;
}

As you can see, that type from std is really acting just like an array, but has dynamic properties.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • Thanks for also noticing the mistake. :) – RetroCausalElectron May 11 '21 at 15:18
  • @RetroCausalElectron no problem! Just don't forget to add the `#include `. If you remove the `using namespace std;` (which I recommend) you'll also need to write `std::vector` – Guillaume Racicot May 11 '21 at 15:35
  • Why do you recommend removing std? – RetroCausalElectron May 11 '21 at 15:43
  • @RetroCausalElectron We see many questions here about why some code fails to compile, just to see that the function conflicts with the std. Also, all calls to the functions from std because unqualified calls, which is a whole can of worm. Also, I would recommend reading some of the answers there: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/2104697) – Guillaume Racicot May 11 '21 at 15:59
2

You’ll need to dynamically allocate the array or make it a global variable. The stack space for that variable is exhausted by that single instruction.

int * x = new int[iter]; //or however you choose to name it
delete []x; //DO NOT FORGET TO DELETE THIS

`

h0tst3w
  • 114
  • 4