A
A
aldexnotproger2021-11-05 12:36:08
C++ / C#
aldexnotproger, 2021-11-05 12:36:08

Why is it possible to define a variable length static array in c++?

Why is this allowed? Isn't memory allocated for an array at compile time? And why does sizeof correctly determine the size of mas (if, for example, you enter n = 5, then sizeof(mas) will be equal to 20 bytes, while sizeof(int) is equal to 4 bytes on my system)?
6184faff35ec4703781909.png
I am compiling this code with MinGW x64

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2021-11-05
@aldexnotproger

This is because you are compiling with gcc.
In general, there is no VLA (variable length array) in the C ++ standard, in principle, and this is a mistake.
But VLA is in C99 and beyond. And gcc with default options allows you to use extensions, incl. and VLA.
If you turn on stricter compliance with the standard, you will get an error and uncompiled code.
I recommend that you always do this (enable strict compliance with the standard) in your projects. And also turn on the output of all warnings (-Wall -Wextra) and try to build without warnings.
The most "advanced" ones translate all warnings into errors, so that the program would not build with any warnings in principle. This is also done by the appropriate compiler option.
Often in such cases it is necessary to ignore some warnings, but this is always done consciously, with an understanding of why this is needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question