Home>

I want to initialize the array, pheromone.

Error message
at initial_pheromone
Segment error
Applicable source code
file as.c
#include<stdio.h>
#include<math.h>
#include<limits.h>
#include 
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include "sub.h"
double pheromone_0;
 double alpha;
 double beta;
 double rho;
 double q_0;
 int n;
 int nant;
void erace (void) {
 alpha = 1.0;
 beta = 1.0;// Heuristic information priority
 rho = 0.5;// pheromone evaporation rate
 q_0 = 0;
 pheromone_0 = 1.0;
printf ("pheromone_0;% f \ n", pheromone_0);
 initial_pheromone (pheromone_0);
printf ("% f \ n", pheromone [0] [0]);
}
int main (int argc, char * argv []) {
 int i;
 n = 51;
 nant = 10;// number of ants

 start_program ();
  erace ();



 for (i = 0;i<n;i ++) {
   free (pheromone [i]);
   free (total [i]);
 }
 free (total);
 free (pheromone);
   return (0);
}
sub.c
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<limits.h>
#include "tesb.h"
ant_struct * ant;
ant_struct * bestant;
double ** pheromone;
double ** total;
void initial_pheromone (double initial_trail) // Initialize pheromone
{
    long int i, j;
    for (i = 0;i<n;i ++) {
    for (j = 0;j<= i;j ++) {
      pheromone [i] [j] = initial_trail;
        pheromone [j] [i] = pheromone [i] [j];
    }
    }
}

void start_program (void)
{
  int i, j;
   if ((ant = malloc (sizeof (ant_struct) * nant + sizeof (ant_struct *) * nant)) == NULL) {
   printf ("out of memory, exit");
  exit (1);
 }
  for (i = 0;i<nant;i ++) {
    ant [i] .tour = calloc (n + 1, sizeof (int));
    ant [i] .visited = calloc (n, sizeof (char));
  }
  if ((bestant = malloc (sizeof (ant_struct))) == NULL) {
   printf ("out of memory, exit");
  exit (1);
 }
  bestant->tour = calloc (n + 1, sizeof (int));
  bestant->visited = calloc (n, sizeof (char));
 pheromone = malloc (sizeof (double *) * n);
  for (i = 0;i<n;i ++) {
    pheromone [i] = malloc (sizeof (double *) * n);
  }
  total = malloc (sizeof (double *) * n);
  for (i = 0;i<n;i ++) {
    total [i] = malloc (sizeof (double *) * n);
  }
}
file sub.h
typedef struct {
  int * tour;
  char * visited;
  int tour_length;
} ant_struct;

extern ant_struct * ant;
extern ant_struct * bestant;
extern double ** pheromone;
extern int nant;

extern double rho;// pheromone evaporation rate
extern double alpha;// Importance of pheromone
extern double beta;// Importance of heuristics
extern double q_0;// probability to choose the best path
int * best_ant_score;
void compute_total (void);

void initial_pheromone (double initial_trail);
void start_program (void);

extern double pheromone_0;
extern int n;
extern double ** pheromone;
extern double ** total;

I thought the array was a problem and tried to display the array, but the array memory was well prepared.
I tried to fix it, but I still get an error. I will try tomorrow again without using malloc

  • Answer # 1

    I compiled your source and I get the following error:
    (CentOs7.5 gcc version 4.8.5)
    gcc -o as as.c sub.c
    as.c: 29: 1: Error: Unknown type name ‘iint’
    iint main (int argc, char * argv []) {
    ^
    as.c: in function ‘main’:
    as.c: 39: 15: Error: ‘optimal’ is not declared (first use within this function)
    scanf ("% \ n",&optimal);
    ^
    as.c: 39: 15: Remarks: Undeclared identifiers are only reported once in each function that occurs
    as.c: 56: 9: Error: ‘total’ is not declared (first use within this function)
    free (total [i]);

    Can you provide a source that does not cause errors?

  • Answer # 2

    Is the value of n as expected atpheromone = malloc (sizeof (int *) * n);in start_program ()? ?


    By the way, is it necessary to allocate memory with malloc ()?
    First of all, why not go through the process of making it with a fixed number of elements?
    For example,

    #define N 30
    #define N_ANT 10
    double pheromone [N] [N];
    ant_struct ant [N_ANT];


    Like shape.

  • Answer # 3

      

    double ** pheromone;
      pheromone = malloc (sizeof (int *) * n);

    Why is it secured with the size of the int pointer even though it is double?
    That's not good in the first place