Array size at run time without dynamic allocation is allowed?
This is valid in C99.
C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.
Note that this is different from malloc
and new
. gcc
allocates the array on the stack, just like it does with int array[100]
by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca
.
Integer array size in C without using dynamic memory allocation
The size of global arrays must be fixed at compile time. VLAs are only allowed inside of functions.
If the value of symbolnum
isn't known until runtime, you'll need to declare stab
as a pointer type and allocate memory for it dynamically.
Alternately, if the array doesn't take up more than a few 10s of KB, you can define the VLA in the main
function and set a global pointer to it.
Why does C++ allow passing the array size at run-time to a function in order to construct a fixed-size array?
Why does C++ allow passing the array size at run-time to a function in order to construct a fixed-size array?
C++ language does not allow you to use a function parameter as the length of an array variable. The length of an array must be a compile time constant expression.
The length of myArray
in your example is not a compile time constant expression, therefore the program is ill-formed.
So, why is it compiling?
Because your compiler chose to do so. The C++ language merely allows the compiler to fail compilation of ill-formed programs. They are allowed to compile succesfully, which lets the compiler to extend the language. You are using a language extension.
The C++ standard does require the compiler to diagnose ill-formed programs (except when allowed not to do so by a specific rule), and if it did not issue a diagnostic message for you, then the compiler does not conform to the standard.
Here is example output from a conforming compiler:
warning: ISO C++ forbids variable length array 'myArray' [-Wvla]
can size of array be determined at run time in c?
Array sizes need to be known with ANSI 89 C. The 99 version of the spec removed this limitation and allowed for variable sized arrays.
Here is the documentation no the GNU version of this feature
- http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC49
Which compilers support arrays with a size fixed at runtime and which compilers don't? [duplicate]
I clicked through most of the compilers available at https://godbolt.org/ and it seems that only the Microsoft Visual Studio compiler rejects it. GCC and Clang on several platforms are fine with this code.
Note that it is only an additional support, both compiler can decide to stop accepting the code, as it is non standard.
The array will be on the stack for both compilers.
Related Topics
What Is a Smart Pointer and When Should I Use One
Where Do I Find the Current C or C++ Standard Documents
Correct Way of Passing Pointer to Another Thread
C++: How to Iterate Over Each Char in a String
When Should Static_Cast, Dynamic_Cast, Const_Cast, and Reinterpret_Cast Be Used
What Are the Basic Rules and Idioms For Operator Overloading
Most Efficient Way of Copying a Raw Byte Array into an Empty Byte Vector
Escape Sequence \F - Form Feed - What Exactly Is It
Define Preprocessor Macro Through Cmake
What Are Copy Elision and Return Value Optimization
Why Isn't Sizeof For a Struct Equal to the Sum of Sizeof of Each Member
Most Efficient Way to Compare a Variable to Multiple Values
How to Fully Disable Resizing a Window Including the Resize Icon When the Mouse Hovers the Border
Why Can In-Class Initializers Only Use = or {}