realloc - 将 int 转换为 char

发布于 2024-09-03 14:59:05 字数 2110 浏览 1 评论 0原文

我通过迭代整个数组将整数数组转换为 char,然后将生成的字符串添加到 ncurses 的方法 new_item 中。由于某种原因,我重新分配内存的方式出现了错误,因此我得到的第一列为:

-4 Choice 1                 0 Choice 1
 4 Choice 2                 1 Choice 1
 4 Choice 3  - Instead of - 2 Choice 1
 4 Choice 4                 3 Choice 1
 4 Exit                     4 Choice 1

-

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <menu.h>

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

char *choices[] = {
                        "Choice 1",
                        "Choice 2",
                        "Choice 3",
                        "Choice 4",
                        "Exit",
                  };
int table[5]={0,1,2,3,4}; 
int main()
{    ITEM **my_items;
    int c;              
    MENU *my_menu;
    int n_choices, i;
    ITEM *cur_item;

    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);

    n_choices = ARRAY_SIZE(choices);
    my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));

    char *convert = NULL;
    for(i = 0; i < n_choices; ++i){
        convert = (char *) malloc ( sizeof(char) * 4);
        sprintf(convert, "%i", table[i]); 
        my_items[i] = new_item(convert, choices[i]);
    }
    my_items[n_choices] = (ITEM *)NULL;

    my_menu = new_menu((ITEM **)my_items);
    mvprintw(LINES - 2, 0, "F1 to Exit");
    post_menu(my_menu);
    refresh();

    while((c = getch()) != KEY_F(1))
    {   switch(c)
        {   case KEY_DOWN:
                menu_driver(my_menu, REQ_DOWN_ITEM);
                break;
            case KEY_UP:
                menu_driver(my_menu, REQ_UP_ITEM);
                break;
        }
    }

    char *n = NULL, *d = NULL;

    unpost_menu(my_menu);
    free_menu(my_menu);
    for(i = 0; i < n_choices; ++i){
       n = (char *) item_name (my_items[i]);
       free (n);
       d = (char *) item_description (my_items[i]);
       free (d);
       free_item(my_items[i]);
    }

    free(my_items);
    endwin();
}

**更新:此问题已修复。请参阅上面的代码!

I'm converting an array of integers into a char by iterating through the whole array, and then I'm adding the resulting string to ncurses's method new_item. For some reason I'm doing something wrong the way I reallocate memory, thus I get the the first column as:

-4 Choice 1                 0 Choice 1
 4 Choice 2                 1 Choice 1
 4 Choice 3  - Instead of - 2 Choice 1
 4 Choice 4                 3 Choice 1
 4 Exit                     4 Choice 1

-

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <menu.h>

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

char *choices[] = {
                        "Choice 1",
                        "Choice 2",
                        "Choice 3",
                        "Choice 4",
                        "Exit",
                  };
int table[5]={0,1,2,3,4}; 
int main()
{    ITEM **my_items;
    int c;              
    MENU *my_menu;
    int n_choices, i;
    ITEM *cur_item;

    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);

    n_choices = ARRAY_SIZE(choices);
    my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));

    char *convert = NULL;
    for(i = 0; i < n_choices; ++i){
        convert = (char *) malloc ( sizeof(char) * 4);
        sprintf(convert, "%i", table[i]); 
        my_items[i] = new_item(convert, choices[i]);
    }
    my_items[n_choices] = (ITEM *)NULL;

    my_menu = new_menu((ITEM **)my_items);
    mvprintw(LINES - 2, 0, "F1 to Exit");
    post_menu(my_menu);
    refresh();

    while((c = getch()) != KEY_F(1))
    {   switch(c)
        {   case KEY_DOWN:
                menu_driver(my_menu, REQ_DOWN_ITEM);
                break;
            case KEY_UP:
                menu_driver(my_menu, REQ_UP_ITEM);
                break;
        }
    }

    char *n = NULL, *d = NULL;

    unpost_menu(my_menu);
    free_menu(my_menu);
    for(i = 0; i < n_choices; ++i){
       n = (char *) item_name (my_items[i]);
       free (n);
       d = (char *) item_description (my_items[i]);
       free (d);
       free_item(my_items[i]);
    }

    free(my_items);
    endwin();
}

**Update: This has been fixed. See code above!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2024-09-10 14:59:05

您尝试realloc与一次又一次相同的大小,realloc 只是返回相同的内存块。因此,您将覆盖 convert 的早期值,并在所有项目中存储相同的 char 数组。

您应该使用 malloc 代替:

// right here
char *convert = NULL;
for(i = 0; i < n_choices; ++i){
    convert = (char *) malloc (sizeof(char) * 4);
    sprintf(convert, "%i", table[i]); 
    my_items[i] = new_item(convert, choices[i]);
}

You attempt to realloc the same memory block with the same size again and again, for which realloc just returns the same memory block. So you are overwriting the earlier values of convert, and storing the same char array in all items.

You should use malloc instead:

// right here
char *convert = NULL;
for(i = 0; i < n_choices; ++i){
    convert = (char *) malloc (sizeof(char) * 4);
    sprintf(convert, "%i", table[i]); 
    my_items[i] = new_item(convert, choices[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文