Is it okay if the array length declared in a C++ array is a variable?

this is okay too... 

The program can run normally, and the printed data size is still correct... How to explain?

The compiler is aarch64-linux5.4-gcc

C++ 14        

After searching for information for a long time, I summarized the opinions on the Internet:

1. In this case, the gcc extension is in the form

2. It is not recommended to use it in this way. It is difficult to troubleshoot if there is any problem.

3. Be honest and use malloc or new

What I mean is: don’t be timid, use it directly, it smells so good~~, pay attention to control the data_size

--------------------Gorgeous dividers----------------

Note: [ ] Before c99, it must be a constant. After c99, the concept of variable-length arrays is supported.

So it can be written like this:

void method(int data_size)
{
    i(data_size<=0 || data_size>100)
    {
        printf("data size exception");
        return;
    }
	uint8_t data[data_size];
	memset(data, 0, sizeof(data));
}

Guess you like

Origin blog.csdn.net/wyl530274554/article/details/128005767