字符缓冲区输入超过256个字符

时间:2016-08-01 10:36:19

标签: c arrays char watcom

我使用FreeRTOS内核在DOS上创建程序,这允许我在包含自己的文本用户界面的屏幕上呈现多个窗口。问题是我遇到了溢出错误,这是由于将超过256个字符输入缓冲区引起的。有办法解决这个问题吗?

我的部分代码:

int default_background=0;
int default_foreground=15;

struct window {                        /* Window structure */
    int special_id;
    int cursor_x,cursor_y;
    int width,height;
    long *background,*foreground;
    char *buffer;
};

long window_index_count=0;
struct window current_windows[10];

long create_window(int width,int height) {
    int i;
    long t;
    struct window new_window;
    new_window.special_id=window_index_count;
    window_index_count=window_index_count+1;
    new_window.cursor_x=0;
    new_window.cursor_y=0;
    new_window.width=width;
    new_window.height=height;
    for (t=0; t<width*height; t++) {
        new_window.background[t]=default_background;
        new_window.foreground[t]=default_foreground;
        new_window.buffer[t]=' ';      /* This is where the error occurs */
    }
    for (i=0; i<10; i++) {
        if (current_windows[i].special_id<=0) {
            current_windows[i]=new_window;
            break;
        }
    }
    return new_window.special_id;
}

1 个答案:

答案 0 :(得分:2)

您实际上并未为缓冲区分配内存。由于默认情况下本地非静态变量未初始化,因此它们的值为 indeterminate 。因此,当您使用指针new_window.buffer时,您不知道它指向何处,导致未定义的行为

它与结构中的其他指针一样。

解决方案是实际为指向指针的内存分配内存。

相关问题