Evgenios1 Δημοσ. 31 Αυγούστου 2010 Δημοσ. 31 Αυγούστου 2010 1)Τα ants σου ειναι struct bla_bla οχι int 2)Εφοσον κανεις allocate στο ants μεσου συναρτησης και by ref, πρεπει να περασεις τον δεικτη by ref πχ > #include <stdio.h> #include <stdlib.h> typedef struct {} bla; void foo( bla **s) { *s = (bla*) malloc(sizeof(bla) * 5); } int main(void) { bla *s; foo(&s); return 0; }
Luffy-san Δημοσ. 31 Αυγούστου 2010 Δημοσ. 31 Αυγούστου 2010 Επισης μεσα στη for στο τελος , βαζεις ants.visited[r] = 1 , με το visited να ειναι δεικτης σε int , κατι που σκαει , γιατι δεν εχεις δεσμευσει μνημη για το visited[r] εδιτ: Αν δεν εχεις κανει λαθος στο copy - paste , δεν πρεπει να κανει compile
Dr.Fuzzy Δημοσ. 31 Αυγούστου 2010 Μέλος Δημοσ. 31 Αυγούστου 2010 Έκανα τις αλλαγές ως εξής, >void ants_place(struct TSP_data *problem, single_ant **ants_place) { int i; int r; srand(time(NULL)); /* randomness depends on time */ *ants_place = (single_ant *) malloc(problem->n * sizeof(single_ant)); /* allocate placement memory (n single ant ant structs) dynamically */ (*ants_place)->tour = (int *) malloc(problem-> n * sizeof(int)); (*ants_place)->visited = (int *) malloc(problem-> n * sizeof(int)); /* place ants randomly */ for (i = 0; i < problem->n; i++) { r = rand() % (problem->n - 1) + 1; /* since path is from nest to food use nodes-1 to place ants to all nodes except the target (food) node */ ants_place[i]->tour[0] = r; ants_place[i]->visited[r] = 1; } δυστυχώς πάλι σκάει.
Luffy-san Δημοσ. 31 Αυγούστου 2010 Δημοσ. 31 Αυγούστου 2010 Φυσικα και σκαει.Τωρα ειναι πιο λαθος απο πριν.Στην ουσια εφτιαξες ενα πινακα απο δεικτες σε single_ant και οχι για τα πεδια του single_ant.Ετσι με καθε προσβαση σε πεδιο του single_ant φερνει seg.fault edit > void ants_place(struct TSP_data *problem, single_ant *ants_place) { int i; int r; srand(time(NULL)); /* randomness depends on time */ ants_place = (single_ant *) malloc(problem->n * sizeof(single_ant)); /* allocate placement memory (n single ant ant structs) dynamically */ for (i = 0; i < problem->n; i++) { ants_place[i]->tour = (int *) malloc(problem-> n * sizeof(int)); ants_place[i]->visited = (int *) malloc(problem-> n * sizeof(int)); } /* place ants randomly */ for (i = 0; i < problem->n; i++) { r = rand() % (problem->n - 1) + 1; /* since path is from nest to food use nodes-1 to place ants to all nodes except the target (food) node */ ants_place[i]->tour[0] = r; ants_place[i]->visited[r] = 1; } ετσι μου φαινεται πιο σωστο , αλλα δεν μπορω να το τσεκαρω....σορρυ
Evgenios1 Δημοσ. 31 Αυγούστου 2010 Δημοσ. 31 Αυγούστου 2010 Έκανα τις αλλαγές ως εξής, >void ants_place(struct TSP_data *problem, single_ant **ants_place) { int i; int r; srand(time(NULL)); /* randomness depends on time */ *ants_place = (single_ant *) malloc(problem->n * sizeof(single_ant)); /* allocate placement memory (n single ant ant structs) dynamically */ (*ants_place)->tour = (int *) malloc(problem-> n * sizeof(int)); (*ants_place)->visited = (int *) malloc(problem-> n * sizeof(int)); /* place ants randomly */ for (i = 0; i < problem->n; i++) { r = rand() % (problem->n - 1) + 1; /* since path is from nest to food use nodes-1 to place ants to all nodes except the target (food) node */ (*ants_place[i])->tour[0] = r;//<------------------------------------------ (*ants_place[i])->visited[r] = 1;//<-------------------------------------------- } δυστυχώς πάλι σκάει. Φυσικα και σκαει.Τωρα ειναι πιο λαθος απο πριν.Στην ουσια εφτιαξες ενα πινακα απο δεικτες σε single_ant και οχι για τα πεδια του single_ant. pointer != array
Dr.Fuzzy Δημοσ. 1 Σεπτεμβρίου 2010 Μέλος Δημοσ. 1 Σεπτεμβρίου 2010 @Evgenios1 pointer != array, εντάξει ρε το ξέρω αυτό! @Luffy-san, Evgenios1 Τα ants_place->tour[0] είναι προφανές ότι πρέπει να είναι ants_place.tour[0] ACO.c:268: error: invalid type argument of ‘->’ (have ‘single_ant’) Anyways, το function το έχω ως εξής τώρα, δεν σκάει, αλλά παίρνω άσχετες τιμές! >void ants_place(struct TSP_data *problem, single_ant *ants_place) { int i; int r; srand(time(NULL)); /* randomness depends on time */ ants_place = (single_ant *) malloc(problem->n * sizeof(single_ant)); /* allocate placement memory (n int elements) dynamically */ for (i = 0; i < problem->n; i++) { ants_place[i].tour = (int *) malloc(problem-> n * sizeof(int)); ants_place[i].visited = (int *) malloc(problem-> n * sizeof(int)); } /* place ants randomly */ for (i = 0; i < problem->n; i++) { r = rand() % (problem->n - 1) + 1; /* since path is from nest to food use nodes-1 to place ants to all nodes except the target (food) node */ ants_place[i].tour[0] = r; ants_place[i].visited[r] = 1; } } και στη main >init_ants_place(&problem, ant); for (i=0; i<problem.n; i++) { printf("ith ant placement: %d\n", ant[i]); // debug } μπέρδεμα...
Evgenios1 Δημοσ. 1 Σεπτεμβρίου 2010 Δημοσ. 1 Σεπτεμβρίου 2010 >void ants_place(TSP_data *problem, single_ant **ants_place) { int i; int r; srand(time(NULL)); /* randomness depends on time */ *ants_place = (single_ant *) malloc(problem->n * sizeof(single_ant)); /* allocate placement memory (n single ant ant structs) dynamically */ for(i=0;i<problem->n;i++) { (*ants_place)[i].tour = (int *) malloc(problem-> n * sizeof(int)); (*ants_place)[i].visited = (int *) malloc(problem-> n * sizeof(int)); } /* place ants randomly */ for (i = 0; i < problem->n; i++) { r = rand() % (problem->n - 1) + 1; (*ants_place)[i].tour[0] = r; (*ants_place)[i].visited[r] = 1; } } Ετσι θα δουλεψουν οι allocetors, αλλα νομιζω οτι εχεις καποιο λοθος με τη λογικη του προγραμματος.
Luffy-san Δημοσ. 1 Σεπτεμβρίου 2010 Δημοσ. 1 Σεπτεμβρίου 2010 @evgenios Καλα ναι , αλλο δεικτες και αλλο πινακες,Απλως το ειπα για να εχει στο νου του πως εχει δεσμευσει ( σχηματικα τουλαχιστον... ) μνημη.Εμενα με βολευει αφανταστα... @dr.fuzzy αφου το βαζεις ants_place τοτε θα κανεις προσπελαση με τελεστη ( . ) , αλλιως με (->) μονο αν το ειχες ants_place .Παλι μπερδευτικες με το (->). edit: Μου φαινεται οτι και του ευγενιου ειναι λαθος η δεσμευση... > void ants_place(TSP_data *problem, single_ant **ants_place) { int i; int r; srand(time(NULL)); /* randomness depends on time */ int j; *ants_place = (single_ant **) malloc(problem->n * sizeof(single_ant)); /* allocate placement memory (n single ant ant structs) dynamically */ for(i=0;i<problem->n;i++) { ants_place[i] = (single_ant *) malloc(problem-> n * sizeof(single_ant )); } for( i = 0 ; i < problem-> n ; i++){ for( j = 0 ; j < problem-> n; j++ ){ ants_place[i][j].tour = (int*) malloc( problem-> n * sizeof(int) ); ants_place[i][j].visited = (int*) malloc( problem-> n * sizeof(int) ); } } /* place ants randomly */ for (i = 0; i < problem->n; i++) for( j = 0; j< problem->n ; j++ ){ r = rand() % (problem->n - 1) + 1; ants_place[i][j].tour[0] = r; ants_place[i][j].visited[r] = 1; } } } Δοκιμασε αυτο dr.fuzzy edit_2: Μεσα στη main δεν χρειαζεται να περνας τη διευθυνση του problem ( &problem).Σκετο problem.Αλλιως αν θες να περασεις με &problem πρεπει να βαλεις στη συναρτηση σαν ορισμα το **problem kai oxi *problem.Ακομα στη main κανεις με διαφορετικο τροπο προσπελαση στα ant απο οτι τα εχεις δεσμευσει.Ετσι γραψε λαθος το απο πανω και βαλτο ετσι > void ants_place(TSP_data *problem, single_ant *ants_place) { int i; int r; srand(time(NULL)); /* randomness depends on time */ ants_place = (single_ant *) malloc(problem->n * sizeof(single_ant)); /* allocate placement memory (n single ant ant structs) dynamically */ for( i = 0 ; i < problem-> n ; i++){ ants_place[i].tour = (int*) malloc( problem-> n * sizeof(int) ); ants_place[i].visited = (int*) malloc( problem-> n * sizeof(int) ); } /* place ants randomly */ for (i = 0; i < problem->n; i++){ r = rand() % (problem->n - 1) + 1; ants_place[i].tour[0] = r; ants_place[i].visited[r] = 1; } } και στη main αλλαξε το &problem με problem.τσεκαρε και τα συντακτικα που μπορει να εχω.
Dr.Fuzzy Δημοσ. 1 Σεπτεμβρίου 2010 Μέλος Δημοσ. 1 Σεπτεμβρίου 2010 @Luffy-san στη main έχω, struct TSP_data problem; οπότε πρέπει να είναι &problem το όρισμα και όχι problem. Αν το κάνω, struct TSP_data *problem; τότε θα γίνει problem. το function είναι: void ants_place(struct TSP_data *problem, single_ant *ants_place) @Evgenios1 Το έχω δοκιμάσει έτσι, τσου, παίρνω ότι να είναι τιμές. Τι πρόβλημα στη λογική; Με το ίδιο function π.χ., void ants_place(struct TSP_data *problem, int *ants), αν επιστρέφω το ants σε ένα int *ants μέσα στη main, δουλεύει μια χαρά! Το μπλέξιμο γίνεται με το structure.
Luffy-san Δημοσ. 1 Σεπτεμβρίου 2010 Δημοσ. 1 Σεπτεμβρίου 2010 sorry...ειχα την εντυπωση οτι το problem ηταν δεικτης
Evgenios1 Δημοσ. 1 Σεπτεμβρίου 2010 Δημοσ. 1 Σεπτεμβρίου 2010 @Evgenios1 Το έχω δοκιμάσει έτσι, τσου, παίρνω ότι να είναι τιμές. Τι πρόβλημα στη λογική; Με το ίδιο function π.χ., void ants_place(struct TSP_data *problem, int *ants), αν επιστρέφω το ants σε ένα int *ants μέσα στη main, δουλεύει μια χαρά! Το μπλέξιμο γίνεται με το structure. Για ονομα του maker!! Κανε ενα typedef, αλλιως σιγουρεψου οτι οπου εχεις tag απο struct να εχεις και το keyword struct μπροστα απο το tag, παντου, και στα casting.
Luffy-san Δημοσ. 1 Σεπτεμβρίου 2010 Δημοσ. 1 Σεπτεμβρίου 2010 Χωρις typedef εχεις τα struct ? Βαλε typedef γιατι οτιδηποτε λεμε ειναι λαθος
Dr.Fuzzy Δημοσ. 1 Σεπτεμβρίου 2010 Μέλος Δημοσ. 1 Σεπτεμβρίου 2010 χωρίς! Όχι typedef...δεν μου αρέσει καθόλου!
Evgenios1 Δημοσ. 1 Σεπτεμβρίου 2010 Δημοσ. 1 Σεπτεμβρίου 2010 χωρίς! Όχι typedef...δεν μου αρέσει καθόλου! τοτε >void ants_place([color="Red"]struct [/color]TSP_data *problem, [color="Red"]struct [/color]single_ant **ants_place) { int i; int r; srand(time(NULL)); /* randomness depends on time */ *ants_place = ([color="red"]struct [/color]single_ant *) malloc(problem->n * sizeof([color="red"]struct [/color]single_ant)); /* allocate placement memory (n single ant ant structs) dynamically */ for(i=0;i<problem->n;i++) { (*ants_place)[i].tour = (int *) malloc(problem-> n * sizeof(int)); (*ants_place)[i].visited = (int *) malloc(problem-> n * sizeof(int)); } /* place ants randomly */ for (i = 0; i < problem->n; i++) { r = rand() % (problem->n - 1) + 1; (*ants_place)[i].tour[0] = r; (*ants_place)[i].visited[r] = 1; } }
Dr.Fuzzy Δημοσ. 1 Σεπτεμβρίου 2010 Μέλος Δημοσ. 1 Σεπτεμβρίου 2010 έτσι το έχω τώρα, αλλά μια από τα ίδια! Κάπου αλλού είναι το πρόβλημα, και λογικά κάτι μ@λ@κι@ γίνεται με το structure, αλλά δεν έχω ιδέα τι.
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.