Newsgroup: it.comp.lang.c
Titolo: questione di piattaforma?

Non riesco a capire perche' il programma si pianta (va in loop...) nella funzione start_cicle.
Qualcuno ha suggerimenti?
 /* quantistic structur property simulation */
#include <stdio.h>
#include <stdlib.h>
 /* global structs */
struct cell {
   unsigned char cell_value;
   struct cell *next_cell;
};
 /* function prototips */
void clrscr(void);
struct cell *make_grid(unsigned long cells_number);
struct cell *cell_jump(struct cell *grid_addr, unsigned long cell_number);
void clean_grid(struct cell *grid_addr, char mode);
void move_quant(struct cell *grid_addr, unsigned long cells_number);
void start_cicle(struct cell *grid_addr, unsigned long loop_num, unsigned long c
ells_number);
void show_stats(struct cell *grid_addr, unsigned long loop_num);
 /* main part */
int main(int argc, char *argv[])
{
    /* treating total numer as a string permitting value over 2^32 */
   unsigned long total_cells, loops;
   struct cell *grid_address;
   unsigned req_mode;
 
    /* blanking stdout */
   clrscr();
    /* short presentation & prefers request */
   puts("Programma di simulazione quantistica\n\n");
    /* number of cells */
   printf("o Numero celle della scacchiera: ");
   scanf("%ld", &total_cells);
    /* field mode */
   do {
      printf("o Modalitą di riempimento: ");
      scanf("%d", &req_mode);
      if ( (req_mode != 0) && (req_mode != 1) && (req_mode != 2) )
          {
             puts("\nErrore. modalitą valide: 0 1 2");
          }
      else { break; };
   } while (1);
    /* loop number (treating as string). 20 digits sufficient? ;) */
   printf("o Numero di cicli: ");
   scanf("%ld", &loops);
 
    /* making grid */
   grid_address = make_grid(total_cells);
 
    /* filling grid by right mode */
   clean_grid(grid_address, req_mode);
 
    /* starting core */
   start_cicle(grid_address, loops, total_cells);
 
    /* getting infos on simulation session */
   show_stats(grid_address, loops);
 
   return 0;
}
void clrscr(void)
{
   unsigned char count_x, count_y;
 
   for (count_x = 1; count_x <= 24; count_x++)
     {
        for (count_y = 1; count_y <= 80; count_y++)
          {
             printf(" ");
          };
        puts("");
     };
}
 
struct cell *make_grid(unsigned long cells_number)
{
   struct cell *head, *cells;
   unsigned long count = 0;
 
   cells = ( head = malloc(sizeof(struct cell)) );
 
   for (; count <= (cells_number - 1); count++)
     {
        cells = cells->next_cell = malloc(sizeof(struct cell));
        cells->next_cell = NULL;
     };
 
   return head;
}
struct cell *cell_jump(struct cell *grid_addr, unsigned long cell_number)
{
   unsigned long count = 0;
 
   for (; count <= cell_number; count++)
     {
        if (grid_addr->next_cell != NULL)
          {
           grid_addr = grid_addr->next_cell;
          }
        else
          {
             return NULL;
             break;
          };
     };
 
   return grid_addr;
}
 
void clean_grid(struct cell *grid_addr, char mode)
{
   unsigned long count_cell = 0;
   unsigned char end_list = 0, odd = 0;
   unsigned char poss[3] = { 0, 1, 1 };
   struct cell *temp_cell = grid_addr;
 
   if ((mode > 2) || (mode < 0))
     { mode = 0; };
 
   while (!end_list) {
      temp_cell = cell_jump(grid_addr, count_cell++);
      if (temp_cell == NULL)
        { break; }
      switch (mode)
        {
          /* 1st mode: grid full => 1,1,1,1... */
         case 0: {
            temp_cell->cell_value = 1;
            break;
         };
          /* 2nd mode: half grid full & half void => 1,0,1,0... */
         case 1: {
            odd = !odd;
            temp_cell->cell_value = (unsigned char)odd;
            break;
         };
          /* 3rd mode: half grid full & half half-full => 1,1,0,1,1,0... */
         case 2: {
            if (odd == 2) { odd = 0; };
            temp_cell->cell_value = poss[odd++];
            break;
         };
        };
   };
}
void move_quant(struct cell *grid_addr, unsigned long cells_number)
{
   unsigned long ran_cell;
   struct cell *temp_cell;
 
   srand(ran_cell);
    /* loop: search a new random cell until it's not empty (cell_value != 0) */
   do { 
      ran_cell = ( (rand() * 2) % cells_number );
      temp_cell = cell_jump(grid_addr, ran_cell);
   } while (temp_cell->cell_value);
 
    /* decrease 1 unit from "source" cell & move it to a new random cell */
   --(temp_cell->cell_value);
 
   ran_cell = ( (rand() * 2) % cells_number );
   temp_cell = cell_jump(grid_addr, ran_cell);
   ++(temp_cell->cell_value);
}
void start_cicle(struct cell *grid_addr, unsigned long loop_num, unsigned long c
ells_number)
{
   unsigned long counter = 0;
 
   while (++counter <= loop_num) {
      move_quant(grid_addr, cells_number);
   }
}
void show_stats(struct cell *grid_addr, unsigned long loop_num)
{
   unsigned char stats[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
   unsigned long count = 0;
   struct cell *temp;
 
   do {
      temp = cell_jump(grid_addr, count++);
      if (temp->cell_value >= 9)
        {
           ++(stats[9]);
        }
      else {
         ++(stats[temp->cell_value]);
      };
   } while (temp != NULL);
 
   puts("\n\n\n");
   printf("Valori conseguiti:\n===================");
   printf("0)\t%d\t\t\t\t1)%d", stats[0], stats[1]);
   printf("2)\t%d\t\t\t\t3)%d", stats[2], stats[3]);
   printf("4)\t%d\t\t\t\t5)%d", stats[4], stats[5]);
   printf("6)\t%d\t\t\t\t7)%d", stats[6], stats[7]);
   printf("8)\t%d\t\t\t\t9 o oltre)%d", stats[8], stats[9]);
}

snip
Ciao Mij
nel pezzo di programma preso dalla funzione move_quant tu fai:
    /* loop: search a new random cell until it's not empty (cell_value != 0)
*/
   do
      ran_cell = ( (rand() * 2) % cells_number );
      temp_cell = cell_jump(grid_addr, ran_cell);
   } while (temp_cell->cell_value);
Ora sono perplesso, poiche´ temp_cell e´ un puntatore a struct cell che puo´
anche avere valore NULL tornatogli dalla funzione cell_jump.
Quindi non puoi testare temp_cell->cell_value se questo puntatore e´ NULL,
altrimenti il programma crasha.
Ciao
Mauro