Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

C has VLAs, so sizeof is a runtime operation. That's already more complex than C++, where sizeof is fixed at compile time.


VLA has been "dropped" from C11 though (made optional), so it's essentially been degraded to a compiler-specific language extension.

IMHO VLAs were a "misfeature" that shouldn't have slipped into the standard in the first place.


With some slight tweaking variable modified types are the simplest and cleanest way forward to supporting arrays/slices as function parameters.

  void foo(size_t n, char buf[n]) {
    size_t m = sizeof buf;
  }
In a saner world n and m should be equal, but in a cruel twist it doesn't do what you'd expect. I don't understand why the committee crippled the semantics here when adding VLAs and variable modified types. Never too late to fix things, though.


That's interesting. Can you elaborate on why?


The reason it doesn't work despite being syntactically correct is because the standard goes out of its way to declare that, despite appearances, buf still behaves as if declared as `char *buf`. Thus sizeof just evaluates to the size of a pointer.

I think the original intent of the variably modified parameter syntax was merely to support optimization--a compiler could hypothetically prefetch n elements. Whether or why they didn't care to consider the potential security and robustness benefits of making the semantics behave like automatic storage VLAs, I have no idea. But over the years I've seen multiple proposals to rehabilitate the semantics.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: