-4

enter image description here

I coded this and when I run on win10 (Code Blocks) the numnbers don't get organized by crescent order, but when I run it on my linux (ubuntu 14 - Code Blocks) its organized. Could someone tell me why? (the code is very simple and not much elaborated.

#include "stdio.h"
#include "stdlib.h"

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

  int i=0, j, num[2], aux;

  for (i =0; i<3; i++)
  {
      printf (" Enter a value for num %d ", i+1);
      scanf ("%d", &num[i]);
  }

  for (i =0; i<3; i++)

  {
      for (j=0; j<3; j++){
        if (num[j] > num[j+1])
        {
          aux = num[j];
          num[j] = num[j+1];
          num[j+1] = aux;

        }
      }
  }
  for (i=1; i<4; i++)
 {
      printf("%d", num[i]);
      printf("\n");
  } 


  system("PAUSE");
  return 0;
}
  • 4
    Num can hold only 2 values, you're accessing it past its end. – Mat Sep 29 '17 at 12:38
  • Wich error do you have? can you paste your output? – Oscar Sep 29 '17 at 12:39
  • `for (i=1; i<4; i++)` should properly be `for (i=0; i<3; i++)` – pzaenger Sep 29 '17 at 12:39
  • 1
    The `for` loops access `num[0]`, `num[1]`, `num[2]` and `num[3]`. Of thouse, only `num[0]` and `num[1]` exist; `num[2]` and `num[3]` do not exist, because you have declared `num` as an array of 2 `int` values. Accessing a variable which does not exist is undefined behaviour; in such a situation the computer is allowed to do whatever it wants, including transforming the sun into a supernova and, of course, giving different results on different operating systems. Please do not run programs which exhibit undefined behaviour, because I really do not want the sun to become a supernova. – AlexP Sep 29 '17 at 12:45
  • @Oscar I put the output, can you see? – Bruno Dumbra Sep 29 '17 at 12:47
  • There is no logic because **undefined behavior is undefined**. As soon as `i` is 2 in the first `for` loop the progam goes into undefined behaviour. – AlexP Sep 29 '17 at 12:50
  • @Bruno Dumbra What are these magic numbers 2, 3, 4?:) – Vlad from Moscow Sep 29 '17 at 12:54
  • 1
    @Bruno Dumbra Just don't run the program on win10. It has numerous bugs.:) – Vlad from Moscow Sep 29 '17 at 12:55
  • @VladfromMoscow, do you mean a sequence? :) – Bruno Dumbra Sep 29 '17 at 12:56
  • https://stackoverflow.com/a/25636788/841108 is very relevant – Basile Starynkevitch Sep 29 '17 at 12:59
  • @BrunoDumbra I mean the numbers hard-coded in the program. – Vlad from Moscow Sep 29 '17 at 13:00
  • [How dangerous is it to access an array out of bounds?](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – Lundin Sep 29 '17 at 13:00

1 Answers1

2

Your logic doesn't work, neither in Linux nor in Windows. Getting a correct result is a matter of a pure luck, as you're accessing memory past the end of the declared array (the first pair of loops uses index 2 and the last loop iterates up to index 4 while your int num[2] array has only two elements, num[0] and num[1]). This triggers a so called Undefined Behavior, which may result in anything, from returning correct results, through crashing the program, up to destroying it.

Edit

To add some insight, consider you local variables: i, j, num[] and aux. Such variables are usually located on a stack, often in the order of declarations (or exactly reversed). Simple variables, like i or aux may be allocated in some CPU registers, but that depends on the level of optimizations, made by a compiler.

So the variables' layout may look like this:

--------------------------------------------------------------
      |         |         |        |        |         | 
  ... |    i    |    j    | num[0] | num[1] |   aux   |  ...
      |         |         |        |        |         | 
--------------------------------------------------------------

That implies, when you reach an item of the num[] array with index one past the array's length, you most probably actually access the aux variable.

So as soon as you input the third value, you actually store it in aux, which will get overwritten immediately after with the first execution of

aux = num[j];

Yet another error hides between the inner for loop and the if condition in it. Can you spot it...?

Look:

      for (j=0; j<3; j++){
        if (num[j] > num[j+1])
           ....

See? Even if the num[] array was 3 items long, this will run into UB, because at the last iteration you have j==2 so index [j+1] would be out of bounds.

CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • Thanks to all, I get the idea and will try harder...like a thousand times : ) – Bruno Dumbra Sep 29 '17 at 13:00
  • @BrunoDumbra Please see expanded answer. – CiaPan Sep 29 '17 at 13:23
  • I understood that my array was short and that the first 'for' was getting the next pointer. And that I needed to decrease my second 'for' (j) in purpose of not comparing with another value that is not from my array and cause a UB. Tks!! – Bruno Dumbra Sep 29 '17 at 16:57