← Back to C Course | Chapter 12: Advanced Topics | Lesson 4 of 20

C के Variable Length Arrays

एक variable length array एक ऐसा array है जिसका size program चलते समय decide होता है, guest count पता होने पर कितनी seats रखनी हैं चुनने जैसा। इसका size बनने के बाद fixed होता है।
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक बहुत बड़ा VLA declare करना, जो stack overflow कर सकता है।
  2. declaration पर एक VLA initialize करने की कोशिश करना, जो allowed नहीं है।
  3. यह मान लेना कि हर compiler VLAs support करता है, जब वे C11 में optional हो गए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.