0

As an example of implementation defined behavior in C. The C Standard says that the size of data types are implementation defined. So, say sizeof(int) is implementation defined.

  1. Does this implementation defined behavior mean that the size(int) is platform dependent or defined by compiler vendor or both?

  2. Once I compile my code, does implementation dependencies would still apply when I run it on different versions of platforms? Would I get performance loss for compiling implementation defined code on one platform and running it on other?

user963241
  • 6,758
  • 19
  • 65
  • 93
  • There are **many** posts regarding exactly this (first question) – asheeshr Dec 04 '12 at 07:04
  • Like [this one](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) for example. – Lundin Dec 04 '12 at 07:32

5 Answers5

3

Yes, implementation defined means that it depends on the platform (Architecture + OS ABI + compiler).

And yes, implementation defined features can differ across different versions of the platform.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • If i create an executable version of my code and run it on a different architecture system, will the sizes change ? – asheeshr Dec 04 '12 at 07:08
  • @AshRj No, the program simply won't run, exactly because the sizes are different. – Šimon Tóth Dec 04 '12 at 07:09
  • @Let_Me_Be: Do you mean that If I compile something on Windows XP then it won't run on Windows 7 because as you said `implementation defined features can differ across different versions of the platform`? I would be surprised. – user963241 Dec 04 '12 at 07:14
  • @user963241 And that is why I wrote "can" and not "will". – Šimon Tóth Dec 04 '12 at 07:16
  • @Let_Me_Be I'm afraid even before sizes will be a problem, the architecture will prevent the executable from running. ;-) In fact it depends on the specific case if programs will run on systems with different sizes. Look at AMD64 vs. x86 for example. – junix Dec 04 '12 at 07:17
  • @junix Yes, even the default executable format can be different, but that is different topic. – Šimon Tóth Dec 04 '12 at 07:19
  • @user963241: Windows 7 is cleverly designed to be able to run Windows XP programs. And as it happens, `sizeof(int)` is 4 on both. But you cannot deduce from that one example whether or not it's possible *in general* to compile a program for one platform (e.g. Win32/x86) and run it on another (e.g. Linux/MIPS). – Steve Jessop Dec 04 '12 at 09:20
3

Does this implementation defined behavior mean that the size(int) is platform dependent or defined by compiler vendor or both?

In principle the compiler vendor can make that decision. In practice, if the compiler wants to emit code that calls directly to system libraries, then it has to follow the same "ABI" (Application Binary Interface) as the system, and among other things the ABI will specify the size of int. So the compiler vendor will "decide" to make it the size the ABI says.

Compilers that target multiple platforms and architectures will make the decision separately as part of the configuration of each platform. Each target then represents a different C implementation, even though you think of it as "the same compiler".

You could write a conforming C implementation in which int is a different size from what it is on the OS that runs the program. People rarely do, and the standard libraries would have to jump through extra hoops when they make system calls. It could be useful as part of an emulator, but then you might reasonably argue that the "platform" is the emulated platform, not the host platform with its different-sized int.

Once I compile my code, does implementation dependencies would still apply when I run it on different versions of platforms?

sizeof(int) is a compile-time constant, which means that the code emitted by your compiler might assume a certain value. That binary code cannot then run correctly on a different version of the platform with a different sized int.

Would I get performance loss for compiling implementation defined code on one platform and running it on other?

If it works at all, then there's no particular reason to assume there will be a performance loss. It generally won't work at all (see above), because binary code intended for one platform in general doesn't work on another platform. If the platforms are similar enough that it does work, it's possible that optimizations that the compiler made intended for one, are not such good optimizations on another. In that case there would be a performance loss, and the fix would be to re-compile the code targeting the correct (version of the) platform.

This does happen with ARM, and to a lesser extent with x86. Different chips in the past have offered essentially the same instruction set, but with some instructions on some chips having significantly different cost relative to other instructions. An optimization that assumes instruction X is fast would likely be a bad optimization on a different chip where instruction X is slow. As you can imagine, this kind of difference doesn't make the chip manufacturer hugely popular with compiler vendors, and even less so with assembly programmers.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
2

Does this implementation defined behavior mean that the size(int) is platform dependent or defined by compiler vendor or both?

In the C Standard terminology, the implementation is the compiler.

Here is the actual definition from the C Standard of the term implementation:

(C99, 3.12p1) implementation: particular set of software, running in a particular translation environment under particular control options, that performs translation of programs for, and supports execution of functions in, a particular execution environment

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Q: But compiler are constraint by platform OS/Architecture ? – Grijesh Chauhan Dec 04 '12 at 07:25
  • @GrijeshChauhan Some implementation defined behaviors are constraint by the ABI. That's why compiler documentation sometimes redirects to the ABI for some implementation defined behaviors. Otherwise some implementation defined behavior choices are made to accommodate the hardware and some others are made for historical reasons. – ouah Dec 04 '12 at 07:58
  • @ouah: "the implementation is the compiler" -- I don't think that's entirely true. The compiler is your immediate point of access to the implementation, but the implementation also consists of things that are not part of the compiler, including the environment in which the program is executed. – Steve Jessop Dec 04 '12 at 08:59
  • @SteveJessop To some extent, yes. C defines what it means by the *implementation* term in C99, 3.12p1. I add a quote in my answer. – ouah Dec 04 '12 at 17:50
0

size(int) is indeed implementation dependent. It has nothing to do with performance, but rather architecture of the platform you are using. A CPU that is 32-bit wide will behave differently than one that is 64-bit wide or even one that is 16-bit wide.

That's what they mostly refer to by platform dependent, but also there is the question of cross-compiling, which brings even more issues. You can use flags like -m to specify the architecture and width which causes code to use run under different platforms than it was originally compiled on.

Alberto
  • 136
  • 1
  • 5
0

According to the C- standard

ISO/IEC 9899:1999 §3.4.1

1 implementation-defined behavior
unspecified behavior where each implementation documents how the choice is made`

It means the behavior which is documented in compiler is implementation defined.

sizeof() is documented.

2 EXAMPLE : An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right.

Annex J 'Portability Issues' includes a lists of Unspecified Behaviour (J.1), Undefined Behaviour (J.2), Implementation-Defined Behaviour (J.3) and Locale-Specific Behaviour (J.4).

Community
  • 1
  • 1
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • 3
    Or rather: the compiler _must_ document all implementation-defined behavior or it does not follow the standard. So if your compiler does not document for example how large sizeof(int) is, then your compiler is, stricly speaking, not a C compiler. – Lundin Dec 04 '12 at 07:25
  • @Lundin: Would it be legitimate for the documentation of a compiler to specify that e.g. any computations performed on the result of casting an out-of-range value to an `int` may yield any arbitrary values, or would the documentation be required to specify some particular consistent behavior? – supercat Jan 10 '14 at 18:46