"Strong" Typing in C by one-element structs. What will the compiler do? -


To make some semantic strong typing, I am using streaks containing a scalar field in my C code. The basic idea is the macro for the cheaper "operation", which will fail on the designated structured field incorrectly, and of course, more complex functions by the rigid parameter list.

Example (basic idea only - not exspecially smart macro codes)

Typingfile structure {float32_t speedval} MySpeed_t; Typedef struct {float32_t timeval} MyTime_t; Typefed Structure {float32_t accvalue} MyAcceleration_t; #define ACC_VEL_DT (ACC, VL, time) \ ((ACC) .accvalue = (vel) .speedval / (time) .timal)), (ACC) #EDD_ SPEED defined (velres, vel1, vel2) \ ((Velres ) .speedval = (vel1) .speedval + (vel2) .speedval), (velres) unint8 some CleverMathAndCheck (MySpeed_t speed, MySpeed_t speedArr [], MyAcceleration_t);

Now what do I expect from a compiler, when handling such a one-time framework? Should I expect some padding, "delete the first element", or a more complex form for horrible things, when those parameters should be used as a function parameter? What about the standard?

No extra padding will be the standard, the address of a straight is the address of its first element, hence the padding Always at the end of the elements or straight, never starts. Apart from that, you do not have to worry about extra padding at the end, because the alignment will be exactly alignment for the data type.

With optimization turned on, you can expect compilers to originally produce the same assembly code, for example (32 bit) for O2 code generated by GCC:

  float add (float a, float b) {a + b return; }  

Looks like this:

  _add: LFB0: .cfi_startproc flds 8 (% esp) fadds 4 (% esp) ret. Cfi_endproc  

If you define something in return:

  typedef struct foo {float x; } Foo; Foo add (foo a, foo b) {foo f; F.X = Ax + B.x; Return; }  

and compile with O2, the assembly is exactly the same.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -