1

I have multidimensional array

double coeff[10][3][12][519][11];

and I set the values for this array in a include-file ( #include "call_of_functions.h") in another function:

#include <stdio.h>

void func_T5_D1(double s, double t, double mt, double coeff[10][3][12][519][11])
{
 #include "call_of_functions.h"
}

and I call this function in main.c

int main(){
double s, t, mt;
double coeff[10][3][12][519][11]={0};
double ex;

printf("enter 3 values for s, t and mt:\n");
scanf("%lf %lf %lf", &s, &t, &mt);

printf("%lf %lf %lf\n", s, t, mt);
func_T5_D1( s,  t, mt, coeff);
ex = coeff[5][1][10][309][10];

printf("%.14e \n",ex);
return 0; 
}

However I get a segmentation fault. If I include the #include "call_of_functions.h" in main.c, it works well.

  • How can I return the values of this 5 dimensional array in main?
  • Or there is another alternative way to do that?
10
  • 2
    We don't have enough information to diagnose the problem. Please - either reduce the problem to something we can reproduce - or launch a debugger on your code to have a hint where the error occurred. Commented Feb 13, 2014 at 10:25
  • 2
    First build with debug information (use the -g flag for gcc), then run in a debugger. When you run in a debugger, it will stop on the location of the crash, and there you can examine and even walk up the function call stack, and examine the values of variables. At least edit your question to include the function call stack (use the command bt in the debugger gdb). Commented Feb 13, 2014 at 10:26
  • 2
    Your coeff array is around 16 MB in size, so you probably have a stack overflow - either make it static (quick and dirty fix) or allocate it dynamically with malloc (proper fix). Commented Feb 13, 2014 at 10:29
  • 2
    That's almost 16 MB of stack space, which might really be too large for your platform to be happy. Commented Feb 13, 2014 at 10:29
  • 1
    @EliasVanOotegem, that is very irrelevant. The OP has actually sent the array to the function as an argument. The array is not local to the function (although the title of the question misleadingly implies so). Commented Feb 13, 2014 at 10:54

1 Answer 1

4

10*3*12*519*11* sizeof(double) is likely 16441920 bytes, which may be greater than the available stack space.

You may make the array global or allocate it dynamically.

Also note that the array is not passed by value to the function, only its address, so you have no problems with "returning the array" as you seem to think.

PS. As for the dynamic allocation, in your case you may do:

double (*coeff)[3][12][519][11];
coeff = calloc (1, 10 * sizeof (*coeff));

and don't forget to free it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. that is really the problem, I set "limit stacksize 16M" and that works. But how can I do this dynamically?
@chill calloc (as in the above example) and malloc already do the allocation dynamically. Your example in your question tries to allocate on the stack, which has an implementation-defined size and usually assumed to be small compared to the system RAM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.