誰かが完成したプロジェクトのコードをdonut.c
というASCIIアートトーラスで修正していました。私は、このプログラムのコードをオブジェクト指向のパラダイムで他の3次元図形を描くことを強いられましたが、複数のプラットフォーム上で実行できるように変更したいと思っていました。私はncurses
でC++を使用して、Windows、Linux、Macでライブラリを使用できるようにしました。しかし、私は自分のコードでエラーに遭遇しています。セグメンテーションフォルトはコードの特定の部分で一貫して発生します。問題を解決するためにどこに進むべきかわかりません。ncursesセグメンテーションフォールト
g++ -Wall -g test.cc -o test -lncurses
を使用してコードを取得してコンパイルします(gdb
)ので、問題の原因を特定できます。ここではその出会いの出力は、次のとおりです。
(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.
test
変数が、私は第三千三十フレームで知っている場所を指すように設定されている、それが出てフォルトが発生します。面白いことに、screen_width
とscreen_height
の値が変更された場合、セグメンテーションフォルトが発生する「フレーム」は同じです。私はncurses
ライブラリが最新であると確信しています。私はこのプロジェクト(2017年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
ncurses
はSegmentation fault
せるprintw
の問題を有しています。この問題を解決するにはどうしたらいいですか。さらに、それはncurses
または私のコードの問題ですか?
'RenderFrame'では、' index'を長方形の画面で外に押し出すのが比較的簡単です。それを防ぎ、 'printw'の問題がなくなるかどうか確認してください。もしそうでなければ、少なくともあなたは1つのバグを押しつぶしました。 – user4581301
valgrindはこのようなバグを見つけるのに便利です(ncursesのバグではないでしょう)。ちなみに、 'addstr'は' printw'よりもあなたの作成に適しています(あなたのコードでは '%'は悪いことです)。 –
'float zbuffer [screen_width * screen_height]' - この行とこのような行は有効なC++ではありません。代わりに 'std :: vector'を使用した場合は、少なくとも、ベクトルへのすべてのアクセスが 'at()'を使って境界内にあることを確認することができます。 –
PaulMcKenzie