Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C programming tricks

Name: Anonymous 2007-09-15 22:06 ID:WCtjqT0N

While programming optimization that makes ones' code completely unreadable is often a bad thing, there are places for it, like that one inner loop of your code that takes up 98% of the program's running time.

What are you tricks for improving performance in C, other than the obvious inline assembly or the like?

Name: Anonymous 2008-01-08 18:32

To exchange two variables in LISP without using a third variable:

(SETQ X (PROG2 0 Y (SETQ Y X)))

Name: Anonymous 2008-01-08 18:42

>>121
(psetq x y y x)

Name: Anonymous 2008-01-08 19:15

>>118
Yes, but parsing Sepples is NP-hard

Name: Anonymous 2008-01-08 19:46

Name: Anonymous 2008-01-08 19:52

>>124
Actually, it's invalid C.
It invokes undefined behavior in the assignment and calls a function that is not standard without providing declaration and definition. (the non-standard function is random btw)

let's see the assignment:
*s=f[random()%2](*s++);
As you might know, in such assignment the compiler is free to evaluate the RHS or the LHS first.
If LHS is evaluated first and then RHS what happends?
That's right motherfucker. Undefined behavior.
Fucking C fag, gtfo /prog/ and read SICP.

Name: Anonymous 2008-01-08 19:54

>>122
noob, >>121-san's code is from HAKMEM

Name: Anonymous 2008-01-08 20:16

>>125
More precisely:

6.5p2
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.  Furthermore, the prior value shall be read only to determine the value to be stored.

In the expression *s=f[random()%2](*s++), s has its value modified and its prior value read to determine the new value:

*s=f[random()%2](*s++)
                  ^^^
but its prior value is also read elsewhere:
*s=f[random()%2](*s++)
 ^

which violates the second sentence of the quoted paragraph.

Name: Anonymous 2008-01-09 0:15

>>127
the way you interpret that second sentence, if(i++) would be invalid because i is modified and it's prior value is read for a purpose other than determining the value to be stored. this is obviously not what the authors of the standard intended.

Name: Anonymous 2008-01-09 3:33

>>125,128
It's random() anyway so the point is doug moot. But most compilers defer postfix ops until the whole expression has been evaluated (I only wish it was in the standard)

Here's a tricky bit of code that is valid but most people won't think so:

int i=0;
i++ && i++ || i++ && i++


What's the value of i after the above fragment executes?

Name: Anonymous 2008-01-09 5:27

>>129
2?

Name: Anonymous 2008-01-09 5:29

>>129
4

Name: Anonymous 2008-01-09 5:33

>>128
A clearer statement of what I interpret the second sentence to mean is: if the prior value is read then this reading shall be used to determine the new value. For i++ I would say that the value of i is read once and used to determine both the new value of i and the value of the expression itself. This is OK with my interpretation.

Name: Anonymous 2008-01-09 5:57

I know I'm late but >>121-122 are faggots

expert programmer style:
(setf (values x y) (values y x))
or:
(rotatef x y)

Name: Anonymous 2008-01-09 5:59

>>129
3

>>130,131 are faggots

Name: Anonymous 2008-01-09 6:00

int i=0;
i++ && i++ || i++ && i++

This is why prefix notation is superior.

Name: Anonymous 2008-01-09 6:02

>>130,131
Wrong. This was a CS exam question that only 3 out of 102 students answered correctly.

Hint: sequence points.

Name: Anonymous 2008-01-09 6:02


int i = 0;
i++ && i++ || i++ && i++
^      ^      ^1     ^
0      Don't eval   2

Result: 3

Name: Anonymous 2008-01-09 6:03

>>134
win

Name: Anonymous 2008-01-09 6:03

>>136
That's why you would write it in lisp and then 'macroexpand it.

Name: Anonymous 2008-01-09 6:06

>>136
Only 3? Hahaha oh wow

Name: Anonymous 2008-01-09 6:30

>>140
Yeah, that was pretty pathetic, considering K&R and the C standard was required reading for the course. The class average on that exam was 57%.

http://img185.imageshack.us/img185/1508/112resgt8.png

Name: Anonymous 2008-01-09 6:52

>>136
o rly because that one was obvious.  I suppose reading K&R has its benefits, too bad I don't know what the hell a sequence point is though.

Name: Anonymous 2008-01-09 7:17

>>141
Is the answer 0?

Name: Anonymous 2008-01-09 8:02

>>143
NO. And I don't even program in C!

Name: Anonymous 2008-01-09 8:14

&& has higher priority than ||, so it's either 2 or 2, so 2.

In lisp, on the other hand, everything is true as long as it's not '(), so it would be 1 (boolean truth).



Am I right? I don't program in C either.

Name: Anonymous 2008-01-09 8:28

>>145
Incorrect.
It's 3.

i = 0
...
              i is 1, incremented
i++ && i++ || i++ && i++
^      ^      ^      ^
0,   ,/            i is 2, incremented
next skipped
i incremented
[code]

should be 3


New one:

[code]
i = 1
sizeof (i++) && i++ || i++

What is i?

Name: Anonymous 2008-01-09 8:33

The answer is: You can't know. Undefined. The C standard says nothing about what the result will be and the only way to know for sure what i will be after that line lies in how the specific compiler that compiles that piece of code handles it.

Name: Anonymous 2008-01-09 9:15

>>146
3 as well, assuming sizeof (i) != 0.

Name: 146 2008-01-09 9:48

>>147
Incorrect.

>>148
Incorrect.
sizeof cannot evaluate to 0.
1 to SIZE_MAX in fact.
Furthermore, sizeof does not evaluate it's operand.

The result will be 2.

Name: Anonymous 2008-01-09 10:45

no

Name: Anonymous 2008-01-09 11:18

>>149
Oh you little wizard you...

Name: Anonymous 2008-01-09 12:43

Furthermore, sizeof does not evaluate it's operand
Furthermore, sizeof does not evaluate it is operand
Syntax error >>149 (8): found verb, expected noun phrase

Name: Anonymous 2008-01-09 12:55

>>152
Holy fuck, which compiler parses English contractions?

Name: Anonymous 2008-01-09 15:04

>>153
ecc

Name: Anonymous 2008-01-09 16:16

>>154
What does the e stand for in ecc?

Name: Anonymous 2008-01-09 16:19

>>155
English.

Oh wait.

Name: Anonymous 2008-01-10 0:52

This is some C code I wrote this morning:

     int count = 0;
     int m = 0; /* Start with the first offset modifier. */
     for (m = 0; count<4 && m < 8; m++)
     {
          if (!m%2) count = 1; /* The count is for one of the four directions. */
          int xo = 0,yo = 0,i = 1; /* Offsets and incrementer. */
          for (;(x+xo>=0) && (x+xo<WIDTH)   /* Make sure we've not */
             && (y+yo>=0) && (y+yo<HEIGHT)  /* gone off the grid.  */
             && grid[x+xo][y+yo]==pl; /* Every single one needs to be ==pl. */
               i++, xo=i*mod[m].x, yo=i*mod[m].y, /* Update offsets. */
               count++);/* Increase count. */
     }

Name: Anonymous 2008-01-10 0:56

count++);/* Increase count. */

Name: Anonymous 2008-01-10 2:09

>>158
What's the matter? Haven't seen well commented C code before?

Name: Anonymous 2008-01-10 3:11

&& grid[x+xo][y+yo]==pl; /* Every single one needs to be ==pl. */
O RLY

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List