0

enter image description herei created array of 5 elements. in which i'm supposed to be able to index only 0 - 4. But why am i able to initialize the 5th index in this case?

Eyal Gerber
  • 1,026
  • 10
  • 27
macjayz
  • 47
  • 7
  • 4
    last index is `4`, not `5`. – kiran Biradar Dec 22 '22 at 11:44
  • yes but check out the image, i'm able to initialize the 5th index, why? – macjayz Dec 22 '22 at 11:45
  • 5
    Accessing element at index other than `0-4` is *Undefined Behavior*. The compiler is free to do whatever it wants. Including ignoring it entirely. – tstanisl Dec 22 '22 at 11:45
  • 1
    It's because c permits buffer overflows. It's a major source of bugs and security flaws in c programs. While c is fast, it does expect programmers to write good programs which don't do this. Sometimes, it might appear to work, other times it may incurr faults, and sometimes you may get a segmentation fault. – Simon Goater Dec 22 '22 at 11:48
  • @tstanisl. so you mean i can initialize any index outside 0-4 without getting an error from the compiler? – macjayz Dec 22 '22 at 11:48
  • 2
    @macjayz yes you can. But since it is UB anything can happen and your program will be unusable. – wohlstad Dec 22 '22 at 11:50
  • @SimonGoater. yes i was actually expecting a segmentation fault, but i'm kinda surprise and confused that it went ahead to compile without any error – macjayz Dec 22 '22 at 11:51
  • 1
    @macjayz segmentation fault is a runtime error, not compile time. And code invoking UB can indeed cause seg-fault, but it might also not cause it. With UB anything can happen. – wohlstad Dec 22 '22 at 11:52
  • 1
    @macjayz, you can force some feedback from the compiler by using sanitizer. Just use `-fsanitize=undefined` for GCC or CLANG – tstanisl Dec 22 '22 at 11:52
  • 1
    It's a bit poor that the compiler doesn't even realise there's a problem with a static index like yours. – Simon Goater Dec 22 '22 at 11:54
  • 2
    It is a very bad idea to post code and program output as pictures. Code should always be included in the question as text. It is really important. – Vladimir F Героям слава Dec 22 '22 at 12:00
  • @SimonGoater, i'm kinda new to C so this seem weird to me. but thanks for you help. – macjayz Dec 22 '22 at 12:00
  • 2
    You should take away a valuable lesson in C: you can't prove the absence of undefined behavior in C programs by compiling and running them. – Paul Hankin Dec 22 '22 at 12:01
  • @VladimirFГероямслава thanks for your contribution, i'll do that next time – macjayz Dec 22 '22 at 12:02
  • @PaulHankin, yes i've taking lot of tutorials but most of the times they don't explain things in details. – macjayz Dec 22 '22 at 12:03
  • 1
    But they probably told you not to do it. That must be sufficient for most situations. – Gerhardh Dec 22 '22 at 12:06
  • [What is undefined behavior and how does it work?](https://software.codidact.com/posts/277486) Ironically, one of the code examples in that question is very close to this code here. – Lundin Dec 22 '22 at 13:53

2 Answers2

1

A good answer for this may be found here: https://stackoverflow.com/a/70276640/4441211

In the link, the answer refers to the use of pointers. In your case you are writing n[5]=1, which is essentially writing *(n+5)=1, which is also pointers.

Eyal Gerber
  • 1,026
  • 10
  • 27
  • 1
    If another SO question answers this question, the correct approach is to flag/close vote this question as a duplicate. – Lundin Dec 22 '22 at 13:52
0

How can it be that I can write/read to/from beyond the defined size of an array in C language?

Defining an array or any other object reserves memory for it.

It does not create any checks on source code that you write to ensure that your source code stays within that reserved space. (There are often some checks built into the operating system, but they operate on units of memory pages, not on individual objects inside your program.)

Reserving memory is merely an accounting arrangement. Your process has a bunch of memory, and the C language gives you a lot of freedom to access it. When memory is reserved for one purpose, the C implementation will not, by itself, use it for any other purpose. It is up to you to write code that uses memory correctly.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312