Array Bound Checking Problem in C Language with Example

Array Bound Checking is an important concept in C programming. In C language, arrays have a fixed size, but the compiler does not automatically check whether an array index is within its valid range during ordinary array access. If a programmer accidentally writes a loop that accesses array elements beyond the declared array size, the program may read or modify memory outside the array. This can cause unexpected results, memory corruption or program crashes. Accessing an array outside its valid bounds results in undefined behavior in C.

C लैंग्वेज में Array Bound Checking एक महत्वपूर्ण कॉन्सेप्ट है। C लैंग्वेज में Array का एक निश्चित Size होता है, लेकिन सामान्य Array Access के दौरान कंपाइलर अपने आप यह चेक नहीं करता कि Array Index उसकी निर्धारित सीमा के अंदर है या नहीं। यदि प्रोग्रामर गलती से ऐसा लूप लिख देता है जो Array के निर्धारित Size से अधिक Elements को Access करता है, तो प्रोग्राम Array की मेमोरी सीमा के बाहर भी डेटा को Read या Modify कर सकता है। इससे गलत परिणाम, Memory Corruption या प्रोग्राम Crash हो सकता है। C में Array की निर्धारित सीमा के बाहर Access करना Undefined Behavior कहलाता है।

Example: Array Bound Checking Problem in C

The following program declares an array of five elements but attempts to access 500 elements. This is an incorrect program and demonstrates an array boundary violation.

निम्न प्रोग्राम में पाँच Elements का Array डिक्लेअर किया गया है, लेकिन इसमें 500 Elements को Access करने का प्रयास किया गया है। यह एक गलत प्रोग्राम है और Array की सीमा का उल्लंघन दर्शाता है।

#include <stdio.h>
int main(void)
{
int list[5], i;
printf("Enter values of list array:\n");
for (i = 0; i < 500; i++)
{
scanf("%d", &list[i]);
}
printf("Values of list array:\n");
for (i = 0; i < 500; i++)
{
printf("list[%d] = %d\n", i, list[i]);
}
return 0;
}

Explanation:

  • The array list[5] can store only five integer elements.

  • Its valid indexes are 0, 1, 2, 3 and 4.

  • However, the loop attempts to access indexes from 0 to 499.

  • Accessing indexes beyond 4 causes undefined behavior.

व्याख्या:

  • Array list[5] में केवल पाँच Integer Elements स्टोर किए जा सकते हैं।

  • इसके Valid Index 0, 1, 2, 3 और 4 हैं।

  • लेकिन लूप 0 से 499 तक के Index को Access करने का प्रयास करता है।

  • 4 से अधिक Index को Access करने पर Undefined Behavior उत्पन्न होता है।

This program has undefined behavior. Its output cannot be predicted reliably. It may crash, display incorrect values or appear to work in some executions. Do not execute it with real data.

इस प्रोग्राम का व्यवहार Undefined है। इसका Output निश्चित रूप से नहीं बताया जा सकता। यह Crash हो सकता है, गलत Values दिखा सकता है या कुछ परिस्थितियों में सही चलता हुआ दिखाई दे सकता है। इसे वास्तविक डेटा के साथ Execute नहीं करना चाहिए।

Correct Program: Accessing Array Elements Within Bounds

The programmer must ensure that the loop does not access indexes outside the declared array size. The following program correctly accesses all five elements.

प्रोग्रामर को यह सुनिश्चित करना चाहिए कि लूप Array के निर्धारित Size से बाहर के Index को Access न करे। निम्न प्रोग्राम सभी पाँच Elements को सही तरीके से Access करता है।

#include <stdio.h>
int main(void)
{
int list[5], i;
printf("Enter 5 values of list array:\n");
for (i = 0; i < 5; i++)
{
scanf("%d", &list[i]);
}
printf("Values of list array:\n");
for (i = 0; i < 5; i++)
{
printf("list[%d] = %d\n", i, list[i]);
}
return 0;
}

Output:

Enter 5 values of list array:
10
20
30
40
50
Values of list array:
list[0] = 10
list[1] = 20
list[2] = 30
list[3] = 40
list[4] = 50

Array Bound Checking in Different Programming Languages

Different programming languages handle array boundary violations differently.

अलग-अलग प्रोग्रामिंग लैंग्वेज में Array की निर्धारित सीमा से बाहर Access करने पर अलग-अलग व्यवहार होता है।

C LanguageJava
Ordinary array indexing does not automatically perform runtime bounds checking.Array indexing performs runtime bounds checking.
Out-of-bounds access results in undefined behavior.Out-of-bounds access throws ArrayIndexOutOfBoundsException.

C++ ordinary built-in array indexing also does not automatically check array boundaries. However, some C++ library containers provide checked access through functions such as at().

C++ के सामान्य Built-in Array Indexing में भी अपने आप Array Boundary Checking नहीं होती है। हालांकि, C++ के कुछ Library Containers में at() जैसे Functions के माध्यम से Bounds Checking की सुविधा उपलब्ध होती है।

In C language, array boundary checking is the responsibility of the programmer when using ordinary array indexing. Always ensure that array indexes remain between 0 and size - 1 to avoid undefined behavior and memory-related errors.

C लैंग्वेज में सामान्य Array Indexing का उपयोग करते समय Array की सीमा जाँचना प्रोग्रामर की जिम्मेदारी होती है। Undefined Behavior और Memory से संबंधित Errors से बचने के लिए हमेशा सुनिश्चित करें कि Array Index 0 से size - 1 के बीच रहें।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025