C के Variable Length Arrays
In this page:
int size = value;
data_type array_name[size]; // size decided at runtime
VLA क्या है?
एक variable length array (VLA) आपको array declared होने के moment एक variable की value इस्तेमाल करके इसे size करने देता है, int arr[10] जैसे compile-time constant चाहिए होने की बजाय।
यह तब handy है जब आपको program के already चलते तक array का exact size न पता हो, जैसे एक user-provided count पर एक buffer size करना।
उदाहरण: What is a VLA?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 5
int n = 5;
int arr[n];
arr[0] = 1;
// Print formatted text to the screen
printf("%d", arr[0]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
VLAs Declare और इस्तेमाल करना
एक VLA declare करने के लिए, पहले size को compute करें या पढ़ें एक integer variable में, फिर उस variable को सीधे array brackets के अंदर इस्तेमाल करें, जैसे int n = getCount(); int values[n];।
Size expression declaration के point पर एक बार evaluate होता है, इसलिए बाद में variable बदलने से array resize नहीं होता।
उदाहरण: Declaring and Using VLAs
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 3
int n = 3;
int values[n];
// Loop: repeat while the condition holds
for (int i = 0; i < n; i++) {
values[i] = i * 2;
}
// Print formatted text to the screen
printf("%d", values[2]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions के अंदर VLAs
VLAs खासकर उन helper functions के अंदर useful हैं जिन्हें caller के input के लिए exactly sized एक temporary buffer चाहिए, एक fixed maximum size over-allocate करने या किसी short-lived चीज़ के लिए malloc() call करने की ज़रूरत से बचते हुए।
function के return होते ही array automatically गायब हो जाता है, कोई manual cleanup ज़रूरी नहीं।
उदाहरण: VLAs inside Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printBuffer(int size) {
int buffer[size];
buffer[0] = 99;
// Print formatted text to the screen
printf("%d", buffer[0]);
}
// Program execution starts in main()
int main() {
printBuffer(5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
VLAs का Memory Allocation
malloc() के उलट, जो heap पर memory reserve करता है जो तब तक रहती है जब तक आप इसे explicitly free() न करें, एक VLA stack पर allocate होता है और इसके enclosing scope के खत्म होते ही automatically reclaim हो जाता है।
यह short-lived buffers के लिए VLAs को इस्तेमाल करना simpler बनाता है, क्योंकि उन्हें free करना भूलने का कोई risk नहीं।
उदाहरण: Memory Allocation of VLAs
// Include standard input/output (printf, scanf)
#include <stdio.h>
void useVLA() {
// Declare n and set it to 4
int n = 4;
int arr[n];
arr[0] = 1;
// Print formatted text to the screen
printf("%d", arr[0]);
}
// Program execution starts in main()
int main() {
useVLA();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
VLAs की Limits
Stack space heap space से कहीं छोटा है (अक्सर बस कुछ megabytes), इसलिए एक unchecked या attacker-controlled variable से sized एक VLA declare करना stack overflow कर सकता है और program crash कर सकता है।
इसे declare करने से पहले हमेशा verify करें कि एक VLA की requested size एक sane, expected bound में रहती है।
उदाहरण: Limits of VLAs
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 10
int n = 10;
int arr[n];
arr[0] = 1;
// Print formatted text to the screen
printf("%d", arr[0]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक बहुत बड़ा VLA declare करना, जो stack overflow कर सकता है।
- declaration पर एक VLA initialize करने की कोशिश करना, जो allowed नहीं है।
- यह मान लेना कि हर compiler VLAs support करता है, जब वे C11 में optional हो गए।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions