Home>
I want to initialize the array, pheromone.
Error messageat 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
-
Answer # 2
Is the value of n as expected at
pheromone = 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
Related articles
- c - sometimes heapsort does not sort well
- about initialization of c language array
- c language compilation does not pass
- c - m5btnbwasreleasefor () does not work
- gcc does not enable ssp
- c heap sort does not sort first
- file reading does not work in c language
- scanf () does not work
- c - toupper does not work well
- output does not work correctly
- javascript - jquerycookiejs does not work
- i want to store colors in a c string array
- errordocument 500 of htaccess does not work well
- javascript for statement does not work
- html - width does not work
- javascript - addeventlistener does not work
- go - printf does not break on \r\n or \n
- the contents of the array will change
- php - get request does not run with jquery
- php - laravel app does not work on docker
Related questions
- creation of a simple grade processing system using c language
- creating a two-dimensional array using malloc in c language
- c language method of substituting values for each row in a two-dimensional array
- i compared the same string with the strcmp function, but it was determined to be a different string
- i want to get the average of the numerical values under a specific condition
- shift any number of characters in an array
- [c] assignment of structures to arrays
- php - i want to create an array by extracting a multidimensional array with regular expressions
- the c language that handled the array came out with strange output and i do not know the cause * there are a lot of comments for
- python - extracting required elements from the dictionary by conditional branch differs from the expected result
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?