1

I'm trying to create a C program to measure how long it takes to copy a 2D array into another. When I run my code, I get:

exited with code=3221225725

Now, this is code provided by the instructor, character for character, but it seems to work for him and not for me. Any idea why I get a stack overflow and he doesn't? When I use a number smaller than 512, it works, but it has to be 512 for my assignment.

#include <stdio.h>
#include <time.h>

int main()
{
    clock_t start, end;
    double time_elapsed;

    int i, j;
    int src[512][512];
    // initialize src
    for (i = 0; i < 512; i++)
        for (j = 0; j < 512; j++) 
            src[i][j] = i*512 +j;

    int dst[512][512];

    start = clock();

    // data copy to measure
    for (i = 0; i < 512; i++)
        for (j = 0; j < 512; j++)
            dst[j][i] = src[j][i];

    end = clock();
    time_elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;

/*
    // print dst for verification
    for (i = 0; i < 512; i++)
        for (j = 0; j < 512; j++)
            printf("\n%d\n", dst[i][j]);
*/
    printf("%f\n", time_elapsed);
   
    return 0;
}
Serif
  • 11
  • 2
  • 1
    Check the stack size in your environment? If it's <2MB - make it larger – littleadv Feb 01 '22 at 03:41
  • 1
    an integer is 4 bytes so a 2d array of integers is (4x512)x(4x512) = 1,048,576 Bytes = 1024 Kilobytes = 1 Megavytes this 1 MB has to be available in CPU stack memory (cache) it could be you have an old CPU with less memory aside from other in process variables. – Abdul Rauf Feb 01 '22 at 03:44
  • 4
    You have two arrays (`src` and `dst`) that are a megabyte each. It's really bad practice to declare such large arrays as local variables. So you should discuss this with your instructor, and get his advice on the matter. Maybe the whole point of this exercise is to teach that you shouldn't be doing this. – user3386109 Feb 01 '22 at 03:58
  • 2
    Your instructor is probably using a different operating system than you are, which is configured to have a larger maximum stack size. This does not mean that your operating system is badly configured. It rather means that your instructor is doing something dangerous, that should normally not be done. – Andreas Wenzel Feb 01 '22 at 04:10

0 Answers0