Why is the program not working for an array but works fine when it is a normal variable?

-1

I'm trying to write a code that takes a couple of inputs, calls upon the function to calculate a value, assigns that value to a variable and print that variable. I actually need to form a matrix out of these values.

When I assign the value to a regular variable, the program does what is expected. So if the main function is like this:

//some user-defined functions that the main function calls//

void main()
{
    int m,n;
    printf("Row: ");
    scanf("%d",&m);
    printf("Column: ");
    scanf("%d",&n);
    double k,A;
    printf("k= ");
    scanf("%lf",&k);
    A=matrix_element(m,n,k);
    printf("%lf",A);
}

The output is correct:

Row: 1
Column: 1
k= 1
2.275499
Process returned 8 (0x8)   execution time : 5.974 s
Press any key to continue.

But when I try to assign the value to an element in an array, the program doesn't work. I mean to say, if the main function is like this:

//some user-defined functions that the main function calls//

void main()
{
    int m,n;
    printf("Row: ");
    scanf("%d",&m);
    printf("Column: ");
    scanf("%d",&n);
    double k,A[800][800];
    printf("k= ");
    scanf("%lf",&k);
    A[m][n]=matrix_element(m,n,k);
    printf("%lf",A[m][n]);
}

The program does nothing:

Process returned -1073741571 (0xC00000FD)   execution time : 2.207 s
Press any key to continue.

What is happening and how can I fix it? I have to take the values in an 800x800 2D array because my end goal is to put it in a for loop and form a matrix of these dimensions.

For those who want to see the MCVE code, here it is:

#include <stdio.h>

double S(int g, int i) //Scattering Cross-section//
{
    double x;
    x=2;
    return x;
}

double D(int g, int i) //Diffusion co-efficient//
{
    double x;
    x=2;
    return x;
}

double A(int g, int i) //Absorption Cross-section//
{
    double x;
    x=2;
    return x;
}

double vF(int g, int i) //velocity x Fission Cross-section//
{
    double x;
    x=2;
    return x;
}

double F(int g, int i) //Fission Cross-section//
{
    double x;
    x=2;
    return x;
}

double h(int i) //Height//
{
    double x;
    x=2;
    return x;
}

double matrix_element(int m, int n, double k)
{
    int g,i;
    double C;
    C=1;
    return C;
}

void main()
{
    int m,n;
    printf("Row: ");
    scanf("%d",&m);
    printf("Column: ");
    scanf("%d",&n);
    double k,A[800][800];
    printf("k= ");
    scanf("%lf",&k);
    A[m][n]=matrix_element(m,n,k);
    printf("%lf",A[m][n]);
}

This one produces the same problem.

c
arrays
double
codeblocks
asked on Stack Overflow May 14, 2019 by Amit Hasan Arpon • edited May 14, 2019 by Amit Hasan Arpon

4 Answers

0

A quick fix is to put the array in the global scope instead of declaring it inside of a function:

double A[800][800];

void main()
{
    int m, n;
    printf("Row: ");
    scanf("%d", &m);
    printf("Column: ");
    scanf("%d", &n);
    double k;
    printf("k= ");
    scanf("%lf", &k);
    A[m][n] = 123.0; // for this example you can replace "matrix_element" with a dummy value
    printf("%lf", A[m][n]);
}

This way it is in a seperate storage location instead of on the stack. double often is 8 bytes, so A[800][800] is 4.88 MB big. That's huge for the stack and may cause a stack overflow.If it's global that shouldn't be a problem unless you're on an embedded system, an old machine or an other system where memory is very limited. See this question for more details on how much memory there is for the stack, but typically, a well-written program shouldn't come close to that.

answered on Stack Overflow May 14, 2019 by Blaze • edited May 14, 2019 by Blaze
0

The 2D array A is mostly likely causing a stack overflow due to its size. Large local variables (i.e. variables with automatic storage duration) should be avoided. Instead use dynamic allocation.

For a dynamic allocated 2D array do like this:

#define ARR_SIZE 800

...

double (*A)[ARR_SIZE] = malloc(ARR_SIZE * sizeof *A);

...

A[m][n] = 42.0;

...

free(A);
answered on Stack Overflow May 14, 2019 by 4386427 • edited May 14, 2019 by 4386427
0

Besides defining the matrix as a global variable or allocating it from a heap using malloc, you can declare a bigger stack for your program. However, the steps is OS/compiler dependent. You will need to consult your compiler documents for detail.

Each method (global, heap, and stack) has its strength/weakness. You should choose the best solution for your needs, even if they all solve the problem.

-1

The problem seems to be the size of the allocation on the stack. The application is crashing at

double k,A[800][800];

Because this array is allocated at the start of the program and the size of the array would be 800*800*8 = 5MB. Which is too big for your stack.

The code works when it is written as:

double* A = malloc(sizeof(double)*m*n);

This would allocated memory on the heap and would make it work.

answered on Stack Overflow May 14, 2019 by Tarick Welling • edited May 14, 2019 by Tarick Welling

User contributions licensed under CC BY-SA 3.0