1
int main()
{
    clrscr();
    int x[3];
    int n;
    cout<<"Enter the array size= ";
    cin>>n;
    cout<<"Enter the elements for array= ";
    for(int i=0;i<n;i++)
    {
        cin>>x[i] ;
    }
    for(i=0;i<n;i++)
    {
        cout<<"x["<<i<<"]="<<x[i]<<"\n";
    }
    getch();
    return 0;
}

When m trying the same logic in c# then I got the right output as if I enter the size of array more than I initialize it gives the exception. But in c++ m not getting any type of error neither on compilation time nor at run time. But according to rule it should be give some error at run time in output if i give array size more than I initialize. And one thing more why it determine the 09 as two numbrs not single as 90 it shows it 0 and 9 at differect index as in output.

output

enter image description here

4
  • 7
    Format your post please. Three months is long enough to have learned how to post questions. Commented Jun 12, 2011 at 18:48
  • sorry i tried but due to network problem it could not be. Commented Jun 12, 2011 at 18:59
  • 1
    Network problems? If you could write the post with broken formatting, you could write it with non-broken formatting. As far as I can tell, you've never formatted code in your posts; someone else has always done it for you. Commented Jun 12, 2011 at 19:01
  • 2
    This is a reasonably low-quality question, but I don't see how it's "ambiguous, vague, incomplete, overly broad, or rhetorical" or "cannot be reasonably answered in its current form". It's quite clear what the question is. Commented Jun 12, 2011 at 19:08

2 Answers 2

4

If you have an array:

char array[3];

And you try to write into an element that doesn't exist:

array[15] = '!';

Then this is an error. However, a compiler is not required by the C++ Standard to diagnose this error, and most do not. This is because it would require computation every time you accessed the array to determine whether you were within the array bounds.

Instead, it is up to the programmer to ensure that he/she uses the array correctly, by writing these bounds checks his- or herself. Then the checks are only performed when the programmer has deemed them necessary, and there are no wasted computations.

So:

std::cin >> n;

if (n > 3)
   throw std::runtime_error("OMG not enough space in my array!");
Sign up to request clarification or add additional context in comments.

11 Comments

+1. To get bounds checking, switch to std::vector and index with at rather operator[].
@larsmans: If he switches to std::vector, he might as well allow as many elements as he likes; no bounds-checking needed.
and what about the nos. as I insert 09 then it show 0 and 9 on seprate indexes and on 7th index there should 0 why is 6 there according to output.
@avirk: Why should the 7th element be 0? There is no 7th element. You're reading from invalid memory. The issues you have with items not being what you expect is all because you're writing and reading from invalid elements.
If there is no 7th element according to the compiler then why it prints the 4th index 78?
|
0

C++ and in-turn C are not required to throw an exception when you over-run an array. Arrays are simply memory blocks, and when you over-run them, the only time you will encounter an error is when you have exceeded the bounds of what the operating system run-time allows you to access as a user in memory. For instance, if you over-ran your array, and wrote over the values for the return pointer for your function, and then when the function returns, it tries to place in the instruction pointer register a bogus return address, the OS itself will throw a segmentation fault because you tried to execute code beyond a memory address you were allowed. Segmentation faults and bus error messages though are OS-specific, not specific to C or C++ (i.e., you'd get the same errors with assembly).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.