Skip to main content
103 votes
Accepted

Why do common Rust packages depend on C code?

Why do Rust packages have any dependency on C code? Taken literally, this question is not really on-topic: it’s a question about some decisions some people made when they wrote some libraries, not a ...
Alexis King's user avatar
  • 8,721
31 votes
Accepted

Why would a language have a concept of undefined behavior instead of raising an error?

This is a controversial topic The C89 standard, section 3.16, defines: undefined behavior: Behavior, upon use of a nonportable or erroneous program construct, or of erroneous data, or of ...
Pseudonym's user avatar
  • 4,726
29 votes

Why would accessing uninitialized memory necessarily be undefined behavior?

On top of what everyone else has said, UB is also often used to signal the fact that a program could mean different things at different optimisation levels. Consider this example: ...
Pseudonym's user avatar
  • 4,726
28 votes

What optimizations does the strict aliasing rule facilitate?

The main thing the optimizer wants to know here is whether writes through one pointer might affect reads through other pointers or names. When it knows for sure that two pointers are unrelated in this ...
rpjohnst's user avatar
  • 814
25 votes

What obstacles prevented C and C++ from standardizing π?

There is no deep reason. The answer is social: The C standard does not specify any constant for π. We can only speculate on why, but ultimately, the set of things the C standard does and does not ...
Alexis King's user avatar
  • 8,721
23 votes

How could a language make the loop-and-a-half less error-prone?

Rust can achieve this without break or code duplication, since blocks are expressions: ...
kaya3's user avatar
  • 19.7k
22 votes

Why would accessing uninitialized memory necessarily be undefined behavior?

Many instances of undefined behaviour in the C specification represent accommodations for existing implementations where the "obvious" treatment was not possible, including detecting and ...
Michael Homer's user avatar
  • 13k
21 votes
Accepted

Why would a language need to have trap representations?

Trap representations are usually a property of the underlying hardware. If they exist, a programming language has to choose a behaviour: Ensure that every value is initialised to a non-trap value ...
Toby Speight's user avatar
21 votes

Why do common Rust packages depend on C code?

(note: this answer is addressing a quite different question from the original revision: why one language would have a dependency on another language?) Is there is something fundamental about C that ...
apropos's user avatar
  • 1,028
20 votes

What are the advantages of strings and character arrays being different?

Different definitions of “character” In Swift, the Character type isn’t a single Unicode codepoint (that’s UnicodeScalar), nor ...
Bbrk24's user avatar
  • 9,067
19 votes

Why would a language have a concept of undefined behavior instead of raising an error?

Performance Checking for errors at runtime is quite expensive. Branching operators massively are slow instructions, especially since they prevent many optimizations. If every basic operator needed a ...
mousetail's user avatar
  • 8,349
19 votes

How could a language make the loop-and-a-half less error-prone?

In languages with a C-style comma operator, you can use while (foo(), condition) { bar(); }
Barmar's user avatar
  • 1,901
18 votes
Accepted

Why would accessing uninitialized memory necessarily be undefined behavior?

It's not strictly necessary for uninitialised memory access to be undefined behaviour in the "nasal demons" sense. A language which allows raw memory access could still choose to specify ...
kaya3's user avatar
  • 19.7k
17 votes

Why are mixed declarations more challenging to implement than forcing all declarations to be at the top of a scope?

One pass compilation C was originally (and, as far as I know, still is) designed to allow for one pass compilation. This means you tokenize, parse, and output, all in one pass, never building an ...
blueberry's user avatar
  • 2,465
16 votes
Accepted

Why can C not be lexed without resolving identifiers?

The short answer is yes, you could do it all in the parser (lexers are optional), but historically it's been easier the other way. It's mostly a historical accident that this all exists at all. This ...
Michael Homer's user avatar
  • 13k
15 votes

How could a language make the loop-and-a-half less error-prone?

A frame challenge: the problem isn't really with the loop construct itself, but with ad-hoc bundling of the "half" loop with the condition check. This is a problem that should be solved ...
Karl Knechtel's user avatar
15 votes

What prevents languages from having arbitrary sized return data on the stack?

This would in principle be possible. It would require that the function communicates back both the data, and its size. It wouldn't quite work with the standard calling conventions, but we could easily ...
leftaroundabout's user avatar
14 votes

Why would a language have a concept of undefined behavior instead of raising an error?

Besides performance and ease of writing an implementation, there is one other reason for undefined behavior. If some property is impossible to check for and cannot be defined because it's ...
Gavin D. Howard's user avatar
14 votes

Advantages/disavantages of including the comma operator?

The benefit of the comma operator is that it allows you to include a sub-expression for its side-effects only. In languages without a comma operator, there are often ways around this ─ suppose ...
kaya3's user avatar
  • 19.7k
13 votes

Is it possible to extend C to have the Rust concept of ownership for memory safety?

I think this answer depends a lot on what you mean by "extend C" here! If you're looking for a language in which Rust ownership exists and C libraries can be used natively, that's basically ...
Mae Milano's user avatar
13 votes

Why would a language need to have trap representations?

One example is that some computers would trap if an invalid bit pattern were loaded into one of the pointer registers (such as the segment selector of an 80286 protected-mode far pointer, necessary on ...
Davislor's user avatar
  • 1,062
13 votes

Invalid signed integer in C and similar languages

C is about what the machine may have (even now) A little history, before the answer. C is a direct successor of B, and B has really one type: the machine register, with then could act as a machine ...
André L F S Bacci's user avatar
13 votes

Why would accessing uninitialized memory necessarily be undefined behavior?

I am not sure if this was in the C standardisation committee's mind when making this decision, but it is worth noting that some computers may perform arbitrary actions when memory is read. A memory ...
occipita's user avatar
  • 569
11 votes

What are the implications of implementing variable-length arrays?

I'd say one of the reasons is that it was a rather niche feature with a very natural syntax (which you usually try to avoid). Most times I've seen VLAs used in code, it was by novice programmers who ...
abel1502's user avatar
  • 2,527
11 votes

Why doesn't C's standard library have functions for searching memory like strings?

I have no particular insight into decisions made by the designers of the C standard library, but I can offer some informed speculation. A C string is a (null-terminated) array of characters. That is, ...
kaya3's user avatar
  • 19.7k
11 votes

How could a language make the loop-and-a-half less error-prone?

A least-change approach would be to make it a compiler error if you write a while (true) or for (;;) loop without a loop-exiting ...
dan04's user avatar
  • 1,959
10 votes

Advantages/disavantages of including the comma operator?

Readability The main problem programmers have with the comma operator is this would permit having multiple expressions in a single line, which is generally frowned upon. ...
CPlus's user avatar
  • 8,321
10 votes

What are the advantages of strings and character arrays being different?

Encapsulation Treating strings as character arrays entails a belief that a character array is an adequate model for a string - not just good enough now, but good enough that you won't want to be able ...
Karl Knechtel's user avatar
10 votes

What are the advantages of strings and character arrays being different?

An array must have a fixed stride, so that the memory address of the ith element equals the address of the start of the array plus i times the stride. If you really want an array of characters, so ...
kaya3's user avatar
  • 19.7k
10 votes

Would a structure ever require padding beyond what is required to align the members?

There's two questions into one here: Would a structure ever require padding beyond what is required to align the members? Maybe? That is an implementation detail, and C tells you it doesn't care ...
Longinus's user avatar
  • 1,853

Only top scored, non community-wiki answers of a minimum length are eligible