首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么只有当选择数组是3或7个元素时才会发生分段错误?

为什么只有当选择数组是3或7个元素时才会发生分段错误?
EN

Stack Overflow用户
提问于 2019-04-24 08:22:08
回答 1查看 224关注 0票数 2

我是一个C的初学者,并试图开发一个小软件的ncurses,菜单驱动的界面。我有这段代码,那是来自http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/menus.html的。

我不明白为什么如果初始化一个由3或7个项组成的选项数组,程序就会出现分段错误,但是如果数组是任何其他大小的,那就没问题了。

这是密码。如果将1添加到n_choices或calloc()调用中,则不会发生分段错误。即

代码语言:javascript
复制
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));

为什么3和7是“特别”的?

代码语言:javascript
复制
#include <ncurses.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <menu.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

WINDOW *mywin;
int rows, cols, h, w, starty, startx;
int wybeg, wxbeg, wymax, wxmax;

// choices
char *choices[] = {
    "Choice 1",
    "Choice 2",
    "Choice 3",
    "Choice 4",
    "Choice 5",
    "Choice 6",
    "Choice 7",
};
void func(char*);

int main(int argc, char** argv)
{
    ITEM **my_items;
    int c;              
    MENU *my_menu;
    int n_choices, i;
    ITEM *cur_item;

    initscr(); //creates stdscr
    cbreak();
    noecho();
    curs_set(0);
    keypad(stdscr, TRUE);

    if(has_colors() == TRUE){
        start_color();
        init_pair(1,COLOR_YELLOW,COLOR_BLUE);
    }

    getmaxyx(stdscr, rows, cols);
    h = rows-4;
    w = cols;
    starty = 1;
    startx = 0;

    char quitHint[] = "<Press q to exit>";

    move(0,0);
    mvprintw(LINES-1, COLS-1-strlen(quitHint), "%s", quitHint);
    refresh();

    mywin = newwin(h,w,starty,startx);
    keypad(mywin,TRUE);
    box(mywin,0,0);
    wbkgd(mywin,COLOR_PAIR(1));
    wrefresh(mywin);

    // MENU
    /* Create items */
    /**************************/
    /* FROM HERE IS THE POINT */
    /**************************/
    // if choices is 3 or 7 elements, segfault occurs! If adding +1 n_choices segfault doesn't occur
    n_choices = ARRAY_SIZE(choices);
    my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
    for(i = 0; i < n_choices; ++i){
        my_items[i] = new_item(choices[i], choices[i]);
        /* Set the user pointer */
        set_item_userptr(my_items[i], func);
    }

    mvprintw(LINES-2,2,"sizeof(choices) = %3d | sizeof(*choices) = %3d | n_choices = %3d", sizeof(choices), sizeof(*choices), n_choices);

    /* Create menu */
    my_menu = new_menu((ITEM **)my_items);

    /* Set main window and sub window */
    set_menu_win(my_menu, mywin);
    set_menu_sub(my_menu, derwin(mywin, n_choices, 38, 2, 2));

    /* Set menu mark to the string " * " */
    set_menu_mark(my_menu, " * ");
    refresh();

    set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);  // selected
    set_menu_back(my_menu, COLOR_PAIR(1));                          // unselected

    /* Post the menu */
    post_menu(my_menu);
    wrefresh(mywin);

    while((c = wgetch(mywin)) != 'q')
    {
        switch(c)
        {   case KEY_DOWN:
                menu_driver(my_menu, REQ_DOWN_ITEM);
                break;
        case KEY_UP:
            menu_driver(my_menu, REQ_UP_ITEM);
            break;
        case 10: /* enter */
        {
            /* execute func() from item pointer */
            ITEM *cur;
            void (*p) (char *);

            cur = current_item(my_menu);
            p = item_userptr(cur);
            p((char *)item_name(cur));
            pos_menu_cursor(my_menu);
            break;
        }
        }
        wrefresh(mywin);
    }

    /* Unpost and free all the memory taken up */
    unpost_menu(my_menu);
    free_menu(my_menu);
    for(i = 0; i < n_choices; ++i){
        free_item(my_items[i]);
    }

    endwin();
    return EXIT_SUCCESS;
}

void func(char *local_choice){  
    mvwprintw(mywin,h-2, 2, "Item selected is : %s", local_choice);
    wclrtoeol(mywin);
    box(mywin,0,0);
    wrefresh(mywin);
}   

gcc -lmenu -lncurses编译

运行Debian Buster 4.19.0-2-686 #1 SMP Debian 4.19.16-1 (2019-01-17) i686 GNU/Linux

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-24 14:08:00

也许作者也犯了同样的错误,决定分配一些额外的内存?但是,为什么3和7是“特殊”大小和三角分段故障?

不,这是new_menu函数想要的:

new_menu函数期望items列表被NULL元素终止。

如果您阅读new_menu文档(new.3x.html),您可以看到:

简要说明 #include <menu.h> MENU *new_menu(ITEM **items); 描述 The function new_menu creates a new menu connected to a specified item pointer array (which must be NULL-terminated).

因此,在3和7的情况下,您看到的崩溃是与-I相关的,与如何将数据存储在内存中有关。您可以做的一件事是在调用my_items函数之前将new_menu内存+一个元素转储到文件中。

我猜在元素之后你会有一些0的值,但在3和7的情况下没有。(我猜是4-多重加值效应)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55825477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档