title: How to develop own block in Scilab/Xcos with C language usage
body: <p>I have been developing my own block in Scilab/Xcos with the CBLOCK4 usage.
  The C language code inside the block is following:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &quot;scicos_block4.h&quot;
  
  #define U  ((SCSREAL_COP *)GetRealInPortPtrs(block, 1))
  #define Y  ((SCSREAL_COP *)GetRealOutPortPtrs(block, 1))
  #define X  ((SCSREAL_COP *)GetState(block))
  #define dX ((SCSREAL_COP *)GetDerState(block))
  #define Xk ((SCSREAL_COP *)GetDstate(block))
  #define W  ((SCSREAL_COP *)GetWorkPtrs(block))
  
  // parameters
  #define N (GetIparPtrs(block)[0])
  
  typedef struct
  {
      double *buffer;
      double *sum;
      int    *index;
  }MovingAverage_t;
  
  FILE *f;
  
  void MovingAverage(scicos_block *block,int flag)
  {
  
    MovingAverage_t *ptr;
    int bufferPos;
  
    if(flag == 4) 
    {
      /* init */
  
      f = fopen(&quot;Debug.txt&quot;, &quot;w&quot;);
  
      if((*(block-&gt;work) = (MovingAverage_t *)scicos_malloc(sizeof(MovingAverage_t))) == NULL)
      {
          set_block_error(-16);
          return;
      }
      ptr = (MovingAverage_t*)*block-&gt;work;       
  
      fprintf(f, &quot;ptr: %ld \n&quot;, ptr);
  
      if((ptr-&gt;buffer = (double*)scicos_malloc(sizeof(double)*N)) == NULL)
      {
          scicos_free(*(block-&gt;work));
          fclose(f);
          set_block_error(-16);
          return;
      }
      
      if((ptr-&gt;sum = (double*)scicos_malloc(sizeof(double))) == NULL)
      {
          scicos_free(*(ptr-&gt;buffer));
          scicos_free(*(block-&gt;work));
          fclose(f);
          set_block_error(-16);
          return;
      }
  
      if((ptr-&gt;index  = (int*)scicos_malloc(sizeof(int))) == NULL)
      {
          scicos_free(*(ptr-&gt;buffer));
          scicos_free(*(ptr-&gt;sum));
          scicos_free(*(block-&gt;work));
          fclose(f);
          set_block_error(-16);
          return;
      }
  
      fprintf(f, &quot;ptr-&gt;buffer: %ld \n&quot;, ptr-&gt;buffer);
      fprintf(f, &quot;ptr-&gt;sum: %ld \n&quot;, ptr-&gt;sum);
      fprintf(f, &quot;ptr-&gt;index: %ld \n&quot;, ptr-&gt;index);
  
      int i;
      for(i = 0; i &lt; N; i++)
      {
          ptr-&gt;buffer[i] = 0;
      }
      *(ptr-&gt;sum) = 0;
      *(ptr-&gt;index) = 0;
  
      for(i = 0; i &lt; N; i++)
      {
          fprintf(f, &quot;buffer[%d]: %f\n&quot;, i, ptr-&gt;buffer[i]);
      }
  
      fprintf(f, &quot;*(ptr-&gt;sum): %f\n&quot;, *(ptr-&gt;sum));
      fprintf(f, &quot;*(ptr-&gt;index): %d\n&quot;, *(ptr-&gt;index));
          
    }
    else if(flag == 1) 
    {
      fprintf(f, &quot;In: %f \n&quot;, U[0]);  
  
      ptr-&gt;buffer[0] = U[0];
  
      int i;
      for(i = 0; i &lt; N; i++)
      {
          fprintf(f, &quot;buffer[%d]: %f\n&quot;, i, ptr-&gt;buffer[i]);
      }
      
    } 
    else  if (flag == 5) 
    {
      /* ending */
      scicos_free(*(block-&gt;work));
      fclose(f);
    }
  }
  </code></pre>
  <p>I also attach the scicos_block structure for the completeness:</p>
  <pre><code>typedef struct
  {
      int nevprt;
      voidg funpt;
      int type;
      int scsptr;
      int nz;
      double *z;
      int noz;
      int *ozsz;
      int *oztyp;
      void **ozptr;
      int nx;
      double *x;
      double *xd;
      double *res;
      int *xprop;
      int nin;
      int *insz;
      void **inptr;
      int nout;
      int *outsz;
      void **outptr;
      int nevout;
      double *evout;
      int nrpar;
      double *rpar;
      int nipar;
      int *ipar;
      int nopar;
      int *oparsz;
      int *opartyp;
      void **oparptr;
      int ng;
      double *g;
      int ztyp;
      int *jroot;
      char *label;
      void **work;
      int nmode;
      int *mode;
      char *uid;
  }scicos_block;
  </code></pre>
  <p>I am able to successfully compile the code but in case I run the simulation containing
  my CBLOCK4 block I always receive following error message:</p>
  <p><em>Warning !!!
  Scilab has found a critical error (EXCEPTION_ACCESS_VIOLATION)
  with &quot;scicosim&quot; function.
  Save your data and restart Scilab.</em></p>
  <p>and the simulation does not either start. I have found that the simulation starts working in case I remove following lines of code inside the CBLOCK4 block:</p>
  <pre><code>ptr-&gt;buffer[0] = U[0];
  
  int i;
  for(i = 0; i &lt; N; i++)
  {
      fprintf(f, &quot;buffer[%d]: %f\n&quot;, i, ptr-&gt;buffer[i]);
  }
  </code></pre>
  <p>I have doubts that I have some bug inside my C code (probably regarding the memory
  allocation) but I can't find it. Does anybody have experience with the Scilab/Xcos
  CBLOCK4 usage?</p>