Consequences of passing too few register parameters to a C function

(devblogs.microsoft.com)

25 points | by aragonite 2 days ago

7 comments

  • _kst_ 19 minutes ago
    It's not even possible to pass too few arguments to a function in C unless you go out of your way to write bad code.

    You can write a function declaration that's inconsistent with its definition in another translation unit. Declaring the function in a shared header file avoids this.

    You can use an old-style declaration that doesn't specify what parameters a function expects. Don't do that. Use prototypes.

    You can use a cast to convert a function pointer to an incompatible type, and call through the resulting pointer. Don't do that.

    You can call a function with no visible declaration if your compiler overly permissive or is operating in pre-C99 mode. Don't do that.

  • LelouBil 6 minutes ago
    [delayed]
  • rurban 8 minutes ago
    Of which decade is this post? I cannot think of any modern architecture which still passes args on the stack.

    Itanium? Stone age

  • bananamogul 1 hour ago
    Raymend Chen has probably forgotten more about programming than I'll ever know, but aren't the first two blah() function examples either missing a } or have a superfluous { after the else?
    • Onavo 50 minutes ago
      Post COVID software engineer grads probably won't understand this comment.
      • camkego 15 minutes ago
        Why? Because of LLM vibe coding?
        • burner420042 3 minutes ago
          Instantly finding a missing semicolon or unbalanced parentheses on a screen of text.

          Kids these days!

  • hyperhello 30 minutes ago
    Do you really not ‘pass’ register parameters? How can anyone tell if you didn’t?
    • Polizeiposaune 20 minutes ago
      Read the post - not all architectures behave the same!

      Itanic had variable-sized register windows, plus extra tag bits for NaT ("not a thing") placeholder values. If you didn't set one of the argument registers the callee might trap in unexpected ways when it touches the register garbage.

  • anitil 1 hour ago
    I had never considered the idea of passing too few register params so I didn't immediately think of the reuse problem. And I had no idea about Itanium's Not-a-thing bit! Always a good read from Raymond Chen.
  • 9fwfj9r 11 minutes ago
    I regard this yet another unintuitive Itanium quirk that makes it failed.