Please refere the following program

#include<iostream.h>
#include<conio.h>

using namespace std;
int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
cin>>a[-1];
cin>>a[15];

cout<<a[-1]<<" " <<a[15];
getch();
}


To my surprise this code works perfectly ok. I thought it will give me some compiler error or runtime error as I am writing in the array index which is undeclared.

My question is
1. What this declaration a[10] means.
2. Is it that in C++ the array bounds are flexible?
3. Or is it a bug in C++

Speaking of referring to, refer to these before posting again.
http://www.daniweb.com/forums/announcement8-3.html
http://www.daniweb.com/forums/thread78223.html

1. What this declaration a[10] means.
It means an array of 10 ints - read your book again.

2. Is it that in C++ the array bounds are flexible?
Nope.

3. Or is it a bug in C++
Part of the premise of using C and C++ is that the programmer is always right, even if they are mistaken.


> I thought it will give me some compiler error or runtime error as I am writing in the array index which is undeclared.
True, but unlike other high level languages, there is no standard which states that you should always get a compile time, or run-time assertion. Tools like lint would spot such obvious mistakes at compile time, and Purify would also spot the same problem at run-time.

I would suggest you use std::vector if you want better out-of-bound detection within the standard language.

I know what a[10] means, that is why I was expecting some error. If C++ does not look for any bound then why it does not allow declaration like int a[]?
From my program it is clear that in C++ array bounds are not checked.
Is this intentional? or sime bug this is my question

Speaking of referring to, refer to these before posting again.
http://www.daniweb.com/forums/announcement8-3.html
http://www.daniweb.com/forums/thread78223.html

1. What this declaration a[10] means.
It means an array of 10 ints - read your book again.

2. Is it that in C++ the array bounds are flexible?
Nope.

3. Or is it a bug in C++
Part of the premise of using C and C++ is that the programmer is always right, even if they are mistaken.


> I thought it will give me some compiler error or runtime error as I am writing in the array index which is undeclared.
True, but unlike other high level languages, there is no standard which states that you should always get a compile time, or run-time assertion. Tools like lint would spot such obvious mistakes at compile time, and Purify would also spot the same problem at run-time.

I would suggest you use std::vector if you want better out-of-bound detection within the standard language.

This will work fine.
You are getting and printing values to/from unallocated memory. You didn't get any garbage value because no other process intervening.

This will work fine.
You are getting and printing values to/from unallocated memory. You didn't get any garbage value because no other process intervening.

In fact this is part of my project which gives me problem. Finally I found that somehow I was exceeding the bound limit. But I feel this is strange C++ such a old language is asking us to specifically declare the array with its size and not keeping any control on the bound check.
May be intentionally C++ designers have kept the bounds flexible. I just wanted to know the reason.

> This will work fine.
> You are getting and printing values to/from unallocated memory. You didn't get any garbage
> value because no other process intervening.
What makes you think that?
As soon as you step off the bounds of the array - FOR ANY REASON - the whole program becomes undefined. The fact that your particular test doesn't crash, and produces a plausible answer means nothing. It could equally have rebooted the machine for all you know about the subject.

> I know what a[10] means, that is why I was expecting some error.
Use lint then. The compiler's job is to translate syntactically valid programs. Not second-guess the semantics of what you actually wrote.

int array[10];
int *p = &array[5];
p[-1] = 42;

You can't even just assume that any -ve subscript is immediately a bad idea.

> From my program it is clear that in C++ array bounds are not checked.
Correct. C and C++ favour performance and the assumption of skilled practitioners. If you're not capable of writing such code, pick another language which nurse-maid's your code every step of the way.

It is of course a flawed assumption, because even the most skilled people screw up when the programs get large enough. Which is why we have lots of extra tools to help find such problems.

> May be intentionally C++ designers have kept the bounds flexible.
But arrays are NOT flexible (not at run-time at least). The array size you choose must be a compile-time constant. If you had array bound checking, but not integer overflow checking, would you be complaining about that at some point?

C was originally written on a PDP-11 with 32K words of memory, and a handful of programmers. At that time, there was no need (and no room) to indulge in luxuries like bounds checking. Hell, original C didn't even have prototypes for functions.

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.