2017-02-05 9 views
-1

누구든지 ASCII 아트 토러스 (donut.c)로 완료 한 프로젝트의 코드를 수정하고있었습니다. 필자는 객체 지향 패러다임에서이 프로그램의 코드로 다른 3 차원 도형을 그리도록 강요 받았지만 다중 플랫폼에서 실행되도록 수정하려고했습니다. 라이브러리를 Windows, Linux 및 Mac에서 사용할 수 있도록 ncurses과 함께 C++을 사용하기로 결정했습니다. 그러나 코드에 오류가 있습니다. 코드의 특정 부분에서 세그먼트 오류가 계속 발생하며 문제를 해결하기 위해 앞으로 나아갈 위치를 알 수 없습니다.ncurses 세그먼테이션 결함

코드 (아래 참조)를 사용하여 g++ -Wall -g test.cc -o test -lncurses을 사용하여 컴파일하므로 gdb을 사용하여 문제가 어디에 있는지 알 수 있습니다.

test 변수가 나는 3천30번째 프레임에서 알 곳에 지점에 설정되어
(gdb) watch test == 3030 
Hardware watchpoint 1: test == 3030 
(gdb) r 
Hardware watchpoint 1: test == 3030 

Old value = 0 
New value = 1 
main (argc=1, argv=0xbffff1d4) at test.cc:38 
38   Output(output, screen_height, screen_width); 
(gdb) step 
Output (
    output=0xbfffe958 ' ' <repeats 28 times>, "[email protected]@@@@@", '$' <repeats 15 times>, ' ' <repeats 54 times>, '$' <repeats 11 times>, '#' <repeats 13 times>, "$$$##", ' ' <repeats 48 times>, "##$$$$#$####******"..., screen_height=24, 
screen_width=80) at test.cc:91 
91  printw(output) 
(gdb) step 
Program received signal SIGSEGV, Segmentation fault. 
__wcslen_sse2() at ../sysdeps/i386/i686/multiarch/wcslen-sse2.S:28 
28  ../sysdeps/i386/i686/multiarch/wcslen-sse2.S: No such file or directory. 

, 그것은 밖으로 결함 것입니다 : 여기에 그 만남의 출력이다. 흥미롭게도 screen_width 및값이 변경되면 분할 오류가 발생하는 "프레임"은 여전히 ​​동일합니다. 나는 내 ncurses 라이브러리가이 프로젝트 (2007 년 2 월 1 일)와 함께 작동하도록 특별히 설치되었으므로 최신 것으로 믿습니다.

내 C++ 코드이며 원래 donut.c에서 수정되었으며 해당 코드로만 수정되었습니다.

static volatile int test = 0; //used for checking what frame it happened at 

void Output(char* output, int screen_height, int screen_width); //where segfault happens 
void RenderFrame(float x_rotation, float z_rotation, char* output, float* zbuffer, int screen_height, int screen_width); 

int main(int argc, const char **argv) 
{ 
    //Initialization completed. Note that I call initscr() and necessary methods before 
    while(getch() == ERR) 
    { 
    RenderFrame(x_rotation, z_rotation, output, zbuffer, screen_height, screen_width); 
    test++; 
    Output(output, screen_height, screen_width); 
    x_rotation += x_rotation_delta; 
    z_rotation += z_rotation_delta; 
    } 
    endwin(); 

    return 0; 
} 

void Output(char* output, int screen_height, int screen_width) 
{ 
    printw(output); 
    refresh(); 
    move(0, 0); 
} 

TL; DR

ncursesSegmentation faultprintw 발생의 문제가있다. 이 문제를 해결하거나 올바르게 이동하려면 어떻게해야합니까? 그렇다면 ncurses 또는 내 코드에 문제가 있습니까?

+0

'RenderFrame'에서'index'를 사각형 화면으로 벗어나기 쉽게 만듭니다. 그것을 지키고'printw'에 대한 문제가 없어지는지보십시오. 그렇지 않으면 적어도 하나의 버그를 괴롭 혔습니다. – user4581301

+2

valgrind는 이와 같은 버그를 찾는 데 유용합니다 (ncurses에서 버그가되지는 않을 것입니다). 그건 그렇고,'addstr'은'printw'보다 사용하는 것이 더 낫습니다 (길잃은'%'는 코드에 나쁜 일을합니다). –

+0

'float zbuffer [screen_width * screen_height]'-이 줄과 이와 유사한 줄은 C++이 아닙니다. 대신에'std :: vector '를 사용했다면, 최소한 벡터에 대한 모든 접근이'at()'를 사용하여 범위 내에 있는지 확인할 수 있습니다. – PaulMcKenzie

답변

0

문제는 코드에 printwaddstr으로 변경해야한다는 질문에 대한 그의 의견에서 설명한 Thomas Dickey과 같았습니다. 의견을 보내 주셔서 감사합니다. printw과 (과) 같은 이유가 궁금합니다.