I was modifying code from a project someone had finished with an ASCII art torus, known as donut.c
. I felt compelled to draw other three dimensional shapes with the code from this program in an object-oriented paradigm, but also wanted to modify it to let it run on multiple platforms. I chose to use C++ with ncurses
so that the library could be used on Windows, Linux, and Mac. However, I am running into an error with my code: a segmentation fault occurs at a very specific part of my code consistently, and I don't know where to move forward on fixing the issue.
I take the code (found below) and compile it using g++ -Wall -g test.cc -o test -lncurses
so we can use gdb
to find out where the issue is. Here is the output of that encounter:
(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>, "$$@@@@@@", '$' <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.
The test
variable is set up to the point to where I know at the 3030th frame, it will fault out. Interestingly enough, if the screen_width
and screen_height
value changes, the "frame" at which the segmentation fault happens is still the same. I believe my ncurses
library is up to date, as I installed it specifically to work with this project (February 1st 2017).
Here is my C++ code, modified from the original donut.c
and redacted to fit only code that is deemed pertinent.
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
has an issue with printw
that causes a Segmentation fault
. How do I fix this issue or move around it so that I can get this to work correctly. More so, is it an issue with ncurses
or my code?