-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.

  • 4
    `double A[800][800];` has 5MB and is on the stack. It is probably too big for your stack. – mch May 14 '19 at 08:09
  • 2
    Where is the definition of `matrix_element`? – klutt May 14 '19 at 08:09
  • Does it work if you move `double A[800][800];` out of `main` so it's not on the stack? – Blaze May 14 '19 at 08:10
  • 1
    Please provide [A Minimal, Complete, and Verifiable Example (MCVE)](http://stackoverflow.com/help/mcve). Where is the function `matrix_element` defined? – David C. Rankin May 14 '19 at 08:17
  • @Broman if you're interested, I'm posting the full code in the next answer. – Amit Hasan Arpon May 14 '19 at 08:18
  • 1
    Why not just `"Edit"` your question and ***Add*** the new information at the end? – David C. Rankin May 14 '19 at 08:19
  • 1
    @AmitHasanArpon Don't post full code. Post a [mcve] and post it in the question. – klutt May 14 '19 at 08:19
  • @Blaze, how do I do that? – Amit Hasan Arpon May 14 '19 at 08:20
  • 1
    First fix a [mcve] and then click edit to edit the question. – klutt May 14 '19 at 08:23
  • 1
    You missed the "minimal" part. I'm pretty sure you can remove 80% of that code and still reproduce your problem. – klutt May 14 '19 at 08:25
  • The functions S, D, vF, h, A and F could probably be replaced by dummy values. – klutt May 14 '19 at 08:27
  • @Broman Well, it seems to be the A[800][800] thing, because when I try with A[100][100], it works fine. But my project requires the 800x800 array. Suggestions? And if you must have the minimal code, I can post that too. Doing it now. – Amit Hasan Arpon May 14 '19 at 08:34
  • @AmitHasanArpon It's not so much that I need it right now. I'm just teaching you in how to properly ask a question here. And you would be surprised how many times you realize what the problem is during the creations of a mcve ([mcve]). Very often that leads to that I don't even have to post a question here. It's a great debugging tool. – klutt May 14 '19 at 08:37
  • I'm sorry. I edited and posted it now. Please don't be offended. And help! @Broman – Amit Hasan Arpon May 14 '19 at 08:42
  • @AmitHasanArpon I posted an answer to explain that, hope it helps – Blaze May 14 '19 at 08:47
  • @AmitHasanArpon I'm not offended. Asking questions takes some time to learn. Actually, this code would probably be enough to demonstrate your problem: `void main() { double A[800][800]; A[0][0]=42.0; printf("%lf",A[0][0]); }` – klutt May 14 '19 at 08:54
  • @Blaze that worked! Thanks man! You saved me from my professor's wrath ^_^ – Amit Hasan Arpon May 14 '19 at 09:17

4 Answers4

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.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Global scope variables are stored in a seperate storage location for Static and Global variables. They are not stored in the heap. – Rishikesh Raje May 14 '19 at 09:33
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);
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
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.

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
  • 1
    `double* A = new double[m*n];` in a C program? – Mathieu May 14 '19 at 08:23
  • 1
    Don’t cast the return value from malloc. – Fredrik May 14 '19 at 08:25
  • The problem does seem to be the size of the allocation. If I try it with A[100][100], it works fine. How do I get around this problem? – Amit Hasan Arpon May 14 '19 at 08:25
  • 1
    In C it is bad practice to cast the result of a call to `malloc`. It serves no purpose because `void*` will convert implicitly and it can hide mistakes such as forgetting to include `stdlib.h`. (https://stackoverflow.com/a/605858/139746 ) – Pascal Cuoq May 14 '19 at 08:27
  • With `double* A = malloc(sizeof(double)*m*n);`, The `A` variable won't be accessible in A[x][y] form. – Mathieu May 14 '19 at 08:27
  • As has been said before it is the size of your stack that is the problem. Just allocate it on the heap. This also makes it possible to easily share the large dataset with other functions and modules without constantly copying it through the function scopes. – Tarick Welling May 14 '19 at 08:28
  • @PascalCuoq my bad. I usally use C++ and my qt project was in C++ so it warned me. I fixed the answer. – Tarick Welling May 14 '19 at 08:29