problem about pointer how to correct it 
Author Message
 problem about pointer how to correct it

I  have  problem  about  memory access with  pointer  please  help me  how
to correct  it

#include <math.h>      // for  Expo    function
#include <stdlib.h>
#include <stdio.h>

/***************************************************************************
*********/

typedef struct in_mem {
  double width;         /* Input membership function width (1/2 of triangle
base).  */
  double *center;       /* Center of each input membership function.
*/
  double *dom;          /* Degree of membership for each membership
function.       */

Quote:
} IN_MEM;

typedef struct out_mem {

  double **outmf;         /* Center of each output membership function.
*/

Quote:
} OUT_MEM;

typedef struct fuz_sys {
  IN_MEM  *emem;        /* Groups all fuzzy system parameters in a single
variable. */
  IN_MEM  *edotmem;
  OUT_MEM *outmem;

Quote:
} FUZ_SYS;

/***************************************************************************
*********/

/* Function Prototypes: */

void fuzzy_init(FUZ_SYS *fuzzy_system);
void fuzzy_free(FUZ_SYS *fuzzy_system);
double fuzzy_control(double e, double edot, FUZ_SYS *fuzzy_system);
void fuzzyify(double u, IN_MEM *mem);
double gaussianmf(double u, double w, double c);
void match(const IN_MEM *emem, const IN_MEM *edotmem, int *pos ,int *count);
double inf_defuzz(IN_MEM *emem, IN_MEM *edotmem, OUT_MEM *outmem, int *pos ,
int *count);

/***************************************************************************
*********/

void fuzzy_init(FUZ_SYS *fuzzy_system) {

/* Define the input and output membership functions. */

 // int i,j;

  /* Allocate memory for membership functions. */
  if (!(fuzzy_system->emem = (IN_MEM *) malloc(sizeof(IN_MEM)))) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  if (!(fuzzy_system->edotmem = (IN_MEM *) malloc(sizeof(IN_MEM)))) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  if (!(fuzzy_system->outmem = (OUT_MEM *) malloc(sizeof(OUT_MEM)))) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  if (!(fuzzy_system->emem->center = (double *) malloc(3*sizeof(double)))) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  if (!(fuzzy_system->emem->dom = (double *) malloc(3*sizeof(double)))) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  if (!(fuzzy_system->edotmem->center = (double *)
malloc(3*sizeof(double)))) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  if (!(fuzzy_system->edotmem->dom = (double *) malloc(3*sizeof(double)))) {
    printf("Error allocating memory.\n");
    exit(1);
  }
  if (!(fuzzy_system->outmem->outmf = (double **)
malloc(18*sizeof(double)))) {
    printf("Error allocating memory.\n");
    exit(1);
  }

  /* Initialize for mf. */
   fuzzy_system->emem->width = 0.3398;  /* Width defined to be 1/2 of
gaussianmf base. */
   fuzzy_system->edotmem->width = 0.3398;

  //for (i=0; i<3; i++) {
    fuzzy_system->emem->center[0]    = -1 ;
    fuzzy_system->edotmem->center[0] = -1;
    fuzzy_system->emem->center[1]    =  0;
    fuzzy_system->edotmem->center[1] =  0;
    fuzzy_system->emem->center[2]    =  1;
    fuzzy_system->edotmem->center[2] =  1;

      fuzzy_system->outmem->outmf[0][0] = 0;
      fuzzy_system->outmem->outmf[0][1] = 1;
      fuzzy_system->outmem->outmf[0][2] = 2;
      fuzzy_system->outmem->outmf[1][0] = 3;
      fuzzy_system->outmem->outmf[1][1] = 4;
      fuzzy_system->outmem->outmf[1][2] = 5;
      fuzzy_system->outmem->outmf[2][0] = 6;
      fuzzy_system->outmem->outmf[2][1] = 7;
      fuzzy_system->outmem->outmf[2][2] = 8;

Quote:
}

/***************************************************************************
*********/
void fuzzy_free(FUZ_SYS *fuzzy_system) {

/* Free memory allocated in fuzzy_init(). */

  free(fuzzy_system->emem->center);
  free(fuzzy_system->emem->dom);
  free(fuzzy_system->edotmem->center);
  free(fuzzy_system->edotmem->dom);
  free(fuzzy_system->outmem->outmf);
  free(fuzzy_system->emem);
  free(fuzzy_system->edotmem);
  free(fuzzy_system->outmem);

Quote:
}

/***************************************************************************
*********/

double fuzzy_control(double e, double edot, FUZ_SYS *fuzzy_system) {

/* Given crisp inputs e and edot, determine the crisp output u. */

  int pos[2];
  int count[2];

  fuzzyify(e, fuzzy_system->emem);
  fuzzyify(edot, fuzzy_system->edotmem);
  match(fuzzy_system->emem, fuzzy_system->edotmem, pos, count);
  return inf_defuzz(fuzzy_system->emem, fuzzy_system->edotmem,
fuzzy_system->outmem, pos , count);

Quote:
}

/***************************************************************************
*********/

void fuzzyify(double u, IN_MEM *mem) {

/* Fuzzify the input u by determining the degree of membership for each
membership
   function in mem. Assumes 5 membership functions, with first and last
membership
   functions leftall and rightall respectively.  Other membership functions
are
   triangular. */

  int i;

    for (i=0; i<4; i++)
    mem->dom[i] = gaussianmf(u, mem->width, mem->center[i]);

Quote:
}

/***************************************************************************
****/

double gaussianmf(double u, double w, double c)

/* Determine degree of membership for a gaussian membership function
   NOTE:  u is input, c is mem. fun. center, and w is mem. fun. width. */

{

    if (u >= 1 )
      u = 1;

    else if (u <= -1)
       u = -1;

    else
     u = u;

    return(exp(-pow(((u - c)/w),2.0)));

Quote:
}

/***************************************************************************
*********/

void match(const IN_MEM *emem, const IN_MEM *edotmem, int *pos ,int *count)
{

/* For each universe of discourse, determine the index of the first
membership function
   with a non-zero degree (i.e. match the rules to the current inputs to
find which rules
   are on).  These indices are used to determine which four rules to
evaluate.  (NOTE:
   A 2 input sytem with no more than 50% overlap for input membership
functions only
   requires the evaluation of at most 4 rules.) */

  int i;
  count[0] = 1;
  count[1] = 1;

  for (i=0; i<5; i++) {
    if(emem->dom[i] != 0) {
      pos[0] = i;
      break;

    }

  }
    i++ ;
    if(emem->dom[i] != 0) {
      count[0]++;
      }

  for (i=0; i<5; i++) {
    if(edotmem->dom[i] != 0) {
      pos[1] = i;
      break;
    }
  }

  i++ ;
    if(emem->dom[i] != 0) {
      count[1]++;
      }

Quote:
}

/***************************************************************************
*********/

double inf_defuzz(IN_MEM *emem, IN_MEM *edotmem, OUT_MEM *outmem, int *pos ,
int *count)
{

/* We use the degrees of membership found in the function match() to form
the implied
   fuzzy sets. The correct output membership function for each rule is
determined by
   adding (and saturating) a shifted version of the input membership
function indices
   (this implements the typical pattern of linguistic-numeric indices in the
body of
   the table of rules).  In this way we compute the rule-base at every step,
rather
   than storing the rule-base in a table.  Defuzzification is also performed
using
   the center-of-gravity method.  A crisp output is returned. */

  double w[2][2];

  int j,k;
  double den, num;

    // tem  = 0;
     num  = 0;
    den  = 0;
    // mul  = 0 ;

     for (k = pos[0]; k < ( pos[0]+ (count[0]-1)); k++)
       {

        for (j = pos[1];k < (pos[1]+(count[1]-1));j++)
         {

            w[k][j]  = min((emem->dom[k]),(edotmem->dom[j]));

              //tem =  w[k][j];
             // den  =  den + tem;

              den  =+ w[k][j] ;
             // mul  = w[k][j]* outmem->center[k][j];

               num  =+  (w[k][j]*outmem-> outmf[k][j]);

          }

        }

        return(num/den);

Quote:
}

/***************************************************************************
*********/

// #if 0  /* Set to zero to call fuzzy_control() from another program. */

/* Test I/O behavior of fuzzy controller. */

void main(void)
 {

/* Test for input given below.  Output is -6.818182. */

  double e, edot, u;
  FUZ_SYS fuzzy_system;

  /* Crips inputs. */
  e = 0;
  edot = 0.5;

  fuzzy_init(&fuzzy_system);
  u = fuzzy_control(e,edot,&fuzzy_system);
  fuzzy_free(&fuzzy_system);
  printf("e = %f, edot = %f, u = %f\n",e,edot,u);

Quote:
}

// #endif

/***************************************************************************
*********/
--



Thu, 06 Oct 2005 01:24:27 GMT  
 problem about pointer how to correct it
Quote:

> I  have  problem  about  memory access with  pointer  please  help me  how
> to correct  it

[300+ lines of C source code deleted.

How do you expect us to help if you don't tell us what the problem is?

You should also reduce your program to the smallest one that exhibits
the problem.  (In doing so, you just might figure it out yourself.)

(BTW, main() returns int, not void.  See the FAQ.)

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
--



Sat, 08 Oct 2005 00:34:21 GMT  
 problem about pointer how to correct it

Quote:
>I  have  problem  about  memory access with  pointer  please  help me  how
>to correct  it

>#include <math.h>      // for  Expo    function
>#include <stdlib.h>
>#include <stdio.h>

>/***************************************************************************
>*********/

>typedef struct in_mem {

snip 300+ lines of code

Don't you think it would help if you told us what kind problem you are
having, maybe with a sample of the input?

Just to start you off

        main should return an int

        don't cast the return from malloc

        use different error messages so you know which part of the code is
having problems.

        outmf is a double**.  You initialize it with a call to malloc (but
you use the wrong size).  However, you never initialize outmf[0],
outmf[1], and outmf[2] (all of which have type double*) to point
anywhere.  And then you dereference these un-iniitalized pointers with
expressions like outmf[0][0].  This invokes undefined behavior.

<<Remove the del for email>>
--



Sat, 08 Oct 2005 00:34:25 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. the correct way to think of and use pointers to pointers

2. Pointer to struct - correct my ideas

3. Corrected: Question concerning ** pointers

4. correct way to cast pointers with offsets?

5. correct code for pointer subtraction (LONG reply)

6. correct posting of problem with deteching tabs

7. Correct mod/rem (was: Problem with % (remainder) operator

8. Problem: Ascii conversion for TCB4.5 - correct.c

9. Problem: Ascii conversion for TCB4.5 - correct.c [1/1]

10. please correct error (SafeArray Problem)

11. Static problem: Corrected question

12. design problem using c++ : casting to correct template.

 

 
Powered by phpBB® Forum Software