0

I'm trying to make a 2D random array that prints itself out on a document and when I put anything above [1448][1448] the array doesn't work and returns "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8)" if I put anything below that it works but I don't understand why it doesn't work, I have over a gigabyte of free memory on my mac.

    FILE * fdave = fopen("/Users/dave/Documents/dave.txt", "w");
    int mapx, mapy, tempx = 0, tempy = 0;
    printf("X Length: 0-");
    scanf("%d", &mapx);
    printf("Y Length: 0-");
    scanf("%d", &mapy);
    int grid[mapy][mapx];

    fprintf(fdave, "{\n");
    for (tempy = 0;tempy  < (mapy); tempy++){
        fprintf(fdave, "{");
        for (tempx = 0; tempx < (mapx); tempx++){
            grid[tempy][tempx] = rand() % 10;
            if (tempx == mapx - 1){
                fprintf(fdave, "%d", grid[tempy][tempx]);
            }else{
                fprintf(fdave, "%d, ", grid[tempy][tempx]);
            }
        }
        if (tempy == mapy - 1){
            fprintf(fdave, "}\n");
        }else{
            fprintf(fdave, "},\n");
        }
    }
    fprintf(fdave, "}\n");

    fclose(fdave);
  • 8
    The array is probably too big for the stack. – Weather Vane May 06 '20 at 17:13
  • Local variables are usually put on the process tack. The stack is a limited resource, on Windows it's a single MiB, on Linux it's 8 MiB. `1448 * 1448 * 4` (`4` is the common `sizeof(int)`) is well over 8 MiB. – Some programmer dude May 06 '20 at 17:15
  • You need to tell the linker to allocate more stack size. What compiler/linker are you using? – jwdonahue May 06 '20 at 17:15
  • Why do you even need an array? The code is just writing random values to file. – Weather Vane May 06 '20 at 17:15
  • 3
    As a possible solution you could use dynamic allocation using `malloc`. – Some programmer dude May 06 '20 at 17:15
  • 1
    Telling the linker/runtime to allocate more stack is not the right solution here. If you need an object that large, you use `malloc`. It doesn't make sense for a permanent resource to be allocated as huge for something needed by one function. – R.. GitHub STOP HELPING ICE May 06 '20 at 17:29
  • Thank you guys so much for the help, I'll look into everything all of you have said, the reason I want this to be an array is so that I can store a map inside of a document and I want it on the document so that I can pull it out and know what is suppose to be there on the map. I was testing out arrays and stuff to learn as I am new to programming. – JoshDave May 06 '20 at 17:46
  • duplicates: [EXC_BAD_ACCESS at main method declaration](https://stackoverflow.com/q/20314489/995714), [Creating a double array with 3 million elements](https://stackoverflow.com/q/33307543/995714) – phuclv May 07 '20 at 12:36

2 Answers2

1

I would use malloc. Something like this:

    int WIDTH = 1448;
    int HEIGHT = 1448;

    int (*array2d)[WIDTH] = malloc(sizeof(int[WIDTH][HEIGHT]));

    array2d[100][100] = 1;

    free(array2d);
CodeWash
  • 155
  • 6
0

The stack memory is limited. 1488 * 1448 * sizeof(int) (4 Byte) = 8386816 Byte is probably way too much to be able to store at the stack.

Rather use dynamically allocated memory, which furthermore has the advantage that you can resize the memory and also free the allocated memory after its use:

int (*ptr)[mapy] = malloc(sizeof(int) * mapy * mapx);
if(!ptr)
{
    fprintf(stderr, "Memory allocation of ptr failed!");
    return 1;
}

ptr[1][1] = 24;

free(ptr);