子垫 returns NULL

subpad returns NULL

我遇到了以下 ncurses 问题:当我尝试在主屏幕上创建子板时,我收到 NULL 指针作为结果并且 errno = 0.

测试示例:

#include <curses.h>
#include <assert.h>

int main(int, char**) {
    initscr();

    WINDOW *pad = subpad(stdscr, 10, 10, 6, 1);
    assert(pad);
    delwin(pad);

    endwin();

    return 0;
}

编译它:

$ g++ -g -O0 -o pad ./main.cpp -lncurses

就在断言触发之前,我有以下状态:

(gdb) run
Starting program: /tmp/ncurses/pad 

Breakpoint 1, main () at ./main.cpp:10
10      assert(pad);
(gdb) p pad
 = (WINDOW *) 0x0
(gdb) p errno
 = 0
(gdb) 

Man page 表示只有 errno = ENOMEM.

才可能返回 NULL

我使用 Debian Jessie 64 位、gcc 4.9、libncurses 5.9。

我的问题是:我做错了什么以及为什么我得到 NULL 指针而不是 supbad?

subpad() 期望父 pad 作为第一个参数,而不是 WINDOW (stdscr) 尽管对 windows 和 pad 的引用存储在 WINDOW* 中事实上,它们之间存在一些差异(pad 缺少屏幕坐标,无法使用 wrefresh 进行刷新)。

来自联机帮助页:

"A pad is like a window, except that it is not restricted by the screen size, and is not necessarily associated with a particular part of the screen."

所以你应该先创建parent pad:

WINDOW *ppad, *subpad;

ppad = newpad(50,50);
if (ppad == NULL) {
    /* always check for null */
}

/* create the subpad */
subpad = subpad(ppad, lines, cols, y, x);
if(subpad == NULL ) {
    /* always check for null */
}   

addstr("Subpad created\n");
refresh();

/* just a pause... */
getch();