0

I created this small piece of software in C:

#include <stdio.h>
#include <stdlib.h>

void print_this_list(int size){
    int list[size];
    for (int i = 0; i < size; i++) {
        printf("%d\n", list[i]);
    }
}

int main(int argc, char *argv[]){

    print_this_list(25);
    return 0;

}

The results of the execution are very interesting (apparently) random numbers:

-1519340623
859152199
-1231562870
-1980115833
-1061748797
1291895270
1606416552
32767
15
0
1
0
104
1
1606578608
32767
1606416304
32767
1606423158
32767
1606416336
32767
1606416336
32767
1
Program ended with exit code: 0  

What exactly are those numbers and what is the "logic" behind them?

  • 4
    garbage values, it can be anything!! **undefined behaviour**!! – Nitin Tripathi Sep 27 '15 at 09:56
  • 1
    Well, to be precise, it's **indeterminate**, which the standard defines as either unspecified (that is, a valid, but arbitrary value) or a trap. As opposed to UB, which may in principle mean just about anything and everything, including random values or the optimizer stripping out the entire function, or even severely bad behavior. Worst thing that can happen according to the wording of the standard is a trap (that is, termination of program). – Damon Sep 27 '15 at 10:24
  • @Damon reading a trap causes UB though, therefore using indeterminate causes UB unless you can prove that there are no traps on the system. [More detailed answer](http://stackoverflow.com/a/25074258/1505939) – M.M Sep 27 '15 at 14:22
  • @M.M: It's true that the C standard does not define traps, but they are nevertheless well-defined. If a handler is installed (if one _can be_ installed, but I know no OS where you can't do it), the handler is called, otherwise the process is instantly terminated. So it really boils down whether the hardware has the ability to flag certain values as invalid (like NaTVal on Itanium) or not, in which case you'll have trap, and otherwise you will not have trap. But in no case will you have _undefined_ behavior. It's always well-defined what will happen. – Damon Sep 27 '15 at 14:28
  • All of that is not covered by the C standard though. It is UB as far as C is concerned . Implementations may implement UB however they choose – M.M Sep 27 '15 at 14:29
  • @M.M: "Undefined behavior" has a very specific, well-defined meaning. I means that the program can do just about anything. The computer could eat your cat, and it would be a legal thing to do. That is not the case here. It is specified that you get either an unspecified value, or a trap. The standard doesn't define what happens if you get a trap, but the compiler does not have _carte blanche_ to eat your cat. No demons may come flying out of your nose. – Damon Sep 27 '15 at 14:34
  • @Damon according to the C standard, demons could indeed fly out your nose here and eat your cat. It is covered by C11 6.3.2.1/2 (quoted in the link I gave earlier) – M.M Sep 27 '15 at 14:44

2 Answers2

9

No logic behind them .It's Undefined Behaviour

void print_this_list(int size){
int list[size];             // not initialized 
for (int i = 0; i < size; i++) {
    printf("%d\n", list[i]);             // still you access it and print it 
  }
}

list is uninitialized and you print it's contents (which are indeterminate) .Therefore ,you get some random garbage values as output.

In order to get working you need to initialize it . You can try this -

  void print_this_list(int size){
  int list[size];             
  for (int i = 0; i < size; i++) {
    list[i]=i;                 // storing value of i in it before printing        
    printf("%d\n", list[i]);             
    }
  }
ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • It's not *undefined behaviour!* Not this time! The value of an uninitialized variable is *indeterminate* and possibly a trap representation, no undefined behaviour occurs when accessing it though. – fuz Sep 27 '15 at 13:48
  • @FUZxxl Can you please point in standard where I can find about it more ? – ameyCU Sep 27 '15 at 14:07
  • It is undefined behaviour. Even if there is no trap, passing indeterminate values to library functions (which `printf` is) causes undefined behaviour. [See here](http://stackoverflow.com/questions/25074180/is-aa-or-a-a-undefined-behaviour-if-a-is-not-initialized/25074276#25074276) for discussion – M.M Sep 27 '15 at 14:28
  • @M.M Means I was correct at first place .That really confused me . Didn't know about trap . Thanks for clarification . – ameyCU Sep 27 '15 at 14:33
  • @FUZxxl reading a trap causes UB, and there may be UB on reading indeterminate values even if there are no trap representations (see link in my previous comment) – M.M Sep 27 '15 at 14:49
0

it's trash . uninitialized value

if you try

    int x; // i define x as uninitialized variable some where in the memory (non empty)
    printf("%d",x); // will print trash "unexpected value  
    // if i do this 
    int x=10;//i define x with initial value so i write over the trash was found in this block of the memory . 

and if i print out of range index as

  int ar[10];
  for(int i=0;i<10;i++)
      ar[i]=rand();
  printf("%d\n",ar[10]);//out of index uninitialized index it will print trash . 

i hope that's help