Questions tagged [language-lawyer]

For questions about the intricacies of formal or authoritative specifications of programming languages.

Typical questions concern gaps between "what will usually work in practice" and "what the spec actually guarantees", but problems with understanding the structure of the spec are also on topic.

Use this tag for questions where you are interested in the formal specification for a certain behavior in the given programming language, even though your question might otherwise have no practical use, or if the code posted would not make sense in a real-world application.

Always combine this tag with a programming language tag.

7856 questions
74
votes
6 answers

Does C or C++ guarantee array < array + SIZE?

Suppose you have an array: int array[SIZE]; or int *array = new(int[SIZE]); Does C or C++ guarantee that array < array + SIZE, and if so where? I understand that regardless of the language spec, many operating systems guarantee this property by…
user3188445
  • 4,062
  • 16
  • 26
74
votes
5 answers

Is the compiler allowed to optimize out heap memory allocations?

Consider the following simple code that makes use of new (I am aware there is no delete[], but it does not pertain to this question): int main() { int* mem = new int[100]; return 0; } Is the compiler allowed to optimize out the new…
Banex
  • 2,890
  • 3
  • 28
  • 38
73
votes
1 answer

Is std::stoi actually safe to use?

I had a lovely conversation with someone about the downfalls of std::stoi. To put it bluntly, it uses std::strtol internally, and throws if that reports an error. According to them, though, std::strtol shouldn't report an error for an input of…
chris
  • 60,560
  • 13
  • 143
  • 205
72
votes
11 answers

What are the common undefined/unspecified behavior for C that you run into?

An example of unspecified behavior in the C language is the order of evaluation of arguments to a function. It might be left to right or right to left, you just don't know. This would affect how foo(c++, c) or foo(++c, c) gets evaluated. What other…
Benoit
  • 37,894
  • 24
  • 81
  • 116
72
votes
3 answers

std::tuple sizeof, is it a missed optimization?

I've checked all major compilers, and sizeof(std::tuple) is 16 for all of them. Presumably they just put elements in order into the tuple, so some space is wasted because of alignment. If tuple stored elements internally like:…
geza
  • 28,403
  • 6
  • 61
  • 135
72
votes
5 answers

Return void type in C and C++

This compiles without any warnings. Is this legal in C and C++ or does it just work in gcc and clang? If it is legal, is it some new thing after C99? void f(){ } void f2(){ return f(); } Update as "Rad Lexus" suggested I tried this: $ gcc…
Nick
  • 9,962
  • 4
  • 42
  • 80
71
votes
2 answers

Why does the compiler match "char" to "int" but not "short"?

I've got a small program: #include using namespace std; void f(int) { cout << "int\n"; } void f(short) { cout << "short\n"; } int main(void){ char c = 0; f(c); return 0; } It prints int. I felt that, if this is because…
Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
71
votes
3 answers

Is it legal to compare dangling pointers?

Is it legal to compare dangling pointers? int *p, *q; { int a; p = &a; } { int b; q = &b; } std::cout << (p == q) << '\n'; Note how both p and q point to objects that have already vanished. Is this legal?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
70
votes
8 answers

Is copying 2D arrays with "memcpy" technically undefined behaviour?

An interesting discussion has arisen in the comments to this recent question: Now, although the language there is C, the discussion has drifted to what the C++ Standard specifies, in terms of what constitutes undefined behaviour when accessing the…
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
70
votes
3 answers

Do C++ enums Start at 0?

If I have an enum that does not assign numbers to the enumerations, will it's ordinal value be 0? For example: enum enumeration { ZERO, ONE, TWO, THREE, FOUR, …
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
70
votes
2 answers

Can using a lambda in header files violate the ODR?

Can the following be written in a header file: inline void f () { std::function func = [] {}; } or class C { std::function func = [] {}; C () {} }; I guess in each source file, the lambda's type may be different and therefore the…
Alex Telishev
  • 2,264
  • 13
  • 15
69
votes
9 answers

Why are there two ways of expressing NULL in C?

According to §6.3.2.3 ¶3 of the C11 standard, a null pointer constant in C can be defined by an implementation as either the integer constant expression 0 or such an expression cast to void *. In C the null pointer constant is defined by the NULL…
Brad Jones
  • 815
  • 7
  • 10
69
votes
3 answers

Exact moment of "return" in a C++-function

It seems like a silly question, but is the exact moment at which return xxx; is "executed" in a function unambiguously defined? Please see the following example to see what I mean (here live): #include #include #include…
ead
  • 32,758
  • 6
  • 90
  • 153
68
votes
2 answers

Why is a double semicolon a SyntaxError in Python?

I know that semicolons are unnecessary in Python, but they can be used to cram multiple statements onto a single line, e.g. >>> x = 42; y = 54 I always thought that a semicolon was equivalent to a line break. So I was a bit surprised to learn (h/t…
alexwlchan
  • 5,699
  • 7
  • 38
  • 49
68
votes
4 answers

Could a C++ implementation, in theory, parallelise the evaluation of two function arguments?

Given the following function call: f(g(), h()) since the order of evaluation of function arguments is unspecified (still the case in C++11 as far as I'm aware), could an implementation theoretically execute g() and h() in parallel? Such a…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055