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 に答える