質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

1回答

388閲覧

メモリの解放エラーの解決法

退会済みユーザー

退会済みユーザー

総合スコア0

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

0クリップ

投稿2021/06/03 12:58

前提・実現したいこと

あるスペクトルの値を出力するプログラムを作成しています。
それぞれの工程でデバッグ作業を行っていて、求めた値は指定したファイルに書き込む仕組みです。

発生している問題・エラーメッセージ

プログラムの最後に確保した領域を解放しているのですが、./a.outで実行すると以下のようなエラー文が出てしまいます。ファイルに書き込むまではできているので、メモリの解放の部分に問題があると考えているのですが、解決法が分かりません。

... complete step3: calculate value of BGS complete file write *** Error in `./a.out': double free or corruption (out): 0x0000000001589090 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x7c619)[0x7f41da288619] ./a.out[0x401317] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7f41da22dc05] ./a.out[0x400919] ======= Memory map: ======== 00400000-00402000 r-xp 00000000 00:2a 9452379 /exports/home/j418828/centos7/grad_study/a.out 00601000-00602000 r--p 00001000 00:2a 9452379 /exports/home/j418828/centos7/grad_study/a.out ... ... ... ... Abort (core dumped)

該当のソースコード

c

1#include<stdio.h> 2#include<stdlib.h> 3#include<math.h> 4 5/* デバックモード */ 6#define DEBUG 7 8/* 関数原型宣言 */ 9double unform_rand(); //一様な乱数の生成 10double normal_rand(double average, double sigma); //世紀乱数の表示 11double calc_strain(double separate); //ひずみの計算 12 13 14/* 一様な乱数の生成 */ 15double uniform_rand() { 16 return ((double)rand()+1.0)/((double)RAND_MAX+2.0); 17} 18 19/* 正規乱数の生成 */ 20double normal_rand(double ave, double sigma) { 21 return ave + sigma * sqrt(-2.0*log(uniform_rand())) * sin(2.0*M_PI*uniform_rand()); 22} 23 24/* ひずみの計算 */ 25double calc_strain(double separate) { 26 if (separate < 4) 27 return 0.0; // 11.080[GHz] 28 else if (separate >= 4 && separate < 7) 29 return 0.000076923076928; // 11.084[GHz] 30 else if (separate >= 7 && separate < 10) 31 return 0.0001538461538; // 11.088[GHz] 32 else if (separate >= 10 && separate < 13) 33 return 0.000375; // 11.0995[GHz] 34 else if (separate >= 13 && separate < 16) 35 return -0.0001923076923; // 11.070[GHz] 36 else if (separate >= 16) 37 return 0.0005769230769; // 11.110[GHz] 38 else { 39 printf("separate not define. \n"); 40 return -1; 41 } 42} 43 44int main(void) { 45 46 /* ファイル変数 */ 47 FILE *fp; //ファイルポインタ 48 char *writefile = "obs_bgs.csv"; //ファイル名 49 50 /* step1 観測条件の設定 */ 51#ifdef DEBUG 52 printf("start step1: set observation conditions \n"); 53#endif 54 55 /* s1-1 観測位置条件の設定 */ 56 57 double z_1_pos_start = 1; //観測開始位置 z_1 [m] 58 double z_end_pos_end = 1.1; //観測終了位置 z_end [m] 59 double del_z_pos_space = 0.1; //観測位置間隔 δz [m] 60 int I_pos_max_num; //観測位置の総数 I [個] 61 int i_pos_count; //観測位置の番号 i [番目] 62 double *z_i_pos; //観測位置 z_i [m] 63 double z_pos_space_leng = 1; //BGSの観測区間 Δz [m] 64 65 /* 観測位置の総数の計算 I = [(z_end - z_1)/ δz] + 1 */ 66 I_pos_max_num = ceil((z_end_pos_end - z_1_pos_start) / del_z_pos_space) +1; 67#ifdef DEBUG 68 printf("complete calc I = %d\n", I_pos_max_num); 69#endif 70 71 /* 観測位置の領域確保 */ 72 z_i_pos = (double *)malloc(sizeof(double)*I_pos_max_num+1); 73 if (z_i_pos == NULL) { 74 exit(1); 75 } 76#ifdef DEBUG 77 printf("complete malloc z_i\n"); 78#endif 79 80 /* s1-2 観測周波数条件の設定 */ 81 82 double nu_1_freq_start = 11.03; //観測開始周波数 ν_1 [GHz] 83 double nu_end_freq_end = 11.031; //観測終了周波数 ν_end [GHz] 84 double del_nu_freq_space = 0.001; //観測周波数間隔 δν [GHz] 85 int J_freq_max_num; //観測周波数の総数 J [個] 86 int j_freq_count; //観測周波数の番号 j [番目] 87 double *nu_j_freq; //観測周波数 ν_j [GHz] 88 89 /* 観測周波数の総数の計算 J = [(ν_end - ν_1) / δν] + 1 */ 90 J_freq_max_num = ceil((nu_end_freq_end - nu_1_freq_start) / del_nu_freq_space) + 1; 91#ifdef DEBUG 92 printf("complete calc J = %d\n", J_freq_max_num); 93#endif 94 95 /* 観測周波数の領域確保 */ 96 nu_j_freq = (double *)malloc(sizeof(double)*J_freq_max_num+1); 97 if (nu_j_freq == NULL) { 98 exit(1); 99 } 100#ifdef DEBUG 101 printf("complete malloc ν_j\n"); 102#endif 103 104 /* step1-3 観測位置と観測周波数の計算 */ 105 /* 観測位置の計算 z_i = z_1 + δz(i - 1) */ 106 for (i_pos_count=1; i_pos_count<=I_pos_max_num; i_pos_count++) { 107 z_i_pos[i_pos_count] = z_1_pos_start + del_z_pos_space*(i_pos_count - 1); 108 } 109#ifdef DEBUG 110 printf("complete calc z_i\n"); 111#endif 112 113 /* 観測周波数の計算 ν_j = ν_1 +δν(j - 1) */ 114 for (j_freq_count=1; j_freq_count<=J_freq_max_num; j_freq_count++) { 115 nu_j_freq[j_freq_count] = nu_1_freq_start + del_nu_freq_space*(j_freq_count-1); 116 } 117#ifdef DEBUG 118 printf("complete calc ν_j\n"); 119#endif 120 121#ifdef DEBUG 122 printf("complete step1: set observation conditions \n"); 123#endif 124 125 /*step2 計算条件の設定 */ 126 127#ifdef DEBUG 128 printf("start step2: set calculate conditions \n"); 129#endif 130 131 /* s2-1 計算条件の設定 */ 132 133 double w_fwhm = 0.025; //半値全幅 w [GHz] 134 double nu_B_0_center_freq = 11.08; //ひずみがない場合の中心周波数 ν_B(0) [GHz] 135 double C_prop_const = 52; //ひずみと周波数の関係を表す比例定数 C [GHz] 136 double h_max_value; //単位長さ当たりのBGS値の最大値 h [a.u.] 137 double ep_z_ik_strain; //ひずみの分布 ε(z_i,k) 138 double g_nu_j_z_ik_bgs; //単位長さ当たりのBGS値 g(ν_j, z_i,k) [a.u.] 139 140 /* 最大値の計算 h = 1/Δz */ 141 h_max_value = 1 / z_pos_space_leng; 142 143#ifdef DEBUG 144 printf("complete step2: set calculate conditions \n"); 145#endif 146 147 /* step3 BGS値の計算 */ 148 149#ifdef DEBUG 150 printf("start step3: calculate value of BGS \n"); 151#endif 152 153 /* s3-1 区分求積に必要な変数の設定 */ 154 155 int K_integral_max_num = 5; //区分求積の分割数 K[個] 156 int k_integral_count; //区分求積の番号 k[番目] 157 double z_ik_integral_pos; //分割されたk番目の位置 z_i,k[m] 158 double nu_B_ep_z_ik_center_freq; //分割位置z_i,kでの最大値を与える周波数 ν_b(ε(z_i,k)) [GHz] 159 double **G_nu_j_z_i_bgs_obs; //観測されるBGS値 G(ν_j, z_i) [a.u.] 160 161 /* 観測されるGBS値の領域確保 */ 162 G_nu_j_z_i_bgs_obs = (double **)malloc(sizeof(double *)*J_freq_max_num); 163 if (G_nu_j_z_i_bgs_obs == NULL) { 164 exit(1); 165 } 166 for(j_freq_count=1; j_freq_count<=J_freq_max_num; j_freq_count++) { 167 G_nu_j_z_i_bgs_obs[j_freq_count] = (double *)calloc(I_pos_max_num+1, sizeof(double)); 168 if (G_nu_j_z_i_bgs_obs[j_freq_count] == NULL) { 169 exit(1); 170 } 171 } 172#ifdef DEBUG 173 printf("complete malloc G(ν_j, z_i)\n"); 174#endif 175 176 for(j_freq_count=1; j_freq_count<=J_freq_max_num; j_freq_count++) { 177 for(i_pos_count=1; i_pos_count<=I_pos_max_num; i_pos_count++) { 178 179 /* 観測されるBGS値 G(ν_j, z_i)の初期化 */ 180 G_nu_j_z_i_bgs_obs[j_freq_count][i_pos_count] = 0.0; 181#ifdef DEBUG 182 printf("complete G(ν_j, z_i) = 0\n"); 183#endif 184 185 for (k_integral_count=1; k_integral_count<=K_integral_max_num; k_integral_count++) { 186 187 /* 分割位置z_i,kの割り当て z_i,k = (z_i - Δz/2) + (k-1)Δz/K + Δz/2K */ 188 z_ik_integral_pos = (z_i_pos[i_pos_count] - z_pos_space_leng/2) + (z_pos_space_leng/(2*K_integral_max_num)) + ((k_integral_count-1)*z_pos_space_leng/K_integral_max_num); 189#ifdef DEBUG 190 printf("complete calc z_i,k\n"); 191#endif 192 193 /* 分割位置 z_i,k における歪み ε(z_i,k) の設定 */ 194 ep_z_ik_strain = calc_strain(z_ik_integral_pos); 195#ifdef DEBUG 196 printf("complete calc ε(z_i,k)\n"); 197#endif 198 199 /* 歪み ε(z_i,k) に対応する中心周波数 ν_B(ε(z_i,k)) の計算 ν_B(ε(z_i,k)) = ν_B(0) + C*ε(z_i,k) */ 200 nu_B_ep_z_ik_center_freq = nu_B_0_center_freq + C_prop_const*ep_z_ik_strain; 201#ifdef DEBUG 202 printf("complete calc ν_B(ε(z_i,k))\n"); 203#endif 204 205 /* s3-2 区分求積 */ 206 207 /* 単位長さ当たりのBGS値 g(ν_j, z_i,k) の計算 g(ν_j, z_i,k) = h(w/2)^2 / ((ν_j - ν_B(ε(z_i,k)))^2 + (w/2)^2) */ 208 g_nu_j_z_ik_bgs = h_max_value*(w_fwhm/2)*(w_fwhm/2) / ((nu_j_freq[j_freq_count]-nu_B_ep_z_ik_center_freq)*(nu_j_freq[j_freq_count]-nu_B_ep_z_ik_center_freq) + (w_fwhm/2)*(w_fwhm/2)); 209#ifdef DEBUG 210 printf("complete calc g(ν_j, z_i,k)\n"); 211#endif 212 213 /* 単位長さ当たりのBGS値 g(ν_j, z_i,k) のk=1〜Kまでの合計 G(ν_j, z_i) = G(ν_j, z_i) + g(ν_j, z_i,k) */ 214 G_nu_j_z_i_bgs_obs[j_freq_count][i_pos_count] += g_nu_j_z_ik_bgs; 215 } 216 217 /* 区分求積 G(ν_j, z_i) = G(ν_j, z_i)*(Δz/K) */ 218 G_nu_j_z_i_bgs_obs[j_freq_count][i_pos_count] *= z_pos_space_leng/K_integral_max_num; 219#ifdef DEBUG 220 printf("complete calc G(ν_j, z_i)\n"); 221#endif 222 } 223 } 224 225#ifdef DEBUG 226 printf("complete step3: calculate value of BGS \n"); 227#endif 228 229 /* ファイルオープン */ 230 fp = fopen(writefile, "w"); 231 if (fp == NULL) { 232 printf("error : can't open file\n"); 233 exit(1); 234 } 235 236 /* ファイル書き込み */ 237 for(j_freq_count=1; j_freq_count<=J_freq_max_num; j_freq_count++) { 238 for(i_pos_count=1; i_pos_count<=I_pos_max_num; i_pos_count++) { 239 fprintf(fp, "%.10f, ", G_nu_j_z_i_bgs_obs[j_freq_count][i_pos_count]); 240 } 241 fprintf(fp, "\n"); 242 } 243#ifdef DEBUG 244 printf("complete file write\n"); 245#endif 246 247 /* ファイルクローズ */ 248 fclose(fp); 249 250 /* 領域の解放 */ 251 free(z_i_pos); 252 z_i_pos = NULL; 253 free(nu_j_freq); 254 nu_j_freq = NULL; 255 for (j_freq_count=1; j_freq_count<=J_freq_max_num; j_freq_count++) { 256 free(G_nu_j_z_i_bgs_obs[j_freq_count]); 257 G_nu_j_z_i_bgs_obs[j_freq_count] = NULL; 258 } 259 free(G_nu_j_z_i_bgs_obs); 260 G_nu_j_z_i_bgs_obs = NULL; 261#ifdef DEBUG 262 printf("complete free\n"); 263#endif 264 265 return 0; 266} 267

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

正直なところソース追いきれて無いですがパッと見ではallocサイズが足りてないように見えます。
添字を1始まりに拘ってるようなので以下のように1個分ずつ多く確保しないとバッファオーバーランしませんか?

C

1z_i_pos = (double*)malloc(sizeof(double) * (I_pos_max_num + 1)); 2nu_j_freq = (double*)malloc(sizeof(double) * (J_freq_max_num + 1)); 3G_nu_j_z_i_bgs_obs = (double**)malloc(sizeof(double*) * (J_freq_max_num + 1)); 4

投稿2021/06/03 13:35

編集2021/06/03 13:44
neconekocat

総合スコア443

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/06/03 13:42

教えていただいた通り訂正したらできました!ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問