neu zu C Programmierer hier. Dieser Code läuft wunderbar, wenn das Training klein ausfällt, aber er stürzt ab, wenn die Reichweite zunimmt. Im Moment bricht es bei 1289 ab, aber ich möchte es bis 7892 verwenden. Der Fehler ist unten; Ich bin mir ziemlich sicher, dass es sich um etwas im Malloc-Call handelt, aber ich bin mir nicht sicher, wie ich es beheben soll. Vielen Dank.Malloc Fehlallokation?
TrainSet* get_train_set(float range){
TrainSet* t;
printf("%f\n", range);
t = malloc(range * sizeof *t);
FILE *fp;
char line[300]; /* 300 is an arbitrary length to read in lines from the text file*/
int count = 0;
fp = fopen("Data/main_training_set.txt", "r");
if(fp == NULL){
perror("Error opening file");
return NULL;
}
while (fgets(line, 300, fp)){
// printf("%d\n", count);
int s = strlen((line)) - 3;
char* quote = (char*)malloc(sizeof(char) * s);
for(int i = 0; i <= s; i++){
quote[i] = line[i];
}
int label = atoi(&line[s]);
t->sentences[count] = quote;
t->labels[count] = label;
count ++;
}
//fclose(fp);
return t;
}
Der Fehler:
buddhism: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Aborted
Und hier ist die h-Datei für Triebzug
typedef struct TrainSet {
char* sentences[7892];
int labels[7892];
} TrainSet;
TrainSet* get_train_set(float range);
#endif /* READ_H*/*
Eingangsdaten sind dies im Wesentlichen, eine verstümmelte Art Satz mit einer ganzen Zahl am Ende jeden Linie.
first four lectures taking place weekendand subject weekends talk five aggregates 1
relationship us thats simply want tobe happy dont want suffer 1
heard words songrecently said "i dont mind dying living scaresme 1
us know difficulties life trying somehowto minimize 1
Sieht aus wie Sie irgendwo eine Speicherbeschädigung haben. Es ist schwer für uns zu helfen, es sei denn, ein [mcve] wird bereitgestellt. Aber für Anfänger 'für (int i = 0; i <= s; i ++)' Ursachen außerhalb des gebundenen Zugriffs. Es muss "für (int i = 0; i
kaylumSehen Sie sich mit [getline] (http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) anstelle von fgets um. Keine willkürliche Speicherzuordnung notwendig. – Schwern
@Swwern Diese Funktion ist möglicherweise auf Nicht-POSIX-Systemen nicht verfügbar –