前提・実現したいこと
C言語、開発ツールはVisual Studioです。
calloc関数を使ってエラーなく最後まで実行完了したいです。
発生している問題・エラーメッセージ
何回目かのcallocによるメモリの確保中に例外がスローされているようだ、というところまではわかりましたが、エラーの詳しい内容がでません。
ConsoleApp.exe によってブレークポイントが発生しました。
とだけ出てしまいます。
該当のソースコード
C
1#include<stdio.h> 2#include<stdlib.h> 3#define MEMBER 20 4 5typedef struct{ 6 int year, month, day; 7}DATE; 8 9typedef struct person{ 10 char name[20]; 11 DATE birthday; 12 struct person *next; 13}PERSON; 14 15//PERSON *NewData(int ele); 16PERSON *NewData(int ele); 17void Insert(PERSON*, PERSON*); 18void Dele(PERSON*, PERSON); 19PERSON *conect(PERSON*, int, PERSON*); 20PERSON *MAXB(PERSON *, PERSON *); 21void print_list(PERSON *); 22 23 24int main(void) 25{ 26 FILE *fp; 27 char filename[256] = { 0 }; 28 char buff[256] = { 0 }; 29 int buf = 0; 30 PERSON *p_buf; 31 printf("読み込む誕生日リストのファイルを入力してください:"); 32 scanf("%s", filename); 33 if ((fp = fopen(filename, "r")) != NULL) 34 { 35 while (fgets(buff, 256, fp) != NULL) 36 { 37 buf++; 38 } 39 fclose(fp); 40 if ((p_buf = NewData(buf)) != NULL) 41 { 42 buf = 0; 43 if ((fp = fopen(filename, "r")) != NULL) 44 { 45 while (fgets(buff, 256, fp) != NULL) 46 { 47 sscanf(buff, "%[^,],%d,%d,%d,\n", &((p_buf + buf)->name), &((p_buf + buf)->birthday.year), &((p_buf + buf)->birthday.month), &((p_buf + buf)->birthday.day)); 48 buf++; 49 } 50 fclose(fp); 51 for (size_t i = 0; i < buf; i++) 52 { 53 //printf("%s,%d,%d,%d\n", (p_buf + i)->name, (p_buf + i)->birthday.year, (p_buf + i)->birthday.month, (p_buf + i)->birthday.day); 54 } 55 conect(p_buf, MEMBER, NULL); 56 //print_list(conect(p_buf, MEMBER, NULL)); 57 } 58 } 59 60 } 61 62 return 0; 63} 64 65PERSON *NewData(int ele) 66{ 67 PERSON *buf; 68 buf = calloc(ele, sizeof(PERSON)); 69 if (buf != NULL) 70 { 71 return buf; 72 } 73 else 74 { 75 return NULL; 76 } 77} 78 79void Insert(PERSON *before, PERSON *target) 80{ 81 PERSON *buf; 82 buf = NewData(1); 83 if (buf != NULL) 84 { 85 if (before != NULL) 86 { 87 buf = before->next; 88 before->next = target; 89 target->next = buf; 90 } 91 } 92} 93 94void Dele(PERSON *target, PERSON First) 95{ 96 PERSON *buf; 97 buf = &First; 98 while (target != buf->next) 99 { 100 buf = buf->next; 101 } 102 if (buf != NULL) 103 { 104 buf->next = target->next; 105 free(target); 106 } 107} 108PERSON *conect(PERSON *target, int times, PERSON *p_ANC) 109{ 110 111 PERSON *p_start; 112 p_start = NewData(1); 113 if (p_start != NULL) 114 { 115 for (size_t i = 0; i < times; i++) 116 { 117 p_start = MAXB(p_start, target + i); 118 } 119 p_start->next = p_ANC; 120 if (times != 1) 121 { 122 PERSON *Next_T; 123 Next_T = NewData(times - 1); 124 for (size_t i = 0; i < times; i++) 125 { 126 if ((target + i) != p_start) 127 { 128 *(Next_T + i) = *(target + i); 129 } 130 } 131 return conect(Next_T, times - 1, p_start); 132 } 133 else 134 { 135 return p_start; 136 } 137 } 138} 139PERSON *MAXB(PERSON *A, PERSON *B) 140{ 141 if (A->birthday.year < B->birthday.year) 142 { 143 return B; 144 } 145 else if (A->birthday.year > B->birthday.year) 146 { 147 return A; 148 } 149 else 150 { 151 if (A->birthday.month < B->birthday.month) 152 { 153 return B; 154 } 155 else if (A->birthday.month > B->birthday.month) 156 { 157 return A; 158 } 159 else 160 { 161 if (A->birthday.day < B->birthday.day) 162 { 163 return B; 164 } 165 else if (A->birthday.day > B->birthday.day) 166 { 167 return A; 168 } 169 else 170 { 171 return A; 172 } 173 } 174 } 175} 176 177void print_list(PERSON *start) 178{ 179 if (start != NULL) 180 { 181 printf("%s,%d,%d,%d\n", start->name, start->birthday.year, start->birthday.month, start->birthday.day); 182 print_list(start->next); 183 } 184}
試したこと
デバッグをして一工程ずつたどってみてcallocのなかで_heap_alloc のところで止まってしまうことは確認しました。
補足情報(FW/ツールのバージョンなど)
Visual Studio 2013,2017で試しました。

回答6件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/08 06:32
2019/01/08 06:36
2019/01/08 09:57