Hi,

This is probably, a really novice question, but I have some code that is using the malloc() function to declare an array, the problem is that I am using a special compiler to compile code for hardware which gives me a lot of errors because of the stdlib.h header file. My code looks as follows:

double (*x)[2];    // Pointer to variable is declared
x = malloc(2 * N * sizeof(double));    //Memory is allocated

//Then I fill the array
   for(i=0; i<N; i++)
   {
	   x[i][0]=data[i];
	   x[i][1]=0;
   }
//The array is then passed to a function
function(x);

In the header file which includes the function

void function(double (*x)[2]);

//The variable is then used as "x" to perform calculations

So I would like to declare it without using the malloc() function. Is it possible to that, and how would I declare the variable, I have tried a simple double x[N][2]; , and passing that to the function but I get compilation errors when I do that.
Thanks you,

Sam

You don't seem to have the malloc or the passing array part of the logic, just right. Post enough of the code that we can get an example that is meaningful to correct for you.

Those errors have to be studied - post them up with your expanded example. Minimum to show is the array declaration, and the function call, and the function that is being called.

The above snippets aren't enough info (for me at least).

Here is the whole code, I am using it to determine the Fast Fourier Transform of an array:

#include <stdio.h>
#include <stdlib.h>
#include "fft.h"

int main()
{
  int i;                    /* generic index */
  int N;                    /* number of points in FFT */
  double data[16]={3.6,2.9,6.3,5.6,4.0,4.8,9.1,3.3,0.4,5.9,4.8,5.0,2.6,4.3,4.1,4.0};
  double (*x)[2];           /* pointer to time-domain samples */
  double (*X)[2];           /* pointer to frequency-domain samples */



  N=16;
 
  /* Check that N = 2^n for some integer n >= 1. */

  /* Allocate time- and frequency-domain memory. */
  x = malloc(2 * N * sizeof(double));
  X = malloc(2 * N * sizeof(double));

  
	
   for(i=0; i<N; i++)
   {
	   x[i][0]=data[i];
	   x[i][1]=0;
   }
  /* Calculate FFT. */
  fft(N, x, X);

  /* Print time-domain samples and resulting frequency-domain samples.*/
  printf("\nx(n):");
  for(i=0; i<N; i++) printf("\n   n=%d: %12f %12f", i, x[i][0], x[i][1]);
  printf("\nX(k):");
  for(i=0; i<N; i++) printf("\n   k=%d: %12f %12f", i, X[i][0], X[i][1]);

	printf("\n %i\n", 5%2.1);

  return 0;
}

The fft.h file looks as follows:

#include <stdlib.h>
#include <math.h>

/* macros */
#define TWO_PI (6.2831853071795864769252867665590057683943L)

/* function prototypes */
void fft(int N, double (*x)[2], double (*X)[2]);
void fft_rec(int N, int offset, int delta,
             double (*x)[2], double (*X)[2], double (*XX)[2]);

/* FFT */
void fft(int N, double (*x)[2], double (*X)[2])
{
  /* Declare a pointer to scratch space. */
  double (*XX)[2] = malloc(2 * N * sizeof(double));

  /* Calculate FFT by a recursion. */
  fft_rec(N, 0, 1, x, X, XX);

  /* Free memory. */
  free(XX);
}

/* FFT recursion */
void fft_rec(int N, int offset, int delta,
             double (*x)[2], double (*X)[2], double (*XX)[2])
{
  int N2 = N/2;            /* half the number of points in FFT */
  int k;                   /* generic index */
  double cs, sn;           /* cosine and sine */
  int k00, k01, k10, k11;  /* indices for butterflies */
  double tmp0, tmp1;       /* temporary storage */

  if(N != 2)  /* Perform recursive step. */
    {
      /* Calculate two (N/2)-point DFT's. */
      fft_rec(N2, offset, 2*delta, x, XX, X);
      fft_rec(N2, offset+delta, 2*delta, x, XX, X);

      /* Combine the two (N/2)-point DFT's into one N-point DFT. */
      for(k=0; k<N2; k++)
        {
          k00 = offset + k*delta;    k01 = k00 + N2*delta;
          k10 = offset + 2*k*delta;  k11 = k10 + delta;
          cs = cos(TWO_PI*k/(double)N); sn = sin(TWO_PI*k/(double)N);
          tmp0 = cs * XX[k11][0] + sn * XX[k11][1];
          tmp1 = cs * XX[k11][1] - sn * XX[k11][0];
          X[k01][0] = XX[k10][0] - tmp0;
          X[k01][1] = XX[k10][1] - tmp1;
          X[k00][0] = XX[k10][0] + tmp0;
          X[k00][1] = XX[k10][1] + tmp1;
        }
    }
  else  /* Perform 2-point DFT. */
    {
      k00 = offset; k01 = k00 + delta;
      X[k01][0] = x[k00][0] - x[k01][0];
      X[k01][1] = x[k00][1] - x[k01][1];
      X[k00][0] = x[k00][0] + x[k01][0];
      X[k00][1] = x[k00][1] + x[k01][1];
    }

You have to specify a value, not a variable, when defining an array:

double x[16][2];

Yep, and that's because arrays are set at compile time, not run time. Although you can use define macros for this:

#include <stdio.h>
#define ARRSIZE 16

int main(void)
{
   int var[ARRSIZE];
   // (...)
}

Edited 6 Years Ago by darkbreaker: n/a

Thank you, I solved the problem, just one more quick question, is there a limit to the array size if I don't use the malloc() function? Right now I am trying to create a size of 32000 and I get a error (There is a stack overflow apparently when I try to debug it with VS 2008)
Thanks

If you declare something like

int var[32000]

The array will be put on the stack, so clearly you will be needing a very large stack. Off the top of my head I can't remember how you specify stacksize to the Visual Studio compiler.

There is a stack overflow apparently

To avoid consuming the stack space, make the array global or declare it static .

For example,

/* Statically allocated. */
double array_1[32000];

int main()
{
  /* Statically allocated, only visible inside main(). */
  static double array_2[32000];

  return 0;
}

You may want to see the /STACK linker option.

This question has already been answered. Start a new discussion instead.