0

我正在使用 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;
}
4

1 回答 1

2

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

结构中的其他指针也一样。

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

于 2016-08-01T10:42:37.407 回答