Skip to main content
added 6 characters in body; added 32 characters in body
Source Link
kriss
  • 24.3k
  • 17
  • 104
  • 120

It should not work and certainly do not work on all compilers and target OS, even if it works on yours.

The likely explanation is that a function returning int always return something, and it's usually the content of a register. It probably happens that the register used for return value is in your case the same used to compute the last expression before returning from the function (on x86 targets, certainly eax).

This being said, an optimizing compiler detecting that there is no return is allowed to completely remove the code of this function. Henceforth the effect you see (may) disappear when activating higher optimizations levels.

I tested it with gcc:

gcc without optimization: inputs 10, 20 -> result is 10

gcc -O1 inputs 10, 20 -> result is 1

gcc -O2 inputs 10, 20 -> result is 0

It should not work and certainly do not work on all compilers and target OS, even if it works on yours.

The likely explanation is that a function returning int always return something, and it's usually the content of a register. It probably happens that the register used for return value is in your case the same used to compute the last expression before returning from the function.

This being said, an optimizing compiler detecting that there is no return is allowed to completely remove the code of this function. Henceforth the effect you see disappear when activating higher optimizations levels.

I tested it with gcc:

gcc without optimization: inputs 10, 20 -> result is 10

gcc -O1 inputs 10, 20 -> result is 1

gcc -O2 inputs 10, 20 -> result is 0

It should not work and certainly do not work on all compilers and target OS, even if it works on yours.

The likely explanation is that a function returning int always return something, and it's usually the content of a register. It probably happens that the register used for return value is in your case the same used to compute the last expression before returning from the function (on x86 targets, certainly eax).

This being said, an optimizing compiler detecting that there is no return is allowed to completely remove the code of this function. Henceforth the effect you see (may) disappear when activating higher optimizations levels.

I tested it with gcc:

gcc without optimization: inputs 10, 20 -> result is 10

gcc -O1 inputs 10, 20 -> result is 1

gcc -O2 inputs 10, 20 -> result is 0

added 160 characters in body; edited body
Source Link
kriss
  • 24.3k
  • 17
  • 104
  • 120

It should not work and certainly do not work on all compilers and target OS, even if it works on yours.

The likely explanation is that a function returning int always return something, and it's usually the content of a register. It probably happens that the register used for return value is in your case the same used to compute the last expression before returning from the function.

This being said, an optimizing compiler detecting that there is no return is allowed to completely remove the code of this function. Henceforth the effect you see may well disappear when activating higher optimizations levels.

I tested it with gcc:

gcc without optimization: inputs 10, 20 -> result is 10

gcc -O1 inputs 10, 20 -> result is 1

gcc -O2 inputs 10, 20 -> result is 0

It should not work and certainly do not work on all compilers and target OS, even if it works on yours.

The likely explanation is that a function returning int always return something, and it's usually the content of a register. It probably happens that the register used for return value is in your case the same used to compute the last expression before returning from the function.

This being said, an optimizing compiler detecting that there is no return is allowed to completely remove the code of this function. Henceforth the effect you see may well disappear when activating higher optimizations levels.

It should not work and certainly do not work on all compilers and target OS, even if it works on yours.

The likely explanation is that a function returning int always return something, and it's usually the content of a register. It probably happens that the register used for return value is in your case the same used to compute the last expression before returning from the function.

This being said, an optimizing compiler detecting that there is no return is allowed to completely remove the code of this function. Henceforth the effect you see disappear when activating higher optimizations levels.

I tested it with gcc:

gcc without optimization: inputs 10, 20 -> result is 10

gcc -O1 inputs 10, 20 -> result is 1

gcc -O2 inputs 10, 20 -> result is 0

Source Link
kriss
  • 24.3k
  • 17
  • 104
  • 120

It should not work and certainly do not work on all compilers and target OS, even if it works on yours.

The likely explanation is that a function returning int always return something, and it's usually the content of a register. It probably happens that the register used for return value is in your case the same used to compute the last expression before returning from the function.

This being said, an optimizing compiler detecting that there is no return is allowed to completely remove the code of this function. Henceforth the effect you see may well disappear when activating higher optimizations levels.