Nick_Iommi Δημοσ. 7 Δεκεμβρίου 2013 Δημοσ. 7 Δεκεμβρίου 2013 Τρεχω το παρακατω προγραμμα σε Ubuntu x64 12.04 ltsκαι μου πεταει την προβληματικη εξοδο που παραθετω κατω απο τον κωδικα:ενω σε ενα αλλο pc τρεχει κανονικα μεχρι και το 9999.Ξερει κανεις που οφειλεται αυτο?? #include <stdio.h> #include <stdlib.h> int main(void){ int i,t; t=0; int* pointer; pointer=malloc(10*sizeof(int)); //δεσμευω μνημη για αποθήκευση 10 ακεραίων οπου καθε ακέραιος ειναι 4 Bytes //Εγω ομως θελω να αποθηκευσω 10000 ακεραίους for(i=0;i<10000;i++){ t++; pointer[i]=i; printf("to proto byte gia to periexomeno %d tha apothikeytei sti thesi mnimis %p\n",pointer[i],&pointer[i]); if(t==10){ //καθε 10η επαναληψη επαναδεσμευω την μνημη που απαιτειται για την αποθηκευση της επόμενης 10αδας ακεραίων pointer=realloc(pointer,10*sizeof(int)); t=0; } } } to proto byte gia to periexomeno 0 tha apothikeytei sti thesi mnimis 0x20ee010 to proto byte gia to periexomeno 1 tha apothikeytei sti thesi mnimis 0x20ee014 to proto byte gia to periexomeno 2 tha apothikeytei sti thesi mnimis 0x20ee018 to proto byte gia to periexomeno 3 tha apothikeytei sti thesi mnimis 0x20ee01c to proto byte gia to periexomeno 4 tha apothikeytei sti thesi mnimis 0x20ee020 to proto byte gia to periexomeno 5 tha apothikeytei sti thesi mnimis 0x20ee024 to proto byte gia to periexomeno 6 tha apothikeytei sti thesi mnimis 0x20ee028 to proto byte gia to periexomeno 7 tha apothikeytei sti thesi mnimis 0x20ee02c to proto byte gia to periexomeno 8 tha apothikeytei sti thesi mnimis 0x20ee030 to proto byte gia to periexomeno 9 tha apothikeytei sti thesi mnimis 0x20ee034 to proto byte gia to periexomeno 10 tha apothikeytei sti thesi mnimis 0x20ee038 to proto byte gia to periexomeno 11 tha apothikeytei sti thesi mnimis 0x20ee03c to proto byte gia to periexomeno 12 tha apothikeytei sti thesi mnimis 0x20ee040 to proto byte gia to periexomeno 13 tha apothikeytei sti thesi mnimis 0x20ee044 to proto byte gia to periexomeno 14 tha apothikeytei sti thesi mnimis 0x20ee048 to proto byte gia to periexomeno 15 tha apothikeytei sti thesi mnimis 0x20ee04c to proto byte gia to periexomeno 16 tha apothikeytei sti thesi mnimis 0x20ee050 to proto byte gia to periexomeno 17 tha apothikeytei sti thesi mnimis 0x20ee054 to proto byte gia to periexomeno 18 tha apothikeytei sti thesi mnimis 0x20ee058 to proto byte gia to periexomeno 19 tha apothikeytei sti thesi mnimis 0x20ee05c *** glibc detected *** ./malloc: realloc(): invalid next size: 0x00000000020ee010 ***
Directx Δημοσ. 7 Δεκεμβρίου 2013 Δημοσ. 7 Δεκεμβρίου 2013 Το realloc σου δεσμεύει πάντα 10 int για αυτό παρουσιάζεται το πρόβλημα, η realloc δεν θυμάται το ήδη δεσμευμένο μέγεθος του pointer int* οπότε θα πρέπει να της το υπενθυμίσεις εσύ. Έτσι όταν για παράδειγμα το t = 10 θα πρέπει να κάνεις realloc για 20 int, δηλαδή 10 που δέσμευσες αρχικά με την malloc (και τα οποία θα αντιγράψει η realloc στο νέο pointer των 20 int) + άλλα 10 νέα int για την αποθήκευση των επόμενων i που θα παράγεις κλπ.
Nick_Iommi Δημοσ. 7 Δεκεμβρίου 2013 Μέλος Δημοσ. 7 Δεκεμβρίου 2013 Σε ευχαριστω πολυ!ειχα την εντυπωση οτι η realloc ειναι πιο εξυπνη !
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα