Array takes more values than it is defined for

I started using arrays just a week ago, and I'm running into some problems .While inputting an array using the for loop, I made the initialiazer variable in the loop larger than the amount of spaces in my array . And I printed it that way . Surprisingly, it worked. Shouldn't it just ignore the extra ones?
And it prints a smiling face after the array,even if the amount of letters are within the range of the array.
(I'm inputting using cin instead of a for loop cuz its so long.)
Here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main() 
{ 
char x[3];
cin>>x;
int a;
for (a=0;a<9;a++)
{cout<<x[a];
}
return 0;
}

Thanks!
Last edited on
Hi,

The compiler warnings, using cpp.sh with all 3 warnings turned on.

 In function 'int main()': 
11:11: warning: iteration 3u invokes undefined behavior [-Waggressive-loop-optimizations] 
10:1: note: containing loop


If you have warnings, then you have problems :+)

It's Undefined Behaviour (UB) to print values past the end of your array - it could just print whatever happens to be there in memory, or it might do something else. When I run on cpp.sh it just prints 3 chars.

So, don't do UB :+)
Nope,no errors .Runs perfect. And the smiley face,irrespective of the number of characters I entered.
Basically, you are lucky. You do not know what your program will do because you are invoking undefined behavior by printing beyond the end of your array. It may do what you want. It may crash. It may format your hard drive. It may not even be consistent; the behavior could be dependent on the system time or the current phase of the moon.

So in a sense, it is not surprising that your program appeared to work. It would also not be surprising if it failed on a different machine, or even later on the same machine; invoking undefined behavior means the compiler may do whatever it likes.
Nope,no errors


But warnings though? If you don't have warnings, then you don't have the warning level high enough. Warnings are your friend, they tell where things are likely to go wrong. Use the compiler to help you do your work. For me, if I have warnings then I still have problems, and am not finished until all the warnings are gone. That only leaves run-time errors for which I use a debugger if needed.
x[a] is shorthand for *(x+a) which is shorthand for "the contents of this piece of memory".

C and C++ give you the power to simply read and write any piece of memory you like. The operating system will stop you if it doesn't like what you're doing (i.e. if you're doing it to a piece of memory that the OS did not allocate for your process), but C and C++ trust you with this power.

This is a great power that can allow you to do things quickly and efficiently. As with all great power, the onus is on you to understand it and to use it wisely and safely.
Okay.. I got some stuff about undefined behavior and warnings.

1)How do I increase the warning level ?(Using Dev C++)
2)The undefined behavior is consistent everytime . It prints 9 letters of the array (because in the loop a<9) and a few spaces ,and then a smiley face. Tried it in Turbo C++,same response.

Would you expect different behaviour each time?
1)How do I increase the warning level ?(Using Dev C++)


Are you using DevC++ on windows? I guess it uses MinGw as it's compiler, which is based on gcc. Here are some extra warnings to enable:

http://www.cplusplus.com/forum/general/183731/#msg899203

You could at least compile with -Wall -Wextra -pedantic-errors

I have no idea how or where to change settings in DevC++ to do this, but I am sure you can figure that out :+)
Topic archived. No new replies allowed.