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

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

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

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

Q&A

解決済

2回答

2475閲覧

C言語 メモリ領域に関するエラーの原因が分かりかねています。

makkasa

総合スコア11

C

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

0グッド

1クリップ

投稿2020/12/04 14:25

編集2020/12/04 14:40

現在、「現行ディレクトリ内のファイル・ディレクトリを更新日順に表示するlsコマンドのようなプログラム」を作成途中のプログラムでエラーが発生しておリます。
自分なりにデバッグを行い、sec_mem関数内でエラーが起きているようなのですが、それ以降はどうしてもわからず、お力を貸して下さい。。

●エラー内容
プログラム実行時、たまに以下のようなエラーが発生する。
「incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6」
※大まかには上記のエラーが発生せずにプログラムが正常に終了します。

#ifndef SELF_LS_H # define SELF_LS_H # include <stdio.h> # include <stdlib.h> # include <unistd.h> # include <dirent.h> # include <sys/stat.h> # include <sys/types.h> # include <string.h> int my_strlen(char *s); int is_hidden(char *s); int count(void); char *my_strdup(char *s1); void swap_str(char **s1, char **s2, struct stat *n1, struct stat *n2); void sort(char **ps, struct stat *pn, int count); char **sec_mem(int count, struct stat **pn); #endif
#include "self_ls.h" int my_strlen(char *s) { int i; i = 0; while (s[i] != '\0') i++; return (i); } int is_hidden(char *s) { if (s[0] == '.') return (1); return (0); } int count(void) { DIR *dir; struct dirent *d_cont; int n; n = 0; if ((dir = opendir("./")) == NULL) return (-1); while ((d_cont = readdir(dir)) != NULL) { if (is_hidden(d_cont->d_name) == 1) continue; n++; } closedir(dir); return (n); } char *my_strdup(char *s1) { char *p; int n; int i; i = 0; n = my_strlen(s1); if (!(p = (char *)malloc(sizeof(char) * (n + 1)))) return (NULL); while (s1[i] != '\0') { p[i] = s1[i]; i++; } p[i] = '\0'; return (p); } void swap_str(char **s1, char **s2, struct stat *n1, struct stat *n2) { char *temps; struct stat tempn; temps = *s1; *s1 = *s2; *s2 = temps; tempn = *n1; *n1 = *n2; *n2 = *n1; } void sort(char **ps, struct stat *pn, int count) { int i; int j; i = 0; while (i < count - 1) { j = i + 1; while (j < count) { if (pn[i].st_mtime > pn[j].st_mtime) swap_str(&ps[i], &ps[j], &pn[i], &pn[j]); j++; } i++; } i = 0; while (i < count) { j = my_strlen(ps[i]); write(1, ps[i], j); write(1, "\n", 1); i++; } } char **sec_mem(int count, struct stat **pn) { DIR *dir; struct dirent *d_cont; char **ps; int i; i = 0; if (!(ps = (char **)malloc(sizeof(char *) * (count + 1)))) return (NULL); if (!(dir = opendir("./"))) return (NULL); while (i < count) { d_cont = readdir(dir); if (is_hidden(d_cont->d_name) != 1) { ps[i] = my_strdup(d_cont->d_name); lstat(d_cont->d_name, pn[i]); i++; } } ps[i] = NULL; closedir(dir); return (ps); } int main(void) { int n; int i; char **ps; struct stat *pn; if ((n = count()) == -1) { perror("error"); return (-1); } if (!(pn = malloc(sizeof(stat) * (n + 1)))) return (-1); if (!(ps = sec_mem(n, &pn))) return (-1); ps[n] = NULL; sort(ps, pn, n); i = 0; while (i < n) { free(ps[i]); i++; } free(pn); free(ps); return (0); }

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

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

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

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

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

guest

回答2

0

main関数内でのmalloc確保のstat構造体の宣言が誤っているようでした。
正) malloc(sizeof(struct stat) * (n + 1));

投稿2020/12/05 02:33

makkasa

総合スコア11

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

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

0

ベストアンサー

sec_mem()の引数、struct stat **pnが、main()のpnと整合していません。

pn[0]はmalloc()で確保されたアドレスを指しますが、
pn[1]以降は、未使用領域を参照し、不定な値を、構造体のポイントとして扱っています。
lstat()への引数として、書くのなら、&((*pn)(i))でしょうか?

まずは、原因確定の為、pn[i]の値を、実際に確認してみてください。

確定したら、修正は、以下の通り。
修正後、念のため、&pn[i]の値も、確認されるのをお勧めします。

C

1char **sec_mem(int count, struct stat *pn) 2{ 3  (省略) 4 while (i < count) 5 { 6 d_cont = readdir(dir); 7 if (is_hidden(d_cont->d_name) != 1) 8 { 9 ps[i] = my_strdup(d_cont->d_name); 10 lstat(d_cont->d_name, &pn[i]); 11 i++; 12 } 13 } 14  (省略) 15} 16 17int main(void) 18{ 19  (省略) 20 if (!(ps = sec_mem(n, pn))) 21 return (-1); 22  (省略) 23}

なお、環境がない為、未検証ですので、ご了承ください。

投稿2020/12/04 16:59

YT0014

総合スコア1708

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

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

makkasa

2020/12/05 02:32 編集

ご回答頂き、ありがとうございます。 コード参考にさせて頂きます。 私自身ポインタとメモリの理解が足りていないため、勉強します。 また、別の1つの原因として、main関数内でのmalloc確保のstat構造体の宣言が誤っているようでした。 正) malloc(sizeof(struct stat) * (n + 1));
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問