-3

I declared an array in C of size 150X150X150. Upon compiling the program to get an array of same size,the compiler gave no errors or warnings. but when I tried running it, the program stops responding.

void main(){
 int i,j,k;
 char giv[150][150][50],tar[150][150][50];
 for(int i=0;i<150;i++)
 {
  for(j=0;j<150;j++)
  {
   for(k=0;k<50;k++)
    cin>>giv[i][j][k];
  }
 }
}

Is there any way that I can create an array of 150*150*150 without causing a run time error? EDIT: I know multidimensional arrays work. This is not a compilation error. Its a run time error, whose cause was I am not able to pinpoint.

7
  • 2
    It is unclear what you're asking but if you're asking whether multi-dimensional arrays are possible, the answer is yes for all C, C++ and Java. Commented Feb 27, 2015 at 17:47
  • 2
    Tried executing what? You can't execute an array. Commented Feb 27, 2015 at 17:48
  • 1
    Maybe post a small sample on how you tried to use your array. You're probably having an infinite loop if it stops "responding". Commented Feb 27, 2015 at 17:49
  • 2
    If they are locally declared arrays you might have broken the stack. Use malloc() to allocate dynamic memory. Commented Feb 27, 2015 at 17:49
  • 1
    How do I ask a good question? Commented Feb 27, 2015 at 17:49

3 Answers 3

7

You just declared two arrays on the stack.

Each array has size: 150 * 150 * 50 bytes, or about 1.1MB.
So you are asking for 2.2MB from the stack.

Typical stack size is about 1 or 2MB.

So I expect you're getting a StackOverflow Exception.
(kinda appropriate for this site)

Sign up to request clarification or add additional context in comments.

Comments

2

You could allocate the arrays on the heap:

#include <stdlib.h> /* for malloc()/calloc() */
#include <stdio.h> /* for perror() */

...

char (*pgiv)[150][150][50] = malloc(sizeof *giv);
char (*ptar)[150][150][50] = malloc(sizeof *tar);

If you want to have the arrays' elements initialised to all 0s on allocation use calloc() as follows:

char (*pgiv)[150][150][50] = calloc(1, sizeof *giv); 
char (*ptar)[150][150][50] = calloc(1, sizeof *tar);

Also test wether the allocation succeed or not:

if (NULL == pgiv)
  perror("malloc() failed");
if (NULL == ptar)
  perror("malloc() failed");

Address an element by doing for example:

(*pgiv)[0][1][2] = 123;

Note that pgiv and ptar are actually pointers (to an array). That's why they need to be dereferenced (using the dereference operator *) before being used like an array.

Comments

2

It seems that the problem is with the limit of the stack memory.

In C++ you could use for example standard container std::vector.

In C you could allocate these arrays yourself dynamically.

The simplest way is either to declare these arrays globally that is outside any function or specify keyword static that the arrays had static storage duration. For example

static char giv[150][150][50],tar[150][150][50];

As for other languages then for example Java and C# allocate arrays in the managed heap. It keeps in the stack only a reference to the array.

11 Comments

The question was about C.
But std::vector is C++, not C
@zenith Is it a big problem? :)
@VladfromMoscow Now it's better :)
@Weather Vane It is difficult to say. Maybe it is time to look at other languages.:)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.