Home > Net >  Initializing array members in for loop causes silent program termination?
Initializing array members in for loop causes silent program termination?

Time:06-13

When trying to initialize

charInfo[] 

via a for loop of any amount of iterations (or any loop for that matter), the program terminates unexpectedly with no indication of why.

#include <stdlib.h>
#include <windows.h>

int main(){

    //Create handle for screen buffer & pointer for accesing console cursor information
    HANDLE handleNewScreenBuffer;
    CONSOLE_CURSOR_INFO *ConsoleCursorInfo;

    //Create rectangle & character info array to be drawn on screen
    SMALL_RECT rectangle = {0, 0, 64, 7};
    CHAR_INFO charInfo[520];
    COORD coordBufSize = {65, 8};
    COORD coordBufCoord = {0, 0};

    handleStandardOut = GetStdHandle(STD_OUT_HANDLE);
    handleNewScreenBuffer = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    GetConsoleCursorInfo(handleNewScreenBuffer, ConsoleCursorInfo);
    ConsoleCursorInfo->bVisible = 0;
    SetConsoleCursorInfo(handleNewScreenBuffer, ConsoleCursorInfo);


    // Write green background and red & to character info array 
    for (int x = 0; x < (sizeof(charInfo)/4);   x){
        charInfo[x].Attributes = BACKGROUND_GREEN | FOREGROUND_RED;
        charInfo[x].Char.AsciiChar = (char)'&';
    }


    WriteConsoleOutput(handleNewScreenBuffer, charInfo, coordBufSize, coordBufCoord, &rectangle);
    SetConsoleActiveScreenBuffer(handleNewScreenBuffer);

    Sleep(5000);
    return 0;
}

Note: When values are initialized manually, there is no issue and the program runs as expected. Here the first and last squares of the console window are initialized correctly.

charInfo[0].Attributes = BACKGROUND_GREEN | FOREGROUND_RED;
charInfo[0].Char.AsciiChar = (char)'&';
charInfo[519].Attributes = BACKGROUND_GREEN | FOREGROUND_RED;
charInfo[519].Char.AsciiChar = (char)'&';

Note: I also understand that this is an outdated way of doing things I'm just exploring a bit.

Do the arrays leave scope when entering a loop? That doesn't make any sense but I'm unsure of any other reason this could happen.

UPDATE: Upon further inspection it seems that the code above can be simplified by removing handleStandardOut = GetStdHandle(STD_OUT_HANDLE); which resolves the issue.

Still begs the question of why establishing a handle to the output buffer (which is un-needed for right now anyway but I digress) causes such strange behavior.

CodePudding user response:

Removing handleStandardOut = GetStdHandle(STD_OUT_HANDLE); resolves the issue. Inplying that as @yano says, the uninitialized *CONSOLE_CURSOR_INFO pointer is causing unpredictability. By creating the CONSOLE_CURSOR_INFO object and then creating the pointer to said object as CONSOLE_CURSOR_INFO *ConsoleCursorInfoPtr = &ConsoleCursorInfo; the line handleStandardOut = GetStdHandle(STD_OUT_HANDLE); can be added back in and behavior is normal.

  • Related