News for hackers. Stuff that matters.
Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @02:23PM (#14361254)
There's a big mistake here:

  // Bogus function, just to see how arguments are passed.
  void bogus();

  // Invoke bogus using ptr.
  void do_ptr() { bogus(&ptr, ptr); }

  // Invoke bogus using arr.
  void do_arr() { bogus(&arr, arr); }
"&ptr" and "&arr" are not the same. &ptr gives you a pointer to a pointer (char * *), but &arr just gives you a pointer to the array data. That's why the code is different.

This would normally show up as a compilation error, since &ptr and &arr have different types. It's disguised here by the variadic bogus() function.

(I didn't know that no-arg functions in C are implicitly variadic! That's bizarre. I would have expected modern compilers to disable that by default, but I can't get Clang to warn me about it even with -Wall.)

Re:Declaring C String Constants by kccqzy on Wednesday May 17, 2017 @02:34PM (#14361414)
It's not really variadic in the sense that you can't use va_start and friends to access the arguments. Instead, if the actual definition doesn't match the call, it's undefined behavior and the compiler can't catch it.
Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @03:00PM (#14361685)
Undefined behavior strikes again!

It really seems like the C standard maintainers and compiler vendors should just get together and sort that whole mess out. It can give you a few extra percent in some benchmarks, sure, and help C keep its mostly-deserved reputation as the fastest language, but the cost is huge.

If these non-type-checked functions are really useful in some very unusual use case, give it a special weird syntax. Don't make it the default and inflict it on everybody whether they want it or not.

Re:Declaring C String Constants by gumby on Wednesday May 17, 2017 @03:27PM (#14361950)
> It really seems like the C standard maintainers and compiler vendors should just get together and sort that whole mess [Undefined behavior] out.

Why do you think there is undefined behavior? Because committee members are lazy?

Not every machine is a C machine (PDP-11). I used to program machines with bytes that could range from 1-36 bits (these are older than the PDP-7/PDP-11 that C was designed for). A joy to program, but the C model did not apply. You may consider these architectures obsolete, and they are, but many decisions in the design of IP, UDP and TCP (and some higher level protocols) still reflect these machines.

Early C standardization efforts bent backwards to support the architectures committee members cared about. Some constraints (undef behavior etc) were removed in later standards (though the C++ committee is more aggressive in this regard than the C committee).

And you well could argue that C's tight coupling to the architecture of the PDP-11 and the programming standards of the 70s holds back CPU design. Many of these undefined areas are places where compilers have the opportunity to take advantage of CPU features (e.g. vector hardware).

The committee mailing list and other discussions are open -- have a read. And if you feel strongly you can even join the committee.

Re:Declaring C String Constants by derefr on Wednesday May 17, 2017 @08:18PM (#14363959)
AFAICT, "undefined behavior" refers to two entirely-separate classes of behavior:

1. cases where the compiler explicitly recognizes a certain permutation of compilation-state as matching some pattern for what the standard calls "undefined behavior"—and then is unconstrained as to what it does in response, but indeed does something in response to detecting that state;

2. cases where the compiler has no compile-time static-analysis approach available for detecting a certain edge-case (e.g. integer overflow), and, further, is inhibited by the standard from generating code to detect that edge-case at runtime either; so the compiler is forced to leave the response to that condition either up to the programmer to explicitly handle, or up to the target architecture to respond to however it likes when it happens.

It's clear to me that undefined behavior of the second class is sort of just part of what C "is": a language where the complexity-classes of all the primitives [well, except FP math] are constrained at compile-time, so a line of code that's O(1) on one arch won't get runtime-polyfilled into being O(N) on another in order to "make the arch sane."

But my question, then, is this: would it break anything about support for the "not C machine" architectures, if the standard said that everything falling into the first class—explicit undefined behavior—was instead defined behavior, defined as "abort compilation"?

Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @08:26PM (#14363996)
I don't know, but I would dearly love to use a compiler that does that.
Re:Declaring C String Constants by gumby on Thursday May 18, 2017 @12:28AM (#14364817)
> I don't know, but I would dearly love to use a compiler that does that.

Well you're in luck, since gcc -Werror when used with many of its -W flags will do precisely that.

You guys should read the minutes of committee discussions and position papers. "undefined behavior" is not something introduced to make programmers' lives hard!

Also look at IEEE734 which does define things to an incredible degree -- it's extremely hard to use (as it needed to be!). It's one of the very best standards of all time, and still has, yes, undefined behavior.

Re:Declaring C String Constants by derefr on Thursday May 18, 2017 @12:35AM (#14364835)
> Well you're in luck, since gcc -Werror when used with many of its -W flags will do precisely that.

That's not lucky; I'd argue it's the opposite. An ecosystem built around compilers that default to not using -Werror (or rather: `-Werror=pedantic -pedantic-errors`) is an ecosystem that encourages people to write code that relies on the optimizations compilers make in response to undefined behavior. Which, undoubtedly, "makes [maintenance] programmers' lives hard," when they change one little thing and suddenly the code gets 10x slower because now e.g. a dead loop isn't being elided.

If all compilers defaulted to something like `-Werror=pedantic -pedantic-errors`—and it was highly discouraged to turn those flags off—then jumping in to contribute to a portable, sprawling codebase would be infinitely simpler, because the codebase would have been written in a far more rigorous way up to that point.

Sunlight is the best disinfectant—so why do we allow our compiler-vendors to default the lights to off?

Re:Declaring C String Constants by iainmerrick on Thursday May 18, 2017 @11:28AM (#14368032)
Amen! I'm not a big Go fan but Go definitely has the better approach here.
Re:Declaring C String Constants by iainmerrick on Thursday May 18, 2017 @11:24AM (#14367989)
To clarify, it's not actually undefined behavior per se that I'm complaining about, it's the modern trend of highly optimizing compilers which take undefined behavior as an excuse to do all sorts of ridiculous things to your code.

"You may have thought you were checking for overflow here, but technically speaking the result of that operation could be undefined, therefore this code could do literally anything, or nothing. So I'll just remove it all. Congratulations, now it runs faster!"

I honestly hadn't realized that was a controversial view. Doesn't almost everyone dislike that? Are there a lot of C programmers who are happy to accept that risk in exchange for slightly better optimization?

Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @04:06PM (#14362328)
I realize it's there for a reason -- as I understand it, to avoid constraining compilers into generating unnecessarily awkward code for unlikely edge cases. And that fits with one of C's main advantages, being fast and lightweight.

But in the last few years I've read many, many articles about problems that crop up when optimizing C compilers do weird things to reasonable-looking code, citing "undefined behavior" as an excuse. I can't say for sure if I've hit issues like that myself, but I've certainly used libraries that have.

Maybe the problem lies squarely with the compiler vendors, but the language spec enables them.

Re:Declaring C String Constants by geezerjay on Saturday May 20, 2017 @02:46AM (#14381323)
> do weird things to reasonable-looking code, citing "undefined behavior" as an excuse.

If the behaviour is undefined then the code is broken. Programmers need to learn the language instead of putting together hail mary code expecting it to somehow work in spite of the language specifically telling them that they should not expect any defined behavior out of it.

People who whine about undefined behavior are like kids who insist in jamming their fingers in a electric socket and whine when they get a shock.

Re:Declaring C String Constants by iainmerrick on Tuesday May 30, 2017 @04:28PM (#14448279)
It would be nice for the compiler to be a friend and ally, rather than a mischievous opponent.
Re:Declaring C String Constants by Garfgon on Wednesday May 17, 2017 @03:22PM (#14361898)
I think it's backward compatibility, not speed. Functions with no arguments lists were supported in C89, so changing it now will break someone's code somewhere.

In fact not specifying a parameter list may make your code slower, as (IIRC) default argument promotions are always applied to parameters passed to such a function.

Re:Declaring C String Constants by JdeBP on Wednesday May 17, 2017 @03:12PM (#14361809)
They did get together to sort the mess out. They did so in the 1980s. Where do you think the idea of "ANSI function prototypes" came from?
Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @04:09PM (#14362354)
It's hardly a complete solution, though, since they kept the zero-arg functions around as a special case!

ANSI C is the version I first learned. I didn't know that weird exception for zero-args functions still existed, or had forgotten it. I could be wrong, but I bet most C programmers don't know about it.

Re:Declaring C String Constants by drt1245 on Wednesday May 17, 2017 @02:36PM (#14361444)
Additionally, he keeps talking about dereferencing the pointer, which I don't think is right. The pointer never gets deferenced in the code shown.

I'm not an x86 guru, but I think that "movq ptr(%rip), %rsi" is different because ptr needs to be moved from relative to the instruction pointer (because it is on the stack, as a non-const variable).

Re:Declaring C String Constants by bonzini on Wednesday May 17, 2017 @02:57PM (#14361662)
It's a global variable, it's not on the stack. It's in the data section.

$arr is copying the address of "arr", so it must use an immediate move (possibly 64-bit). It could also use RIP-relative with the "lea" (load effective address) instruction.

$ptr(%rip) is accessing memory, so it uses RIP-relative addressing with the "mov" instruction.

Re:Declaring C String Constants by magnetic on Wednesday May 17, 2017 @04:20PM (#14362429)
> It's a global variable, it's not on the stack. It's in the data section.

And because it's a const, it's in the .rodata section... (read-only) Playing too liberally with the data often leads to a SEGV (as it should).

Re:Declaring C String Constants by AbacusAvenger on Thursday May 18, 2017 @08:27AM (#14366622)
The code is more interesting if you -do- actually dereference the pointer, too. I've changed the prototype of "bogus" to take a single char as the first argument, and dereference the two pointers inside the do_arr/do_ptr functions:

https://goo.gl/jZYx0f

So the "do_arr" version "knows" what value is at * arr because "arr" is immutable, so the compiler can just choose to load a constant. The "do_ptr" version has to load from memory instead.

But what if we tell the compiler that the value of "ptr" won't change (i.e. "char * const" instead of "const char * ")? The code becomes the same:

https://goo.gl/iNaUHe

So basically "const char []" and "char * const" are more logically equivalent here.

Re:Declaring C String Constants by novia on Wednesday May 17, 2017 @05:17PM (#14362863)
This is a little off topic, but does anyone have any good resources to help me wrap my head around pointers in C? Right now they are very confusing to me.
Re:Declaring C String Constants by Zelizz on Wednesday May 17, 2017 @06:12PM (#14363282)
Imagine all memory is a big array. A pointer is just an index into that array. A pointer dereference is like accessing something at an array index. A pointer to a pointer is array index to a location where you'll find another array index.
Re:Declaring C String Constants by novia on Thursday May 18, 2017 @06:49AM (#14366180)
I understand pointers conceptually. It's the syntax (in C specifically) that is giving me trouble. I am fine with pointers in ASM.
Re:Declaring C String Constants by iainmerrick on Thursday May 18, 2017 @11:33AM (#14368088)
Hmm, it's been a long time since I read it, but this sounds like something that good old K&R probably does well. It's famously short and well-written.

It's old-fashioned but I assume (maybe others can correct me) the latest edition is up to date enough that it won't teach you any outright bad habits.

Re:Declaring C String Constants by novia on Thursday May 18, 2017 @11:46AM (#14368217)
Thank you very much! I'll give it a look.
Re:Declaring C String Constants by kccqzy on Wednesday May 17, 2017 @02:38PM (#14361478)
You are right. This is RIP-relative addressing.
Re:Declaring C String Constants by justinmk on Wednesday May 17, 2017 @03:28PM (#14361964)
> I didn't know that no-arg functions in C are implicitly variadic! That's bizarre. I would have expected modern compilers to disable that by default, but I can't get Clang to warn me about it even with -Wall.

Try -Wstrict-prototypes, then `void` is required.

Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @04:26PM (#14362474)
That works in GCC, but not in Clang. As far as I can tell Clang has no warnings relating to this at all.
Re:Declaring C String Constants by eridius on Thursday May 18, 2017 @02:24AM (#14365193)
BTW -Wall doesn't give you all warnings. It gives you a specific set, which will never change because of all the legacy codebases using -Wall that Clang doesn't want to cause problems with when introducing new diagnostics. The flag that really gets you all of the diagnostics is -Weverything.
Re:Declaring C String Constants by 0xffff2 on Wednesday May 17, 2017 @02:24PM (#14361286)
>(I didn't know that no-arg functions in C are implicitly variadic! That's bizarre. I would have expected modern compilers to disable that by default, but I can't get Clang to warn me about it even with -Wall.)

I didn't know that either. I wonder if `void bogus(void)` behaves any differently?

Re:Declaring C String Constants by Koshkin on Wednesday May 17, 2017 @02:33PM (#14361403)
Yes, in C you must use 'void' to indicate that the function takes no arguments. (This is deprecated in C++ in favor to an empty argument list - which breaks backward compatibility with C).
Re:Declaring C String Constants by nercht12 on Wednesday May 17, 2017 @05:23PM (#14362920)
What happens if I extern a C function with no "void" then? Do I get a variadic in C++?
Re:Declaring C String Constants by Koshkin on Thursday May 18, 2017 @10:59AM (#14367773)
C++ is not backwards compatible with C in this regard.
Re:Declaring C String Constants by aidenn0 on Thursday May 18, 2017 @01:45AM (#14365049)
It's not variadic, it's unspecified arguments. The compiler ought to complain if two function calls are made with conflicting argument types.
Re:Declaring C String Constants by kccqzy on Wednesday May 17, 2017 @02:31PM (#14361368)
Yes it would behave differently. Putting void in the parenthesis tells the compiler the function accepts no arguments at all.
Re:Declaring C String Constants by 0xffff2 on Wednesday May 17, 2017 @02:43PM (#14361529)
Good to know. I never bothered with `void` inside the argument list in C before (I primarily write C++, very rarely C). I will have to remember to be more explicit when I do write C code in the future.
Re:Declaring C String Constants by a_t48 on Wednesday May 17, 2017 @02:30PM (#14361342)
You speak the truth! &arr is a "char (*)[12]"!
Re:Declaring C String Constants by khaled on Wednesday May 17, 2017 @02:36PM (#14361441)
GCC has -Wstrict-prototypes.
Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @04:14PM (#14362388)
Strange that that isn't included in -Wall! I knew -Wall isn't the same as -Weverything, but I'm always surprised at what it omits. Why wouldn't you want that warning?

Edit to add: Aha, my 'gcc' was actually Clang because I'm on OS X. Installing the real GCC, I find:

gcc -Wstrict-prototypes does what I want and disallows this feature. But -Wall doesn't include that flag, and GCC doesn't seem to have -Weverything.

Clang accepts -Wstrict-prototypes but doesn't catch no-arg functions! That's bad. Even with -Weverything, Clang lets you do anything you want with no-arg functions. Very bad.

Re:Declaring C String Constants by khaled on Wednesday May 17, 2017 @06:21PM (#14363341)
I think it is not included in -Wall because such prototype are legitimate pre-ANSI K&R C function prototypes.
Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @08:30PM (#14364011)
Hmm, could be! Maybe explicitly specifying a newer standard might do the trick then. I'll try a few things.