array

Hi there
I am trying to write a code that in there I want to use these orders
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;

int main() {
    int i,j;
       long double c[1048576][2];
for (j=0;j<2;j++)
        for (i=0;i<1048576;i++)
             c[i][j]=0;
             c[0][0]=1;

         for (j=0;j<2;j++)
        for (i=0;i<1048576;i++)

          cout<<c[i][j]<<",";
return 0;
}

but it fails to do.
the result is:

process returned -1073741571 <0*C00000FD> execution time : 11.191 s


any help is appreciated.
im no expert here but i i think that array is way too big thats 1048576x2

my compiler ran it but the program crashed ....... too much memory to allocate
Last edited on
There is a limit of how much memory you can put on stack. I'm not sure how much that usually is. Several MB, I think. My guess is that you array is too big. You should be fine if you allocate it dynamically though.
try to allocate it as dynamic memory and use the nothrow thingy as argument to #include<new>
and if its null its too big
Last edited on
why i cant see the answers?
closed account (1vRz3TCk)
because, as has already bean said, the array is too big for the stack so you program is broken.

For an array that large you need to use the heap (dynamic memory).

See: Multidimentional arrays are evil
http://www.cplusplus.com/articles/G8hv0pDG/
Last edited on
oh and he is trying to assaign values to arrays during run time you cant do that remem array is a costant pointer


sorry sleepy

oh im sorry i meant the size of the array
Last edited on
closed account (1vRz3TCk)
zander, ???
c[i][j]=0;
c[0][0]=1;

he did it there or am i wrong im kinda tired so it could be my mistake
variables i and j are integers(-32767 to 32767) but in for loop you are trying to run the loop untill
1048576.array size is also matters because it stores on stack.
closed account (1vRz3TCk)
I think you are confused about something. you can assign values to arrays at runtime:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>

int main() 
{
    using namespace std;

    const int size_x = 10;
    const int size_y = 2;
    long double arr[size_x][size_y];

    for (int i = 0; i < size_x; ++i)
    {
        for (int j=0; j < size_y; ++j)
        {
            arr[i][j] = i * j;
        }
    }
    
    
    for (int iy = 0; iy < size_y; ++iy)
    {
        for (int ix = 0; ix < size_x; ++ix)
        {
            cout << arr[ix][iy] << " " ;
        }
        cout << endl;
    }
    return 0;
}
oh im sorry i meant the size of the array
closed account (1vRz3TCk)
mdpjpakki,

you raise a good point, the size of the indexer is important as is the size of the integer type.

Edit: by size I mean in terms of value range.
Last edited on
Except that sizeof int is usually 4.
Topic archived. No new replies allowed.