-1

What happens, If I reference uninitialized variable? like,

void func(int *p)
{
  // My operation
}

int main()
{
  int a;
  func(&a);
}
  • 3
    Taking the address of an uninitialized variable is fine. Trying to read from the address is undefined behavior. – Barmar Apr 08 '17 at 07:25
  • 1
    So it depends on what `func()` does. If it assigns through the pointer it's OK, if it reads it first it's wrong. – Barmar Apr 08 '17 at 07:26
  • Hopefully you will get a compiler warning that you shouldn't ignore. – DisappointedByUnaccountableMod Apr 08 '17 at 07:27
  • 1
    The code as shown is perfectly fine. – alk Apr 08 '17 at 07:28
  • read http://stackoverflow.com/a/1597426/6764663 for more info – Tanuj Yadav Apr 08 '17 at 07:34
  • @Barmar, to satisfy my curiosity, where does the standard say that reading from an uninitialized variable _in itself_ is UB? (Any use of the value can _result_ in UB by the program, but the reading???) – Paul Ogilvie Apr 08 '17 at 07:44
  • @PaulOgilvie Sorry, I was simplifying. Reading it returns an indeterminate value, which could potentially be a trap representation. – Barmar Apr 08 '17 at 07:46
  • @PaulOgilvie How do you read a value without using it? – Barmar Apr 08 '17 at 07:48
  • @Barmar, "use" means to use it further computations. Just reading it is not using it in further computations. Of course that is useless, but we agree that it cannot result in UB. – Paul Ogilvie Apr 08 '17 at 07:50
  • @PaulOgilvie Does assigning it to another variable count as a use? – Barmar Apr 08 '17 at 07:52
  • @PaulOgilvie: "*where does the standard say that reading from an uninitialized variable in itself is UB*" Please see the C11 Standard's annex ["*J.2 Undefined Behaviour*"](http://port70.net/~nsz/c/c11/n1570.html#J.2) – alk Apr 08 '17 at 07:57
  • @Barmar, we're getting to the philosophical level :-) Where does UB begin? When indexing an array with it to write, or to read from it and assume you read celcius, or whatever. So what types of UB are there? Compiler UB, program UB, what more? – Paul Ogilvie Apr 08 '17 at 07:58
  • The best that can be said was provided by @alk - it's fine as it is. Like moslty everything, it can be misused:( – ThingyWotsit Apr 08 '17 at 08:02
  • @alk, thanks for pointing me to a 200 bullet point list. Any more detailed pointer? – Paul Ogilvie Apr 08 '17 at 08:05
  • @paulogilvie: ... the 11th bullet. – alk Apr 08 '17 at 09:33
  • @alk, thank for the detailed pointer. I appreciate it. It brings us back to "use" and whether accessing the variable is "use" and hence whether that sole access can result in UB. It seems to leave free a compiler inserting a trap that triggers some (software) interrupt upon use, or even upon access. However, I remember a case where the indeterminate value was used as a randomiser. – Paul Ogilvie Apr 08 '17 at 12:10
  • @PaulOgilvie: The C Standard does not talk about using a "*variable*" but about using its "*value*". Reading a variable in my opinion definitely makes use of its value. – alk Apr 08 '17 at 12:14
  • 1
    @alk, found this: http://stackoverflow.com/questions/31739792/is-uninitialized-local-variable-the-fastest-random-number-generator/31746063#31746063, where clang removed a loop that used an unitialized automatic variable. However, there the value was used. Thank you for the discussion. – Paul Ogilvie Apr 08 '17 at 12:16

1 Answers1

2

What happens, If I reference uninitialized variable

func() receives the address of the variable a as defined in main(). Inside func() the pointer pa defined by func(int * pa) points to the memory holding the indeterminate value of a.

func() may assign to a by doing

*pa = 42;

which would set a to 42.

If func() did

int b  = *pa;

it reads uninitialised memory, namely the indeterminate value of a, and this would invoke Undefined Behaviour.

From the C11 Standard (draft):

J.2 Undefined behavior

1 The behavior is undefined in the following circumstances:

[...]

  • The value of an object with automatic storage duration is used while it is indeterminate
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alk
  • 69,737
  • 10
  • 105
  • 255