Creating an array of variable size

Hi, I am trying to create an array which has size depending on the length of a text file that is read in. My code is as follows:

    // Do not count empty lines or comments
    if(line.length() == 0 || line[0] == '#' || line[0] == '/' || line[0] == '$'){continue;}

 int numberofevents=numberoflines/2000;
 int arraysize=2*numberofevents;
 int scopedata [arraysize][1000];

This returns Error: Non-static-const variable in array dimension ReadFastRoot.C:31:
(cint allows this only in interactive command and special form macro which
is special extension. It is not allowed in source code. Please ignore
subsequent errors.)

How can i get around this?


google for:
dynamically allocated multi-dimensional arrays in c++

Otherwise you can use the c++ vectors which grow up automatically when you add an element. Thus, you do not need to care about the size of your array, a priori.

So i created what i believe is a 2D array using the following:

int** scopedata = new int*[1000];
    for(int i=0; i < 1000; i++){
      scopedata[i] =new int[arraysize];

(arraysize is 400 - checked using cout)

however, I am able to access elements which should not exist, for example

cout << scopedata[999][500]; returns 0. I would expect a memory address error for this value

Why is it that i can access such elements? -> Create a two-dimensional array at runtime -> Multi-Dimensional Arrays -> Introduction to dynamic two dimensional arrays in C++