ここからCプログラマーには新しいです。このコードはトレーニングが小さく設定されたときに美しく実行されますが、範囲が拡大するとクラッシュするようになります。今は1289で壊れていますが、私は7892まで使いたいです。エラーは以下の通りです。私はそれがmalloc呼び出しの何かに関係しているとはかなり確信していますが、私はそれを修正する方法がわかりません。ありがとう。Mallocの割り当てが間違っていますか?
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;
}
エラー:ここ
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
とは
typedef struct TrainSet {
char* sentences[7892];
int labels[7892];
} TrainSet;
TrainSet* get_train_set(float range);
#endif /* READ_H*/*
入力データは、基本的にこれで編成のための時間のファイルは、すべての最後に整数と文の文字化け一種ですライン。
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
あなたはどこかでメモリの破損があるように見えます。 [mcve]が提供されない限り、私たちが助けてくれるのは難しいです。しかし、最初の 'for(int i = 0; i <= s; i ++)'は、境界外アクセスを引き起こします。 'for(int i = 0; i
kaylumfgetsの代わりに[getline](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html)を見てください。任意のメモリ割り当ては必要ありません。 – Schwern
@Schwernその機能はPOSIX以外のシステムでは利用できない可能性があります –