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

C

Name: Anonymous 2009-10-20 17:13

Hey!

Is there a shortcut for this "pattern":
c = (a -b);
if (c != 0) return c;
c = (d-c);
if (c != 0); return c;
//...



I don't quite see how I can avoid storing c, because it matters if it's <0 or >0... or does it get optimized away anyway?

--

qsort, bsearch, and so on take a function whose arguments need to be "const void *p". Is there a way to cast them into the right type right in the function definition, without needing to either introduce two new variables, or cast them every time they're used?

Thanks /prog/!

Name: Anonymous 2009-10-20 17:16

(c = a -b)? return c :; perhaps?

Name: Anonymous 2009-10-20 17:16

Whoops didn't read ahead, disregard

Name: Anonymous 2009-10-20 17:23

>>1 KUN here

-1, 0 or +1 is fine though. I don't need the exact value. If that helps in any way.

Name: Anonymous 2009-10-20 17:25

If you write something like
if (a - b) return a - b; the compiler will evaluate the common subexpression a - b only once. But you're worrying about the performance of inconsequential things.

Also note that in your example the second if will be executed only if a - b == 0, which means the second if essentially becomes if (d).

Name: Anonymous 2009-10-20 17:27

>>5

Yuck, the example is messed up then. Obviously I'm not comparing "c". Sorry about that.

I wasn't worrying so much about performance, just that it looks kind of ugly, and needs an extra variable. So I can just make a macro for that expression, and should be done with it. Fine, thanks!

Name: Anonymous 2009-10-20 17:42

>>5

Actually, on a second thought, how can it be evaluated only once?

a and be could be function calls (which they are in my case), so the result could be different.

Name: Anonymous 2009-10-20 17:44

Your `pattern' is complete nonsense, but

c = (a -b);
if (c != 0) return c;


can be replaced with

if (c = (a -b)) return c;

if you [i]really[i] need to use your bullshit `pattern'.

So I can just make a macro for that expression, and should be done with it.
Now you have two problems.

Name: Anonymous 2009-10-20 18:01

ITT: LET'S GO OVER A BUNCH OF WAYS TO WRITE SOMETHING WHICH WILL ALL LOOK THE SAME AFTER COMPILATION!

Name: Anonymous 2009-10-20 18:01

/prog/ == /c/

Name: Anonymous 2009-10-20 18:09

>>9
The point is, they look different before compilation. Compilers don't give a fuck about readability. People do.

Name: Anonymous 2009-10-20 18:13

>>8

Shut your face you dickhead. I was talking about "pattern" in the sense of a repeated "pattern" of expressions. I know I can write it that way, and guess, it's exactly the same. So shut your face. I'm looking for an easier way to write it, so it's easier to read. Fucking hell you're a shithead.

>>11

Thanks, yes.

Name: Anonymous 2009-10-20 18:16

>>12
>>8 and >>11 are the same person.

Name: Anonymous 2009-10-20 18:20

>>13

IHBT

I'm just looking for a better way to write these expressions, if you have a bunch of them, and the things you're comparing are "long" expressions themselves, function calls, and so on.

Name: Anonymous 2009-10-20 18:29

c new fags itt

Name: Anonymous 2009-10-20 18:54

>>7
If the expression has side-effects then obviously it has to be evaluated every time it occurs, but then that's not what you fucking put in your fucking example now is it?

A compiler that does whole-program optimization could still deduce that the value of the function only depends on the parameters and that it has no side-effects in which case it would only have to be called once AFAIK. I have no idea how common this type of optimization is.

Name: Anonymous 2009-10-20 18:59

the compiler i wrote when i was twelve would only evaluate this once

Name: Anonymous 2009-10-20 19:43

This is where we see the elegance of Perl's a || b || c idiom. Too bad C forces logic results to 0 or 1.

Though it strikes me that I'd probably write the first example as
return a !=b?a-b:c != d?c- d: //...
Does it still get optimized the same, I wonder? *lazy*

For the second, you can cast the function pointer to void* when you pass it to bypass the type checking.

Name: Anonymous 2009-10-20 20:14

>>18
Perl's a || b || c idiom.
What does it do?

Name: Anonymous 2009-10-20 20:17

>>19
Return the first of a, b, and c that has a true value, or false if none of them do. Same goes for a or b or c in Python, etc.

>>18
You can use ?: in GNU C just the same way.
a ?: b ?: c

Name: Anonymous 2009-10-20 20:28

>>20
BUT I FINALLY FOUND A REASON TO USE PERL!!!

Name: Anonymous 2009-10-20 21:06

>>20
The binary Elvis operatorですか?!

Name: Anonymous 2009-10-21 0:59

>>22
You mean the literal string dildos?

Name: Anonymous 2009-10-21 1:33

>>21-23
The correct reason to use Perl is the length operator, =()=.

Name: Anonymous 2009-10-21 2:07

The correct reason to use Perl is the anus operator, (*).

Name: Anonymous 2009-10-21 2:23

>>1

a - b ?: d - c ?: <something else goes here>

Name: Anonymous 2009-10-21 2:27

>>25
Invalid Perl code!
Perhaps your referring to <*>, as in:
$num_visible_files_in_directory =()= <*>

Name: Anonymous 2009-10-21 3:10

>>18

Thanks faggot, but the functions need to be of type "int", not "void *". The ARGUMENTS need to be of type "const void *".

Fucking hell, are there only retards in my /prog/?

Name: Anonymous 2009-10-21 3:15

>>16

Alright, "improved" example:


c = f1(a1) - f2(a2);
if (c != 0) return c;
c = f3(a3) - f4(a4);
if (c != 0) return c;
c = f5(a5) - f6(a6);
if (c != 0) return c;
//...

Name: Larry 2009-10-21 3:16

>>27
Actually, he did post <*>, but I haxed his anus.

Name: Anonymous 2009-10-21 3:17

>>28

How has /prog/ been responding to this guy for so long?  Clearly he doesn't belong here.

Back to /b/, please!

Name: EXPERT PROGRAMMER 2009-10-21 3:31


#define RETURN_IF_NZ( expr )           \
        ({ typeof(expr) _res = (expr); \
           if( _res != 0 )             \
              return _res;             \
         })

/* ...  */

RETURN_IF_NZ( f1(a1) - f2(a2) );
RETURN_IF_NZ( f3(a3) - f4(a4) );
RETURN_IF_NZ( f5(a5) - f6(a6) );


GCC QUALITY

Name: Anonymous 2009-10-21 3:40

c=f1(a1)-f2(a2);if(c) return c;c =f3(a3)-f4(a4);if(c) return c;c=f5(a5)-f6(a6);if(c) return c;FV QUALITY!

Name: Anonymous 2009-10-21 4:14

>>32

Alright. Probably as good as it gets. You got an idea for the second question too? >>1 qsort, bsearch function parameter

Name: Anonymous 2009-10-21 4:38

>>32
This is not threadsafe. Please consider using the reentrant version RETURN_IF_NZ_r

Name: Anonymous 2009-10-21 6:57

Creating macros for stuff like this is asking for trouble.

if (c = f1(a1) - f2(a2)) return c;
if (c = f3(a3) - f4(a4)) return c;
if (c = f5(a5) - f6(a6)) return c;


is still the best way to do this.

Name: Anonymous 2009-10-23 14:52

Name: Anonymous 2010-11-15 0:20

Name: Anonymous 2013-09-01 14:11



    +    __         ,. -‐-、                __i___i__
        i7<::::\     /:::::/´i{      ┌───  `ヽ ___|___|__
       +  }|:::!´\::::ヽ,  /::::://::::/}       | ┌─┐ ヽ ┌‐┴‐┐
        {!:::|、   \::|{/::::://::/ソ        !  !  ┘   / | l__|__,l|
  ┼     },!:::ヽ>-‐ァ'ニ7-'<::/r'´ `ヽ、    /  L_____」  / .!    .」
        ,ゝ、.,___,.ィ'/! ̄\::\、´、.,,__   '.,
     ,./    く::::/」   ;ヽ、_」{' \`ヽ、 ',
    く/  ,'  7'十r  ;   /-rイ    ':、__」 .i
     ,'  /  /!,_」_! ./| ,.'-‐-'、!  |  !   |
     i  .,' ./ァ' ;-、レ' .!/' ;'´`ヽ!  ! ハ   .i   ┼       ,.-、 _
     |  i  ハ! ! r!      !  rリ!__」/ /!   ,'!.         / ノ´ )
     L__ハ_!` `´ ,    `"´,.,/   !__/,ハ       _  /  / /__
       | !、    、_ _,   /  /   .,':::!}     ( `ヾ,  '  '"´ _)
       ! |/>,、,     /    ,'    /_::_|{,__    ヽ  `ヽ   <´
    +   ', V´   `iァーr /   /  /´  }/-!、 _,,. -‐〉、__,  、__,、,_`)
        ヽ.!    /イ´/  '´ _,,..イ   /」:::::::/::!7:,r'、::`'-r‐ァ´
         ノ\  i_!/ _,. イ´  ,'  /」::::::::::}!::!{ァ'  `''}|::!{
       r!´}!:`ヽr-rァ'"´`7/     /」´:::::::::::_}!::|{    /}|::!}
       /L! {!::;:イハ、:::;:-、」{    r'」´/ゝ、::::::::},ヘ!}   /`{|::!{
      ,!、::ヾ ̄( ノ):: ̄\!    /]:::::':::/|>ー'ヽ、 _,,.イヽ::}ヘ!{
 ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ____ ____  ___
|19|19|19|25|25|25|33|33|33|37|37|37|42||42|
|歳|歳|歳|歳|歳|歳|歳|歳|歳|歳|歳|歳|歳||歳|

Name: Anonymous 2013-09-01 15:42




          ,. '"´         `'' 、     (
          /    ,. '"´      ´ヽ  \    ,ゝ
        , '    ./ /  /  /|   |    ヽ  (
       '    / /   //ト-/ |  ,ハ/  | ハ  `(   
      r|    .|/  |   ' |テ‐-'、 |  /_ |  ;  |   ` ー-‐'"ヽ.,_.ノー-、_.ノ 
      |.{`''ー-7  人  |!イ んハ  !/ _」ヽ/  .!   ○
      | }ミ=| /| _」\| 、弋zソ    ん!Y   , ;  o
      レ` ̄ <´ rァ|_ | ///      lrソム/ /レ'  ゚
     ./     `ア,//´}        ' //|/\
     /   /   / ||| |、/´}    _     ム, ̄´
   / /  // _./_八  レ' {_、      /` }
    {rヘ / |ア´: : :.`ヽ\    ヽ.-- イ  /
     ∨ /: : : : : : : : ∨ハ     }/ヽ{__∧
     /: : : : : : : : :.r'7ァ--、__/三rヘ、__],ム
    rく: : : : : : : : : : /´ ̄ ̄ `ヽ}/: {       |
    {X\ : : : : : : : :{      }ムヘ、   _/
    \X`7ァー--ヘ ァ-─-=、/|_」|  ̄ ̄!
      `ヽー‐''" ̄`7    |/|__」X!    .|
       /::ム   /     ;|X|:::::::|X|     ;
      ,:'::::::∧  '      ムk}!:::::ムk|     .!
    rイ、:::::/:::::ヽ|    ,イ::|X|:::/X八    ノ
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ヽ-‐ '´ ̄ ̄ ̄ ̄ ̄` ´ ̄ ̄ ̄ ̄
     :::::   ::           :::              .:::
  ::::  :::                       ....:::::::::
   :::  ::       (ゝ ''"´ ̄ ̄`"''   、  ..:::....
    ::::::   , '"´ ̄    /`ヽ       `ヽ、  ::::.....
    :::   |   /    /            ヽ,    ::::::
     ::.   ; /   /  |          `ヽ   '.,
      ..  / '   ;' /|  ,|     |     ,ハ   ',
      _ノ  |   /| /\| /|  ハ '  |!   |   }
     ´ ̄|  '  /|斤'テ メ、| / |/   /    |   |   ....::::::
 ::::.......   '、 ∨o|八弋.zノ レ' 、_/ _,,..イ、  ;ヽ、__ .; ...:::::
       く \| 7xx`       ァ'テアlY  /ヽ.,__ /    ...:::
   ギリ... / ` ア{     '    弋_ンノ./`''ー 7
     //  ,  人    、_     xx‐oイ r_`> {    ー┼_,  |   | ー┼-、ヾ ヽ/
      レ| /   / \ / ノ`   / / ̄| ギリ..'、     /´| /ヽ レ  |   |  |  /
       ∨`ヽr‐ r∨´二ヾ_,,.. イ  ,'  /  /   ヽ  し i´ ノ   .ノ  ノ 、ノ  ヽ---
       ,. : '"´ ̄/ -─、リ  /|/| ,∠.,_/   ト、ハ
      ./: : : : : : ;   _ ‐、ノ-r く /レ'|!: : : :`ヽ、ノ ∨ (_,つ      r┐[][]
      /: : : : : :r-{  `ン-、こ}!イ /ム| : : : : : : :';   r─''⌒ヽ ⊂ニ ニ⊃   r┐[][]
    r; : : : : : : | \ーr‐‐/ |ハ_|/X/: : : : : : : : }   ヽ-'⌒l } ⊂ニ ニ⊃⊂ニ ニ⊃
    {X\: : : : : 、    ̄「  /  '、.X/: |: : : : : : : :|      _/ ./  「 | `┘  ⊂ニ ニ⊃
     \X\: : : }`     ;  |   ∨: :, : : : : : : : ム     (_/   ヽ.二二l 「 | `┘  ○ O o
      \X>r|      { |!  」ヽ/--─ァ''"X}                ヽ.二二l
        `「 rヘ、    ノ-ヘ._r‐':::{X_>-''「 ̄
        '、\ \__,.イ::::::::{X {::::::|     |
        ∧  `''ー‐ソ}::::::::|X |::::r'、__,ム、

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