More of a curiosity at the moment, is there a way to know if initscr()
of <ncurses.h>
has been called before? I believe calling initscr()
twice is a bad idea.

- 51,086
- 7
- 70
- 105

- 414
- 5
- 16
2 Answers
When initscr
is called successfully, it sets the variables stdscr
and curscr
, which are only non-null after initialization. You do not need extra variables for this purpose. curses also initializes the variables LINES
and COLS
, which usually give the actual screen size (except as noted in the use_env
manual page).
As noted in a comment, initscr
only returns to the caller if successful. Its complement newterm
initializes the same variables, but can return to the caller if not successful.

- 51,086
- 7
- 70
- 105
-
Checking that `stdscr != nullptr` isn't always the safest check as stdscr will remain non-null even if endwin() has been called. So while the check can be used to see if initscr has been called at least once it isn't safe to check that you are in curses mode. – mfbutner Aug 26 '18 at 23:20
-
but that wasn't the **question** – Thomas Dickey Aug 26 '18 at 23:23
-
You are right. Your solution answers the question posed. I just wanted to make sure that people didn't misuse your answer to check for something it wasn't meant to check for. – mfbutner Aug 27 '18 at 23:43
-
That aspect can be checked using [**`isendwin`**](https://invisible-island.net/ncurses/man/curs_initscr.3x.html#h3-isendwin) – Thomas Dickey Aug 27 '18 at 23:54
You could have a bool variable that's initialized to false and set to true immediately after calling initscr() (and performing other initializing). The downside is you have to remember to check the bool variable, and set it after calling initscr().
There's a straightforward example in this link: http://math.hws.edu/orr/s04/cpsc225/curses.html
NOTE: As Jonathon Leffler points out in the comments, this link isn't a great example overall, I'm just pointing you to the snippet of code where he initializes everything to give an example.
I'm not familiar with the library, but it looks like you could also declare a WINDOW* and set it to null, and then check to see if it's null later to see if it's been called. The documentation I found from googling says that on failure the function doesn't return so you wouldn't have a null pointer if it's run. That's in essence the same as using a bool, but maybe a little less cluttered (and checking pointers to see if they're null is a common C idiom).
A very C++ way to do it would be to create a class that wraps the WINDOW*, or whatever is returned by the function, then call the initscr() in the constructor. If there's a corresponding function to deallocate it, call it in the destructor. By using a singleton design pattern, the class can only be constructed once.
An even safer way is to wrap the WINDOW* in an std::unique_ptr, but pass a custom deleter to it (How do I use a custom deleter with a std::unique_ptr member?) - assuming there's a function that deallocates the window.
Then you don't need anything in the destructor, the smart pointer will deal with it for you.

- 1
- 1

- 955
- 5
- 11
-
Grumble: your reference cites the compile/link command as `g++ -g -Wall -lncurses -otestcurses testcurses.cc`. That's a bad choice — the library should be listed after the object files (or source file in this case), not before: `g++ -g -Wall testcurses.cc -lncurses -o testcurses`. I know there's nothing you can do about that, and it looks OK otherwise. It isn't a complete tutorial on exploiting ncurses, but it doesn't pretend to be, either. – Jonathan Leffler Jul 18 '15 at 03:26
-
Ah, my only interest in the tutorial was the sample code under "Getting into and out of Curses Mode" where he uses the boolean to track if he's called it or not. That's not my favored solution. – mock_blatt Jul 18 '15 at 04:17
-
1My previous comment was mostly a minor warning to people who might read it as a result of your linking to it. There isn't a complete solution; if there is other code not under your control that might (or might not) have called `initscr()`, there is very little you can do about detecting whether it was called. Obviously, if you're in charge of all the code then you can indeed provide yourself a wrapper that does the tracking and relevant initialization. The general problem is insuperable unless the library provides a mechanism to check (or `initscr()` checks for itself). – Jonathan Leffler Jul 18 '15 at 04:27
-
@mock_blatt I was hoping for an ncurses variable/function, not a user defined one. I'll mark this as the answer anyway though because it works. – Sanfer Jul 18 '15 at 04:31
-
(looks like the clean-up function is endwin(), by the way) ncurses appears to be a classic C API, and having an 'init' function of some type is common. They may or may not have a check to see if you've already called it, but outside of that you're not going to get built-in help. You can use the language constructs of C (checking for null pointer or a tracking variable), or in your case the higher-level constructs of C++ (class wrapper). – mock_blatt Jul 18 '15 at 04:40
-
The given example makes as much sense as a function to test if stdout is open. – Thomas Dickey Jul 19 '15 at 10:27