我遇到了以下 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
$1 = (WINDOW *) 0x0
(gdb) p errno
$2 = 0
(gdb)
Man page表示 NULL 可能仅在 errno = ENOMEM
时返回。
我使用 Debian Jessie 64 位、gcc 4.9、libncurses 5.9。
我的问题是:我做错了什么以及为什么我得到 NULL 指针而不是 supbad?
最佳答案
subpad() 期望父 pad 作为第一个参数,而不是 WINDOW (stdscr) 尽管对窗口和 pad 的引用存储在 WINDOW* 中,但实际上它们之间存在一些差异(pad 缺少屏幕坐标且无法刷新刷新)。
来自手册页:
"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."
所以你应该先创建父垫:
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();
关于c - subpad 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32728028/