By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

C++ array

P: 2
Dear sir,

I have problem C++ array(with respect windows operationg System)

When i declare int a[5]. It must get value a[0],a[1],..a[4]. But it is getting upto a[10}.

why is it so?


Thanks lot
Nov 11 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
Dear sir,

I have problem C++ array(with respect windows operationg System)

When i declare int a[5]. It must get value a[0],a[1],..a[4]. But it is getting upto a[10}.

why is it so?


Thanks lot
memory is allocated to elements array a[0] to a[4]. However, C and C++ (unlike Java) have no array bounds checking so a program may address elements outside the array, e.g.
x=a[5];
a[6]=10;
a[10] =z;
etc. This will read or write memory outside the array bounds which could give incorrect data or corrupt other variables, etc. What happens depends on the compiler/operating system, etc. The program may
(1) just carry on with corrupt data and appear to work OK (it then crashes months later when run with different data)
(2) give errors later on when the corrupt data is accessed which are difficult to track down.
(3) may give a memory segmentation error.

It is generally advised to use <vector> in C++.
Nov 11 '06 #2

P: 2
memory is allocated to elements array a[0] to a[4]. However, C and C++ (unlike Java) have no array bounds checking so a program may address elements outside the array, e.g.
x=a[5];
a[6]=10;
a[10] =z;
etc. This will read or write memory outside the array bounds which could give incorrect data or corrupt other variables, etc. What happens depends on the compiler/operating system, etc. The program may
(1) just carry on with corrupt data and appear to work OK (it then crashes months later when run with different data)
(2) give errors later on when the corrupt data is accessed which are difficult to track down.
(3) may give a memory segmentation error.

It is generally advised to use <vector> in C++.

Thank You sir.
Nov 23 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.