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

Macros vs Inlines

Name: Anonymous 2011-10-15 4:05

Why do so many C programmers define stuff like min and max as macros?

I see stuff like #define MIN(a, b) (a < b ? a : b) everywhere, but then you get issues if you do stuff like MIN(a++, b--). If MIN was an inline function you wouldn't get undefined behavior like that.

Is it because C doesn't have overloadable functions, so you can't define a single MIN that works with every type of number?

Name: Anonymous 2011-10-15 4:14

>>1
Ask on Freenode's ##C.

*grabs popcorn*

Name: FrozenVoid 2011-10-15 4:38

Macros are guaranteed to not have any overhead
They are more portable, without any types
They takes less space to code, can use all global vars/functions
In most of code small functions can be nested very deep without overhead of real functions. You have to be careful though with side effects, but its easy to spot.

Name: Anonymous 2011-10-15 5:37

Name: Anonymous 2011-10-15 5:39

Name: Anonymous 2011-10-15 5:40

Name: Anonymous 2011-10-15 6:20

>>3
So inline isn't used because it can't be trusted?
It's supposed to be a hint to the compiler isn't it?

Name: FrozenVoid 2011-10-15 6:46

>>7
Functions declared as inline are often treated as variable declared register(i.e. ignored 90% of the time) and even if inline they have the overhead of functional semantics.
The idea of inline function is very rarely useful
1.when you need many function calls used often, real calls save space(single code path)
2.when you need several or rare calls, a macro is enough
3. If you need to return values or encapsulate large chunks of code, the overhead can be considered negligible for non-inline call(and will save space for each use).

Name: Anonymous 2011-10-15 6:52

>>1
if you do stuff like [code]MIN(a++, b--)[/code[o]
[/o]You don't because the all-caps name reminds you that it's a macro and you shouldn't put expression with side-effects in its invocation. Alternatively, you can use Amplified C[sup]http://voodoo-slide.blogspot.com/2010/01/amplifying-c.html[/sup].

Name: >>9 2011-10-15 6:54

Oh god what did I do?
if you do stuff like MIN(a++, b--)
You don't because the all-caps name reminds you that it's a macro and you shouldn't put expression with side-effects in its invocation. Alternatively, you can use Amplified Chttp://voodoo-slide.blogspot.com/2010/01/amplifying-c.html.

Name: Anonymous 2011-10-15 6:59

>>10
I could amplify my penis for you, if you'd like.

Name: Anonymous 2011-10-15 7:16

>>10>>9
http://voodoo-slide.blogspot.com/2010/01/amplifying-c.html.
Software Engineer at EA DICE.
I've shipped 4 AAA games on MLOC code bases

WOW! "AAA games" are made by silly C/C++ retards. No wonder modern games suck ass. And EA does worst games of them all. Even brainless japs do better games than EA.

Name: Anonymous 2011-10-15 7:18

>>12
Ah! And take note that this oligophrenic monkey is proud their piece of shit weights MLOCs.

Name: Anonymous 2011-10-15 7:34

herp
> herp

Name: Anonymous 2011-10-15 7:38

>>13
They are paid by lines of code or similar metric.

Name: Anonymous 2011-10-15 7:53

>>15
If they arent paid a percent from sales, then they are slaves with no initiative.

Name: VIPPER 2011-10-15 8:06

>>15
So like that would come out under minimum wage.

Name: Anonymous 2011-10-15 9:05

>>15
/* Original pre-ISO C style without function prototypes. Guaranteed +5 to LOC per functions */
int main(argc, argv)
    int   argc;
    char  *argv[];
{
    ...
}

Name: Anonymous 2011-10-15 9:12

>>18
[code]/* Standard IO library header inclusion for:
   - fprintf */
#include <stdio.h>

/* Main function.
   It prints the string "Hello, world!".
   It returns an int exit value to the OS.
   It takes the standard parameters
   describing the command-line program
   arguments. */
int
main(argc, argv)
/* Type declarations */
  int argc;
  char* argv[];
{
  /* The string to be printed */
  static
  const char*
  const str =
    "Hello, world!";

  /* Prints the string str to the
     standard output */
  fprintf(stdout,
          "%s\n",
          str);

  /* Returns a successful exit value */
  return 0; 
}

Name: Anonymous 2011-10-15 9:31

>>19
int
main(argc, argv)

I always wondered why they put int on a separate line. Now I know!

Name: Anonymous 2011-10-15 9:54

>>12
(defun my-function ((a int) (b int) (return int)) (return (+ a b))
His Lisp macros are verbose as fuck. Why not my-function a b -> add a b?

int is a good default (90% of values are ints).

Name: Anonymous 2011-10-15 10:06

D-D-D-D-DUBZZZZZZZZZZZZZZ

Name: HAXUS THE GREAT 2011-10-15 12:06

Go back to /Japan/ please.

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