array

Aug 4, 2011 at 7:40am
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.
Aug 4, 2011 at 7:47am
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 Aug 4, 2011 at 7:50am
Aug 4, 2011 at 7:53am
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.
Aug 4, 2011 at 7:54am
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 Aug 4, 2011 at 8:04am
Aug 4, 2011 at 8:05am
why i cant see the answers?
Aug 4, 2011 at 8:14am
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 Aug 4, 2011 at 8:30am
Aug 4, 2011 at 8:25am
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 Aug 4, 2011 at 9:03am
Aug 4, 2011 at 8:32am
closed account (1vRz3TCk)
zander, ???
Aug 4, 2011 at 8:45am
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
Aug 4, 2011 at 8:48am
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.
Aug 4, 2011 at 8:52am
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;
}
Aug 4, 2011 at 9:01am
oh im sorry i meant the size of the array
Aug 4, 2011 at 9:28am
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 Aug 4, 2011 at 9:53am
Aug 4, 2011 at 9:49am
Except that sizeof int is usually 4.
Topic archived. No new replies allowed.