i just want to know if subscript is greater than the size of array then either this value is stored outside of the memory located to array or sometimes do not saved and poduces error
can you tell how and by whom this is decided what action to be taken

Dani AI

Generated

Short answer: what happens is set by the language rules and by the implementation (compiler/runtime/OS). In C and C++ the standard does not define behavior for indexing outside an array (that is, it is undefined behavior). Managed languages such as Java and Python perform bounds checks and will raise an exception for an invalid index. This is why the exact symptom differs between platforms and languages; the formal category for C/C++ is described here: Undefined behavior (cppreference).

Why results vary in practice: compilers and optimizer passes assume well-formed code and may generate code that breaks if the program relies on out‑of‑bounds accesses. The operating system only intervenes when a memory protection fault occurs; otherwise an incorrect access can corrupt program state silently. For reliable detection during development, run memory tools such as AddressSanitizer or Valgrind: AddressSanitizer (LLVM) and Valgrind quick start.

Practical recommendations: prefer bounds-checked APIs (for example, use std::vector::at in C++ when you need checks), enable sanitizers (-fsanitize=address and -fsanitize=undefined) and run tests under them, use static analysis and assertions, and treat raw array indexing in C as fragile. In this thread and pointed to the observable outcomes, asked who decides, and summed up the danger with a joke — the authoritative answer is: the language spec plus the implementation and runtime. For avoidance and debugging use the tools above and favor safer abstractions.

Recommended Answers

All 3 Replies

> can you tell how and by whom this is decided what action to be taken
Yes, the troll under the bridge eats your code.

actualy,
if the subscript is greater than the size of the array
2 things can happen:
1. the value will be storred in the next memory location, calculated by the index, so if v[10] is a vector, than v is the address of its first element, and v is *(v+i) so v[11] is *(v+11). if in that location is another variable, its value will be overwritten.
2. it will produce a segmentation fault or something like that if that memory location cannot be written, that is it is outside the zone allocated for your process

Doublex is prety Right

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.