-1

I'm working on TSP problem using ant colony optimization in C. I think that I implemented correctly, but my program doesn't work. I know that I had problems with memory in my code, because when I run my program the console write "core dumped." I used a gdb to find errors and when I wrote debugging commands I saw something like that:

Program received signal SIGSEGV, Segmentation fault.
0x08049691 in odleglosc (miastoA=1668848019, miastoB=1062464331, graf=0x804d440) at trail.c:34
34      return graf[miastoA][miastoB];
(gdb) bt
0  0x08049691 in odleglosc (miastoA=1668848019, miastoB=1062464331, graf=0x804d440) at trail.c:34
1  0x080496f1 in Length (sciezka3=0x804f5a0, graf=0x804d440, size=22) at trail.c:42
2  0x0804919d in updateFeromon (feromon=0x804f380, mrow=0x804e8a0, graf=0x804d440, parowanie=0, 10000000000000001, iloscFeromonu=2, miasta=22, mrowki=4) at pheromon.c:40
3  0x080489e3 in main (argc=1, argv=0xbffff0f4) at main.c:55

UpdateFeromon from pheromon.c calls a Length function from trail.c and next this function calls odleglosc also from trail.c. The range of miastoA and miasto is from 0 to 21. I dont know which function change my values ( miastoA and miastoB) and how to fix it.

This is a link to my code http://speedy.sh/FTTZe/mrowki.tar

josliber
  • 43,891
  • 12
  • 98
  • 133
  • ok so miasta is a number of cities, mrowki is a number of ants. In the first loop i wanna allocate a memory to a 2d array. ( mrow[ number of ants] [ number of cities]. In the second loop i initialize a random trail for my ants. sth like that 1 ( ant) : (trail) 2, 5 ,7, 1, 20, ..... 22 elements Sorry for mistake with number of cities, yes it is 22 not 21 ;) Now i think that i made a mistake with allocate a memory for mrow in first loop. So how can i allocate a memory for a int **mrow? – user3464535 Apr 18 '14 at 22:58
  • sth like that ? int **mrow; mrow=malloc(numberOfAnts * sizeof(int*)); for(i=0; i – user3464535 Apr 18 '14 at 22:58
  • Pls see the updated answer below. – simurg Apr 19 '14 at 08:42

1 Answers1

0

This is a complicated code and it's hard to understand a complicated code written in a language I don't speak. Anyway, I tried to inspect the code and saw one place that looks suspicious. In function inicjalizujMrowki() you seem to overwrite mrow[] values in the second for loop below:

for(i=0; i<miasta; i++)
{
    mrow[i]=(int*)malloc(sizeof(int)*miasta);
}
for(j=0; j<mrowki;j++)
{
    int start =0;
    mrow[j]= losowaSciezka(start, miasta);
}

As a general result of my investigation, miasta (whatever it is) is 22 (not 21 as you indicated in the question) and the problem is definitely an incorrect allocation, thus the reason that the code above rang the bells.

Edit: After your explanation above, I see that mrow was indeed the problem. I'm writing this on my phone so I can't give you code directly but I can show you the way. Think of mrow as a pointer to an array of pointers to arrays. So the initialization should go like this (pseudo code):

int **mrow;
**mrow = malloc(num_rows);
for (0 to num_rows-1) {
    *mrow = malloc(num_columns);
}

Edit: I'm way too much involved in this. I think it is better to, in stead of looking at the code and try to find the problem, invest some time in learning how to use a debugger. That'll help a lot.

simurg
  • 1,208
  • 7
  • 14
  • Please do not edit my answer :) Below is what you wrote: – simurg Apr 21 '14 at 06:50
  • Ok i wrote sth like that: int ** inicjalizujMrowki( int mrowki, int miasta){ int i; int j; int **mrow; mrow=malloc(sizeof(int*)*mrowki); for(i=0; i – simurg Apr 21 '14 at 06:52
  • ants 1 [ 1, 12, 17, 3, ...... , 21, 18] length = 2040,00 ants 2 ...... but now is shows sth like that: ants 1 [ 0, 0, 0, 0 ...., 0, 0] ... I dont know why when i changed the implementation of allocate a memory in function inicjalizujMrowki shows only 0, but earlier shows random trail. btw a function losowaSciezka returns a array of random ant trail. – simurg Apr 21 '14 at 06:52