-5

I created an array with storage space allocation like:

int* geom = new int[50];

And I printed the "values" of geom from -1000 to 1000 and I expected to get "random" values since I haven't initalised any value of array to anything. However, I got a lot of zeros and random numbers at certain places of which values and positions stayed exactly the same after each compilation and execution.

`
...
value of geom[-2] :209
...
value of geom[43] :0
value of geom[44] :0
value of geom[45] :0
value of geom[46] :0
value of geom[47] :0
value of geom[48] :0
value of geom[49] :0
value of geom[50] :1041
value of geom[51] :0
value of geom[52] :1970037110
value of geom[53] :1718558821
value of geom[54] :1868916512
value of geom[55] :892689261
value of geom[56] :943333469
value of geom[57] :858993460
value of geom[58] :808858675
value of geom[59] :2570
value of geom[60] :0
value of geom[61] :0
...
value of geom[310] :60609
`

As you can see especially for the "negative side" I got only zeros and 209 for the [-2]. So it means it is not a random type of thing.

I would like to know is there a "default" constructor type of thing if we don't use std::fill_n to fill the values for our array? And if it exists how does it know which until which point should it fill the array since I got not-randomly distributed values in a range of 2000 cells.

I got random values for each execution and compilation when I declared an array like: int geom[50] (Still there was a significant amount of zeros though)

I am using Ubuntu 18.04 by the way.

Orkhan S.
  • 7
  • 2
  • 8
    *"I expected to get "random" values"* It's undefined behavior. Anything can happen. It doesn't mean that it will behave different every time and doesn't guaranties any kind of randomness. You are incorrect in having any expectations at all about undefined behavior, other than it being undefined. – François Andrieux Apr 23 '19 at 18:07
  • It sounds like you are asking if there is some method of "uninitializing" a variable. Is this what you are asking for? – François Andrieux Apr 23 '19 at 18:09
  • Here I printed only the values which are not zero. So it is not quite random behavior since it has a huge proportion of zeros. – Orkhan S. Apr 23 '19 at 18:10
  • 2
    `geom[-2]` is not a valid array index. Attempting to read or write it invokes Undefined Behaviour (same goes for reading valid but uninitialised indices) This means your entire program is now meaningless and the compiler has *no* obligation to generate any specific behaviour - literally *anything* goes; from a crash, to removing all your code, to doing what you expect, to writing garbage to your harddrive. All are acceptable outcomes once your code contains UB and you have *no guarantees*. The compiler is also not obligated to warn you when you engage in UB, it can just assume you don't. – Jesper Juhl Apr 23 '19 at 18:11
  • 1
    @OrkhanS. Yes, that's what I mean by undefined behavior doesn't guarantee any kind of randomness. It's not random, it's undefined. The observed behavior may or may not change at any time for any reason. There is no reason to expect there to be many zeroes, and there is no reason to expect there to be few zeroes. – François Andrieux Apr 23 '19 at 18:11
  • Thank you all for the clarification. I think I shouldn't do "crazy" experiments with codes anymore. – Orkhan S. Apr 23 '19 at 18:15
  • Beside the fact that it is undefined and that you shouldn't assume anything... I think this (https://stackoverflow.com/a/197699/6571309) could be the reason why [-2] is different. But I am not sure. It has to do with the implementation of delete[] – rkouye Apr 23 '19 at 18:15
  • 1
    Do crazy experiments, it is a good way to learn. If not, you would have always assumed that it was random. – rkouye Apr 23 '19 at 18:21
  • How about crossing the street on the red light and expecting to be hit by a car? The C++ language rules usually work by guaranteeing an outcome when you follow them, rather than specifying what happens when you disobey. – SergeyA Apr 23 '19 at 18:27

1 Answers1

2

geom[-2] is not a valid array index here. Attempting to read or write it invokes Undefined Behaviour (same goes for reading valid but uninitialised indices).

This means your entire program is now meaningless and the compiler has no obligation to generate any specific behaviour - literally anything goes; from a crash, to removing all your code, to doing what you expect, to writing garbage to your harddrive. All are acceptable outcomes once your code contains UB and you have no guarantees.

The compiler is also not obligated to warn you when you engage in UB, it can just assume you don't and generate code based on the assumption that "of course you didn't break the rules" and if you then did break the rules anyway, you get to keep whatever broken mess results.

Any behaviour you observe from code with UB can change the next time you run the application (or not), when you change or upgrade your compiler, when you change OS, and for many other reasons. You have left the realm of well defined program behaviour and can have no expectations about the result of your program.

See also:

https://blog.regehr.org/archives/213

https://blog.regehr.org/archives/226

https://blog.regehr.org/archives/232

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html

This is also related: Can a local variable's memory be accessed outside its scope?

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • This is not true. Generally speaking, negative array indexes are not invalid by themselves. – SergeyA Apr 23 '19 at 18:29
  • @SergeyA can you elaborate on what is "not true"? What I attempted to say was that 1) `-2` is not a valid index here and reading or writing it is UB. 2) reading any other (valid) index is also UB if the value at that index has not been initialized. Did I not make that clear or do you disagree? If so, why/how? – Jesper Juhl Apr 23 '19 at 18:33
  • 2
    In the language, a negative index is valid ([Are negative array indexes allowed in C?](//stackoverflow.com/q/3473675) - it's a C question, but applicable). However, in the current question, it is not valid (`geom[-2]`). (Unless, in some hidden code, OP has changed what `geom` points to (i.e. `geom += 2;`)). – 001 Apr 23 '19 at 18:46
  • The way I read your answer (previous to the edit) was that any negative index is invalid. With later edits this reading is not supported anymore. – SergeyA Apr 23 '19 at 19:05
  • @SergeyA I'm glad my edits made it more clear. Thanks for pointing out the answer could be improved. – Jesper Juhl Apr 23 '19 at 19:07