0

I run code below and get some strange output:

#include <iostream>

using namespace std;

int main()
{
    for(int ll=1444; ll<1450; ++ll)
    {
        cout<<ll<<endl;

        cout<<"###### 0 #######"<<endl;
        int mtx[ll][ll];
        cout<<"###### 1 #######"<<endl;
    }
    return 0;
}

The output is:

1444
###### 0 #######
###### 1 #######
1445
###### 0 #######
###### 1 #######
1446
###### 0 #######
###### 1 #######
1447
###### 0 #######
###### 1 #######
1448
###### 0 #######
Segmentation fault

I tried checked one by one with ll's value, when ll reaches 1448, segmentation fault does happen.

Then I changed the array from int to bool, problem disappears.

A calculation based on ll's value:

ll=1447, total space of array is 1447*1447*4 = 8375236 bytes = 8178.95 Kbytes
ll=1448, total space of array is 1448*1448*4 = 8386816 bytes = 8190.25 Kbytes

Could a possible reason be the size of this array is bigger than default page size? (How to check it in Ubunut 14.04..?)

BTW, tried with java and there is no problem.

Dave Xu
  • 147
  • 1
  • 11
  • 3
    [This question](http://stackoverflow.com/q/851122/2706918) may help you. – Nitinkumar Ambekar Sep 01 '15 at 04:57
  • 1
    In standard C++, array dimensions must be known at compile-time. If this code compiles for you, then you are using a non-standard compiler extension – M.M Sep 01 '15 at 05:19

2 Answers2

5

You are allocating your array into the stack, and the default stack size is usually 8 MB.

Ghooo
  • 496
  • 1
  • 3
  • 11
2

What you are doing here is allocating an array of [n][m] on the stack every cycle. Depending on how large the stack is (you can set this, but there are limits) you will eventually run out of memory.

What you want to do is to allocate memory on the heap using the new operator or use a container that will do this for you.

Since you are using C++,

Using STL

#include <vector>
#using std 

This creates a vector of vectors, each of dimension ll.

vector<vector<int>> v(ll, vector<int>(ll, 0));

This creates a 1-d vector of the same size as your 2d array. I would typically use this approach because it gets the job done with the least fuss. The drawback here is that you would have to address the vector in the form v[i*ll + j] instead of v[i][j]

vector<int> v(ll*ll, 0)

Without Using STL

This creates an array of arrays on the heap, and is not contiguous. you have to remember to call delete in a loop after you are done with the data structure. This is considered bad because you are creating an array of arrays, which is usually a less efficient data structure as its not contiguous.

int **v = new int *[ll];
for (int i = 0; i < ll; i++)
    v[i] = new int[ll];

This creates a contiguous 2d array on the heap, see this for more details

int (*array)[256] = malloc(512 * sizeof *array);

Also see How do I declare a 2d array in C++ using new?

for creating a 2d array using new

to fix this code I would do the following:

int main()
{
for(int ll=1444; ll<1450; ++ll)
{
    cout<<ll<<endl;

    cout<<"###### 0 #######"<<endl;
    std::vector<int> v(ll * ll, 0);
    cout<<"###### 1 #######"<<endl;
}
return 0;
}

code not tested but you get the idea

Community
  • 1
  • 1
aCuria
  • 6,935
  • 14
  • 53
  • 89
  • Using _new_ is a possible solution, don't know down-votes for what! – Nitinkumar Ambekar Sep 01 '15 at 05:04
  • Yeah, it would be nice if down-votes are explained. I don't thing anything here is fundamentally wrong. Since the OP is using C++ its good to understand how to use STL – aCuria Sep 01 '15 at 05:05
  • 2
    No idea why the downvote, but there **is** at least one fundamentally wrong statement: `Using a 2-d array is considered bad because you are creating an array of arrays, which is usually a less efficient data structure`. C-style 2-d arrays are just flat arrays. Also this answer only vaguely addresses OP's question. – user703016 Sep 01 '15 at 05:07
  • I was wrong, on the stack they are laid out identically, on the heap it depends on how you do it. I do recall being dissuaded from using 2d arrays in favour of 1d ones in CS class years though, not sure exactly why now. At the very least, they are more complicated to use. see this link for how to heap allocate a 2d array contiguously. http://stackoverflow.com/questions/10116368/heap-allocate-a-2d-array-not-array-of-pointers – aCuria Sep 01 '15 at 05:23