0

Say that I have the following arrays that I call in a function:

      int n = 20;
      double x[] = {
        0.003435700407453, 0.018014036361043, 0.043882785874337, 0.080441514088891,
        0.126834046769925, 0.181973159636742, 0.244566499024586, 0.313146955642290,
        0.386107074429177, 0.461736739433251, 0.538263260566749, 0.613892925570823,
        0.686853044357710, 0.755433500975414, 0.818026840363258, 0.873165953230075,
        0.919558485911109, 0.956117214125663, 0.981985963638957, 0.996564299592547
      };
      double w[] = {
        0.008807003569576, 0.020300714900193, 0.031336024167055, 0.041638370788352,
        0.050965059908620, 0.059097265980759, 0.065844319224588, 0.071048054659191,
        0.074586493236302, 0.076376693565363, 0.076376693565363, 0.074586493236302,
        0.071048054659191, 0.065844319224588, 0.059097265980759, 0.050965059908620,
        0.041638370788352, 0.031336024167055, 0.020300714900193, 0.008807003569576
      };

I would like to return the int n and the two arrays. I can do this by using a structure which is easy if I know the length of array x and w. However, the function depending on the inputs can return an array x of length 2,4,6,15, etc and an array w of length 2,4,6,15, etc. I do not know the length of array w and x.

I've created a structure:

 struct quadpts{    //structure used to pass multiple values into roeFlux
    int n;  //The specific heat ratio
    double *x;
    double *w;
};
typedef struct quadpts Quadpts;

and used:

Quadpts qpt = (Quadpts) malloc(size(Quadpts));

to assign the varied length values in array x and w. However, I realized that people were adding one value at a time in many of the examples I have seen and looked up. Is it possible to assign a whole array to a varied length array in a structure? Or am I limited to filling the array in the structure 1 by one. Can this be done using a for loop? If so, would I have to define

Quadpts qpt = (Quadpts) malloc(size(Quadpts));

in a different way each time to account for the new addition to the structure array?

Thank you for your help. I'm new at C and am limited to MATLAB knowledge.

EDIT:

I realized that I have problems with those big arrays. I'm using a switch case syntax in order to allocate different size arrays to the variable x and w. But I realized that I need to designate the size of those arrays to begin with and they will only be seen within the for loop. How can I make it such that the arrays are seen outside of for loops such that I can save them to the structures? The following is a shorten version of my code. I'm constantly getting error at double x[n] as previous definition was here or redefinition.

double quad1d(int pqOrder){
int n;
switch(pqOrder)
{
case 1:
    n = 1;
    double x[n] = {
        0.500000000000000
    };
    double w[n] = {
        1.000000000000000};
    break;

case 3:
      // Order 3 Gauss-Legendre points
      n = 2;
      double x[n] = {
        0.211324865405187, 0.788675134594813
      };
      double w[n] = {
        0.500000000000000, 0.500000000000000
      };
      break;
    }
int i;
Quadpts * qpt = (Quadpts*)malloc(sizeof(Quadpts));
for (i=0; i<n; i++){
    qpt->x = malloc(qpt->x_len * sizeof(double));
    qpt->w  = malloc(qpt->w_len * sizeof(double));
    qpt.x=x[i];
    qpt.w=w[i];


}
return &(qpt[0]);

}

ykmizu
  • 421
  • 1
  • 5
  • 7

1 Answers1

0

There may be other problems but one thing that stood out immediately was that having pointers in a struct isn't good enough, you have to use malloc() for each one in your case.

And if you declare those big arrays inside your function they will be on the stack which will be eventually overwritten.

Use:

struct quadpts{    //structure used to pass multiple values into roeFlux
    int n;  //The specific heat ratio
    double *x;
    int x_len;
    double *w;
    int w_len;
};

And inside the function:

qpt->x = malloc(qpt->x_len * sizeof(double));
qpt->w  = malloc(qpt->w_len * sizeof(double));

And then fill your arrays. Don't forget to free() them once you are done using them [outside your function].

Another mistake is that you should use malloc() with a struct pointer:

Quadpts * qpt = (Quadpts*)malloc(sizeof(Quadpts));

You would also need 3 lines of code once you no longer need it to avoid memory leaks.

free(qpt->x);
free(qpt->w);
free(qpt);
  • I realized that I have problems with those big arrays. I'm using a switch case syntax in order to allocate different size arrays to the variable x and w. But I realized that I need to designate the size of those arrays to begin with and they will only be seen within the for loop. How can I make it such that the arrays are seen outside of for loops such that I can save them to the structures? – ykmizu Mar 23 '13 at 19:38
  • By using malloc() inside the function those arrays will exist outside the function until free() is used on them. –  Mar 23 '13 at 19:39
  • what does qpt-> x mean? is the x refering to the large array x? – ykmizu Mar 23 '13 at 19:48
  • "->" is what you use when you have a struct pointer, if you declared a struct you use "." and qpt->x will be a pointer to the start of the x array (it's just a block of memory but that's what an array is). –  Mar 23 '13 at 22:09
  • I'm trying to fill the arrays with a for loop and using the syntax, qpt.x[i]=x[i] where x contains the values I want. I get the following error: request for member 'x' in something not a structure or union – ykmizu Mar 23 '13 at 22:14
  • If you use malloc() to allocate memory for your struct you can't use "." on the pointer, use "->" instead. –  Mar 23 '13 at 22:19
  • Just noticed your code. You have serious problems. I'm not going to rewrite your code for you, research malloc() and how it works, you are not using it correctly (by doing it over and over again in a loop!). Not to mention you are building the same arrays twice for some reason and not assiging anything to x_len and w_len. You should get a good C book, look here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list –  Mar 23 '13 at 22:23
  • After this is done, you said that I need to free it? Do I free this before the return statement or after? Also, how do I return this structure? I thought it would just be return &qpt; – ykmizu Mar 23 '13 at 22:28
  • dingrite, I'm sorry to bother you. Thank you for all the help you have given me. I decided to try another way and just make all the arrays into one big array and pass that. I don't have enough time to understand malloc. I'll be sure to when I have more time. Thank you. – ykmizu Mar 23 '13 at 22:38