Variable length arrays is a feature where we can allocate an auto array (on stack) of variable size. C supports variable sized arrays from C99 standard. For example, the below program compiles and runs fine in C.
But C++ standard (till C++11) doesn’t support variable sized arrays. The C++11 standard mentions array size as a constant-expression See (See 8.3.4 on page 179 of N3337). So the above program may not be a valid C++ program. The program may work in GCC compiler, because GCC compiler provides an extension to support them.
Student_id : 523 Stud_Name : Sanjayulsha Name_Length: 11 Allocated_Struct_size: 23 Student_id : 535 Stud_Name : Cherry Name_Length: 6 Allocated_Struct_size: 18 Size of Struct student: 12 Size of Struct pointer: 8
This article is contributed by Abhay Rathi and Sanjay Kanna. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- A shorthand array notation in C for repeated values
- new and delete operators in C++ for dynamic memory
- sizeof operator in C
- Functions in C/C++
- What are the data types for which it is not possible to create an array?
- How to write long strings in Multi-lines C/C++?
- Function Pointer in C
- How to pass a 2D array as a parameter in C?
- How to dynamically allocate a 2D array in C?
- Difference between pointer and array in C?
- Initialization of variables sized arrays in C
- Data type of case labels of switch statement in C++?
- Storage for Strings in C
- calloc() versus malloc()
- What is Memory Leak? How can we avoid?