1

Can we access the mth array element, if it has m elements? I mean that, if the array has 7 elements, was it ever possible to store any value in array[7]? But the array indexes start from 0 and end with 6, when length is is 7.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Azhagu Surya
  • 132
  • 1
  • 1
  • 17
  • I'm not a C developer, but I happen to mostly know the answer to your question. Strictly speaking, assigning `array[7]` in an array of size 7 is wrong, but it may be tolerated at runtime. As @Mohit mentioned, this operation is not well defined, and you don't know what outcome it could have. – Tim Biegeleisen Jun 22 '16 at 05:05
  • but it shows exact ouput. – Azhagu Surya Jun 22 '16 at 05:15
  • please go to this link, – Azhagu Surya Jun 22 '16 at 05:15
  • http://code.geeksforgeeks.org/index.php – Azhagu Surya Jun 22 '16 at 05:15
  • 1
    It may work, it may partially work, or you may get a segmentation fault. Think of accessing an array outside of bounds in C as driving drunk without wearing a seatbelt. You may careen down the road and reach home, oblivious to the peril, or you may get into an accident. Best practice is not to do it :-) – Tim Biegeleisen Jun 22 '16 at 05:16
  • @AzhaguSurya The link you post you should check it as it does not have any code related to this . And second thing , it could work as you expected as it is undefined behaviour . And compiler will not warn you about this . – ameyCU Jun 22 '16 at 05:19

5 Answers5

5

No, accessing the element beyond the array bound is undefined behaviour. Taking address of the element 1 beyond last array element is well defined.

To understand the implications, consider reading How dangerous is it to access an array out of bounds?

Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • pleae go to this link, i am really getiing confused, the program doesnt show any error or warning msgs reagrading that issues.... – Azhagu Surya Jun 22 '16 at 05:12
  • @AzhaguSurya You're assuming there should be an error or warning message. Some compilers will be nice and give you a warning *if* they can figure out there's a problem, but most won't. Also your link doesn't go to any code. – user253751 Jun 22 '16 at 05:42
  • [Undefined behaviour may or may not result in warnings](http://stackoverflow.com/questions/37942101/why-doesnt-the-compiler-warn-you-if-there-is-possible-undefined-behaviour/37942242#37942242). It is like if one jumps with parachute, he will be saved. If he jumps without one, the behaviour is undefined. Sometimes he may survive, and sometime not. Or may be the shock may convert him into a superhuman (who knows) – Mohit Jain Jun 22 '16 at 05:48
3

You answer your own question -

was it ever possible to store any value in array[7]? But the array indexes start from 0 and end with 6, when length is is 7

As you see for array of length 7 , possible indexes are from 0 to 6. Array is allocated that much memory on stack. If you try to access array[7] then you would be accessing memory past the array which is un-initialized and un-authorized and will cause undefined behaviour.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
3

No, You are only allowed to access elements with indexes from 0 up to size-1. Anything outside of that range causes undefined behavior. If the index was near the range, most probably you read your own program's memory. If the index was largely out of range, most probably your program will be killed by the operating system.

msc
  • 33,420
  • 29
  • 119
  • 214
1

Never try to access out-of-bounds array elements. It has undefined behavior.

Though, if you are a damn crazy man, you may try something like this:

int ar[7];
int tmp = ar[7];
ar[7] = 8;
std::cout << ar[7] << std::endl; // the output is 8, as you can guess
ar[7] = tmp;

If you are lucky enough, this will not cause any error, since you put the initial value of ar[7] back. If you don't, MSVS, in the end of the function, will generate a runtime error with the message:

Stack around the variable 'ar' was corrupted

Don't know about other compilers.

RedRidingHood
  • 568
  • 3
  • 16
0

Given char foo[4], memory will be allocated like this:

foo+0  foo+1  foo+2  foo+3  foo+4
  |      |      |      |      |
  |first |second|third |fourth|

with the first element nestled between foo+0 and foo+1, the second between foo+1, and foo+2, the third between foo+2 and foo+3, and the fourth between foo+3 and foo+4. The address foo+4 is a perfectly fine address, but the fourth element immediately precedes it (likewise the first element is immediately before foo+1, the second before foo+2, and the third before foo+3.

Most code identifies elements by their starting address, but sometimes it may be useful to have code which operates upon the element immediately preceding a pointer. For example, if a software-stack pointer points at the next location where an item should be written, then popping the stack should return the item before the address given by that pointer. If the stack used byte-sized objects and was initialized to foo, then after four single-byte items have been pushed the pointer would be equal to foo+4, indicating that the next item to be popped should be the fourth one.

supercat
  • 77,689
  • 9
  • 166
  • 211