-1
int array[x][x];

I want to find the largest value(EXACT value, like: x=20) of x that can compile without any error. Any ideas? Thanks!

Update: I post the original question below, I hope I didn't misunderstand something.

By trial and error, discover what the largest value of x can be in the following program so that it still compiles and runs without error:

main(){
  int array[x][x];
  array[0][0] = 0;
}

Print this value of x.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
  • 2
    possible duplicate of [The maximum size of an array in C](http://stackoverflow.com/questions/9386979/the-maximum-size-of-an-array-in-c) – Mitch Apr 05 '15 at 21:20
  • `getrlimit` may be of use. – Fiddling Bits Apr 05 '15 at 21:48
  • how would that would be of use exactly? – Ryan Apr 05 '15 at 22:52
  • 1
    Why do you need such a large amount of memory? – autistic Apr 05 '15 at 22:57
  • @undefined behaviour: It is one of my homework problem... – John Lewis Apr 05 '15 at 23:07
  • @undefined behaviour: Yes, it should be compile without any error just like the question said. – John Lewis Apr 05 '15 at 23:10
  • Hint, on normal x86_64 systems, you have just above 4 megabytes of stack space (with default target and memory models). So your largest `int`(4-bytes) `[x][x]` array will be roughly the max size for `16-bits` in each dimension. (`~32,768`). – David C. Rankin Apr 05 '15 at 23:10
  • @David C. Rankin: I understand. But I don't know how to get the exact value. – John Lewis Apr 05 '15 at 23:14
  • It ends up being a little more complicated due to the way the compiler can make use of both the `.data` and `.bss` segments for storage. I was surprised because generally you are limited to ~4 meg (`2^21` or `4,194,304`) for each memory segment. However, since the compiler can use both `.data` and `.bss` you double that to `8,388,608`. Divide by (`sizeof int` or `4`) yields `2,097,152`. Then take the square root to find the maximum array size (`1,448.15`). Which given the additional variables, etc. meant on my box, `1447` was in fact the maximum. – David C. Rankin Apr 06 '15 at 00:45

3 Answers3

1

The largest object for your system is SIZE_MAX (from stdint.h) bytes. By dividing that by sizeof int and taking the square root, you should be able to find the largest "square array", as I would call this.

Using that for your value of x will compile, but whether or not it'll run is a different question. Coincidentally, it does run on ideone, where I tested it... but I have a feeling that is purely by coincidence.

If it were to fail to run, I would divide x by 2 and try again... and continue to do so until I find something which runs.

At that point, I would set x to half way between the last failing value and the successful value and try again, rinse, lather, repeat...

There's not much point to this. It's a bit of a pointless exercise, because we usually strive to have as small of a memory footprint as possible.

autistic
  • 1
  • 3
  • 35
  • 80
  • Have you checked the result of your size computation on ideone? If I am not much mistaken, you get a round-up error when `SIZE_MAX` is converted to a double, which means that you are passing `2^64` instead of `2^64-1` to `sqrt()`, so that your calculation becomes: `sqrt(2^64)/2^2` This is exactly `2^31`, which, used as the 2D array size, yields 2^64 again, which is not representable by `size_t`. As such, you are allocating an array of size zero and got lucky that your program didn't crash. – cmaster - reinstate monica Apr 05 '15 at 23:34
  • @cmaster I did say "purely by coincidence", didn't I? – autistic Apr 06 '15 at 00:44
  • Yes, you did. Yet, this coincidence is a bit of the extreme kind. I just wanted to point out what's really happening in your code... – cmaster - reinstate monica Apr 06 '15 at 07:50
0
it depends on a number of environment items. such as:
32 or 64 bit machine? 
is there unlimited memory? 
what is the default (if not changed in a linker command file) size of the stack?
is this array to be on the stack or on the heap? 

experimentally, you could write a program that allocates/frees an ever expanding malloc, 
until the malloc fails, 
then the prior malloc sizing is the answer for your environment.
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • I don't see how this answers the question... The author isn't asking about `malloc`, the "stack" or the "heap". These are all irrelevant. – autistic Apr 05 '15 at 22:34
  • They are not irrelevant. As the OP has declared the array as a local variable, it will be most probably in the stack so the stack size matters – mcleod_ideafix Apr 05 '15 at 23:32
0

Perhaps not eleagnt, but manual bisection of array size worked in roughtly a dozen iterations to confirm the maximum int 2D array size on my system. With the default target and memory model on an x86_64 box, array[1447][1447] was the largest that would both compile and run. (much smaller than I thought, my original guess used giga, instead of mega...) The array consumes 8,375,236 bytes of memory (add an additional 8-bytes for i and j):

#include <stdio.h>

// #define MAXA 32768
// #define MAXA 10000
// #define MAXA 1000
// #define MAXA 5000
// #define MAXA 2500
// #define MAXA 1500
// #define MAXA 1200
// #define MAXA 1350
// #define MAXA 1425
// #define MAXA 1450
// #define MAXA 1437
// #define MAXA 1443
// #define MAXA 1446
// #define MAXA 1448
#define MAXA 1447

int main ()
{
    int i = 0;
    int j = 0;

    int a[MAXA][MAXA] = {{0}};

    for (i = 0; i < MAXA; i++)
        for (j = 0; j < MAXA; j++)
            a[i][j] = j;

    return 0;
}

At MAXA 1448:

$ ./bin/max_int_array
Segmentation fault

At MAXA 1447

$ ./bin/max_int_array
(no error)
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85