473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Unexplainable behaviour of array

Parul Bagadia
188 New Member
hello ppl,
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. int main()
  3. {
  4.     int number=0;
  5.     int A[2]={0};
  6.           cout<<"Enter the number of elements you want in SET A"<<endl;
  7.     cin>>number;
  8.     for (int i=0;i<number;i++)
  9.     {
  10.         cout<<"Enter "<<i+1 <<" element"<<endl;
  11.         cin>>A[i];
  12.     }
  13.     return 0;
  14.  
  15. }
Here i know that as i have taken array size as 2 if value of number increases than that of the value of i,program should crash down, but whtas happening here is, if i enter the value of i as 4 its asking for the value of elements 8 times ; when i put the value of i as 3 its asking for value for 9 times, when i=8, elements askes=4, when i=5 elements asked=6.....
I debugged the program but its invain...
Actually here what i want to know is when i have given the limit of array as 2, first of all after 2 elemnts it should crash down; but then its taking garbadge value of i that's explainable to bit extent... but once the for loop is over for e.g when i take value of i as 4, how its getting executed 8 times??
Dec 16 '08 #1
3 1257
gpraghuram
1,275 Recognized Expert Top Contributor
If you specify the size of array as 2 and copy over 5 elements it is array overwrite.
You wont see the crash directly.
If the array overwrites the memory occupied by your program it wont crash . But if it tries to touch anyother process's memory then you will see the crash.


Raghu
Dec 16 '08 #2
Parul Bagadia
188 New Member
@gpraghuram
Thanx.
But what's happening in my program is if i take the value of number variable as 4 , the for loop gets executed 8 times this should not happen even if array is getting overwritten right?
Dec 16 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
No as soon as you over-write the bounds of the array you invoke undefined behaviour. Undefined behaviour is... well... undefined :D That means anything could happen including a program crash, the program running as expected (this time) or extra iterations of the loop.

There is little point trying to explain undefined behaviour as it may not be the same 2 times in a row.

However in your case a simple guess would be that when you write outside the bounds of your array the next bit of memory you write to is the memory containing number so number is changed and you end up entering a different number of numbers to that requested.
Dec 16 '08 #4

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

Similar topics

0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
5
by: Levent | last post by:
Hi, When compiled with gcc 3.3.3 and lower on various systems (tried cygwin, linux, aix) the following code behaves strangely: #include <iostream> class Foo { public: typedef int subs;
12
by: RoSsIaCrIiLoIA | last post by:
On Mon, 07 Feb 2005 21:28:30 GMT, Keith Thompson <kst-u@mib.org> wrote: >"Romeo Colacitti" <wwromeo@gmail.com> writes: >> Chris Torek wrote: >>> In article <4205BD5C.6DC8@mindspring.com> >>>...
8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
5
by: srinivas.satish | last post by:
Hi, i checked this piece of code with both gcc as well as vc++ compilers. Behaviour is same. I don't understand the behaviour. Can some1 please explain the reason of the result. ...
19
by: Sharath A.V | last post by:
I had an argument with someone on wheather this piece of code can invoke undefined bahaviour. I think it does not invoke any undefined behaviour since there is sufficient memory space of 9...
24
by: temper3243 | last post by:
Hi, Many people have used this code in my project. It works because b is using the extra memory for other 4 variables.I access obj max. well here are a few questions 1) Where does it fail. I...
4
by: Marc R. | last post by:
Hi there, consider the following code: #include "stdafx.h" int main(int argc, char* argv) { const int Xres = 600; const int Yres = 600; std::pair<float,floatdispFNabla;...
18
by: andreas.luethi | last post by:
Hi All Compile a our source with BM XL C/C++ Enterprise Edition V8.0 for AIX (Version: 08.00.0000.0000) produces a lot of Informational message like this: "aaalib.c", line 671.1: 1506-412 (I)...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.