C language blocks[7][2]={0,}; stack buffer error during initialization

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

static int m_Glass[7][2] = { {1,1},{1,1},{1,1},{1,1},{1,1},{1,1},{1,1} };


void InitBlocks(RECT blocks[][2], RECT& bound);
void DrawBlocks(HDC hdc, RECT blocks[][2]);

static RECT Blocks[7][2];

int randomnumber;
int rand_array[7];

InitBlocks(Blocks, clientR);

DrawBlocks(hdc, Blocks);

        for (int k = 0; k < 7; k++) {
            randomnumber = rand() % 2;
            rand_array[k] = randomnumber;
        }

        Blocks[7][2] = {0, };//0 for breaking glass; 1 for reinforced glass


        for (int row = 0; row < 7; row++) {
            Blocks[row][rand_array[row]] = { 1, };
        }

void InitBlocks(RECT blocks[][2], RECT& bound) {

    RECT r, t;
    int w, h;

    r = bound;
    r.right /= 2;

    r.top = 50;
    r.bottom = r.top + 40;

    w = r.right - r.left;
    h = r.bottom - r.top;

    for (int i = 0; i < 7; i++) {
        t = r;
        OffsetRect(&r, 0, h);

        for (int j = 0; j < 2; j++) {
            blocks[i][j] = t;
            OffsetRect(&t, w, 0);
        }
    }
}


void DrawBlocks(HDC hdc, RECT blocks[][2]) {
    COLORREF color = 255;

    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 2; j++) {
            if (m_Glass[i][j])
                DrawObject(hdc, blocks[i][j], RGB(0, 0, 0), RGB(0, color, color), 0);
        }
    }
}

I imported only a part of the code, not the whole code, but it says a stack buffer error occurs during Blocks[7][2]={0,}; initialization. I looked up Googleing, but most of the errors about stack buffers are due to the for syntax setting incorrectly, but I think I set the for syntax setting correctly.

The code I want is to initialize 0 and 1 to a structure called RECT (under certain conditions) and get a value of 0 or 1 back, but I don't know what to do with it.

I'd appreciate it if you could help me because I think I'm talking too much.

c api

2022-09-20 13:11

1 Answers

First of all, the code you uploaded does not fit the grammar of C language.

Blocks[7][2] = {0, };//0 for glass breaking; 1 for tempered glass and Blocks[row][rand_array[row]] = {1, }; These codes are not allowed grammar.

You said you were experiencing a stack buffer error, but Blocks exists in the global space, so it is unlikely that stack overflow will occur even with incorrect memory access.

More detailed error messages are needed to determine exactly what error stack buffer errors refer to.

Did it happen at runtime? Did it happen at compile time? Was that code really compiled and executable?

Before we start, I looked at the code again, and it's C++. The code you wrote is not in C language.

Code error number is C6200, C6386. On the C6200, it says 7 is above the valid range 0 to 6 for the next non-technical buffer, and on the C6386, it says buffer overruns occurred, and the actual writable size is 224 bytes, but it can actually write only 256 bytes.

It's literally about accessing the wrong index.

static RECT blocks[7][2];Blocks have 7 and 2 in size, so the accessible indexes are 0-7 and 0~1.

However, Blocks[7][2] = {0, };//0 is breaking glass, and 1 is reinforced glass is approaching 7 and 2 indexes, so this warning is occurring.

I'd like to use a two-dimensional array structure to initialize a number (0 or 1) in each structure to get the initialized value back

Looking at the code, it seems that the desired action is to look at the value of Blocks and substitute 0 or 1 to m_Glass depending on the conditions. The conditions seem to be based on random numbers.

Blocks[7][2] = {0, };//0 for broken glass, 1 for reinforced glass for which you want to initialize, but the target is invalid. Therefore, initialize m_Glass to 0 as follows:

for (int i = 0; i < 7; ++i) {
    for (int j = 0; j < 2; ++j)
        m_Glass[i][j] = 0;
}

Subsequently, place 0 or 1 in m_Glass according to the content of land_array.

for (int i = 0; i < 7; ++i) {
    m_Glass[i][rand_array[i]] = 1;
}

After that, calling DrawBlocks(hdc, Blocks); will determine whether the picture of the corresponding block is glass or reinforced glass based on 0 or 1 entered in m_Glass.

For that to happen, the code in DrawBlocks() should be:

void DrawBlocks(HDC hdc, RECT blocks[][2]) {
    COLORREF color = 255;

    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 2; j++) {
            if (m_Glass[i][j])
                DrawObject(hdc, blocks[i][j], RGB(0, 0, 0), RGB(0, color, color), 0);
            else
                ; // Drawing on tempered glass
        }
    }
}


2022-09-20 13:11

If you have any answers or tips


© 2022 pinfo. All rights reserved.