language-lawyer

How are array and pointer types handled internally in C compilers? ( int *a; vs. int a[]; )

I need a language lawyer with authoritative sources. Take a look at the following test program which compiles cleanly under gcc: #include <stdio.h> void foo(int *a) { a[98] = 0xFEADFACE; } void bar(int b[]) { *(b+498) = 0xFEADFACE; } int main(int argc, char **argv) { int a[100], b[500], *a_p; *(a+99) = 0xDEADBEEF; *(b+499...

Sequence points and partial order

A few days back there was a discussion here about whether the expression i = ++i + 1 invokes UB (Undefined Behavior) or not. Finally the conclusion was made that it invokes UB as the value of 'i' is changing more than once between two sequence points. I was involved in a discussion with Johannes Schaub in that same thread. Accor...

Supress unused variable warning in C++ => Compiler bug or code bug?

Presently, I am using the following function template to suppress unused variable warnings: template<typename T> void unused(T const &) { /* Do nothing. */ } However, when porting to cygwin from Linux, I am now getting compiler errors on g++ 3.4.4 (On linux I am 3.4.6, so maybe this is a bug fix?): Write.cpp: In member function `vo...

How do I specify an integer literal of type unsigned char in C++?

I can specify an integer literal of type unsigned long as follows: const unsigned long example = 9UL; How do I do likewise for an unsigned char? const unsigned char example = 9U?; This is needed to avoid compiler warning: unsigned char example2 = 0; ... min(9U?, example2); I'm hoping to avoid the verbose workaround I currently h...

"Reading" a POD preincrement result does not yield undefined behavior. Why exactly?

This is a stupid question. :) [EDIT: stupid or not, this turned out to be a C++ peculiarity question, see UPDATE_2] Suppose we have: int a = 0; // line 1 int b = ++a; // line 2 What happens in line 2 is (note, numbers are just markers and do not specify exact order): = [1: write result of (3) to result of (2)...

Why can't I pass self as a named argument to an instance method in Python?

This works: >>> def bar(x, y): ... print x, y ... >>> bar(y=3, x=1) 1 3 And this works: >>> class Foo(object): ... def bar(self, x, y): ... print x, y ... >>> z = Foo() >>> z.bar(y=3, x=1) 1 3 And even this works: >>> Foo.bar(z, y=3, x=1) 1 3 But why doesn't this work? >>> Foo.bar(self=z, y=3, x=1) Traceback...

How is it legal to reference an undefined type inside a structure?

As part of answering another question, I came across a piece of code like this, which gcc compiles without complaint. typedef struct { struct xyz *z; } xyz; int main (void) { return 0; } This is the means I've always used to construct types that point to themselves (e.g., linked lists) but I've always thought you had to name t...

typedef and non-simple type specifiers.

Why is this code invalid? typedef int INT; unsigned INT a=6; whereas the following code is valid typedef int INT; static INT a=1; ? As per my understanding unsigned int is not a "simple type specifier" and so the code is ill-formed. I am not sure though. Can anyone point to the relevant section of the Standard which makes the fi...

How do you import an enum into a different namespace in C++?

I have an enum in a namespace and I'd like to use it as if it were in a different namespace. Intuitively, I figured I could use 'using' or 'typedef' to accomplish this, but neither actually work. Code snippet to prove it, tested on GCC and Sun CC: namespace foo { enum bar { A }; } namespace buzz { // Which of these two methods I ...

Implicit declaration in C

Does the following program invoke Undefined Behaviour in C? int main() { printf("Printf asking: Where is my declaration ?"); } In the above program there is an implicit declaration of printf(), so is the above code fully standard compliant or it just has some implementation specific behaviour? ...

Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value?

In C bitwise left shift operation invokes Undefined Behaviour when the left side operand has negative value. Relevant quote from ISO C99 (6.5.7/4) The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1× 2E2, reduced modulo on...