세그멘테이션 오류 해결방법이 뭘까요?

ysh0950의 이미지

int main()
{
int i;
int ret;
struct dirent **filelist;
ret = scandir(curdir, &filelist,0,alphasort);
bkgd(COLOR_PAIR(1));
WINDOW *top_bar, *left_bar, *right_bar, *bottom_bar;
int key;
init_scr();

top_bar = subwin(stdscr,1,80,0,0);
left_bar = subwin(stdscr,20,39,1,0);
right_bar = subwin(stdscr,20,39,1,39);
bottom_bar = subwin(stdscr,1,80,21,0);

box(left_bar, ACS_VLINE, ACS_HLINE);
box(right_bar, ACS_VLINE, ACS_HLINE);
top(top_bar);
bottom(bottom_bar);

/*
for(i=0; i<12; i++)
{
wprintw(left_bar,"%s", filelist[i]->d_name);
}
*/
while(1)
{
key = getch();
if(key == 'q')
{
if(check_quit()==1)
{
break;

}
}
}

endwin();
}

저쪽 주석부분 소스를 입력하면 세그멘테이션 오류라고 뜨네요
뭐가 문제일까요?

pchero의 이미지

ret = scandir(curdir, &filelist,0,alphasort);

함수 수행이후, 값이 얼마가 되었던 간에 주석부분

for(i=0; i<12; i++)
{
    wprintw(left_bar,"%s", filelist[i]->d_name);
}

를 수행하네요.

만약, curdir 안에 파일 갯수가 12개 이하일 경우, 버퍼 오버플로우가 나면서 세그멘테이션 오류가 발생하는 것 같습니다.
해당 for 구문을 파일의 갯수에 맞춰서 수행하도록 수정하면 될 듯 싶습니다.

---------------------------------
제일 왼쪽이 저입니다 :)

ysh0950의 이미지

답변 감사합니다!
그런데 말씀하신대로 파일 갯수에 맞춰봤지만 계속 세그멘테이션 오류가 뜨네요...

pchero의 이미지

scandir 에서 사용한 curdir 에 문제가 있는 것 같습니다.
curdir 에 정확한 디렉토리 위치가 들어갔는지 확인하시면 될 것 같습니다.

그래도 결과에 이상이 있다면 먼저 아래 예제를 돌려보시고, 원하시는 결과가 나오도록 조금씩 변경하면서 시도해보세요.

       #define _SVID_SOURCE
       /* print files in current directory in reverse order */
       #include <dirent.h>
 
       int
       main(void)
       {
           struct dirent **namelist;
           int n;
 
           n = scandir(".", &namelist, NULL, alphasort);
           if (n < 0)
               perror("scandir");
           else {
               while (n--) {
                   printf("%s\n", namelist[n]->d_name);
                   free(namelist[n]);
               }
               free(namelist);
           }
       }

---------------------------------
제일 왼쪽이 저입니다 :)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.