How to C (as of 2016)

10    08 Jan 2016 13:31 by u/el_cordoba

10 comments

2

I always use -O0 because I got sick of getting value optimized out in gdb.

edit: most of this article is fascist garbage, I'd take it all with a pinch of salt

3

edit: most of this article is fascist garbage, I'd take it all with a pinch of salt

I see "C" what you mean. The never use this, never use that garbage.

Every tool has it's uses and you should use the tools available to you where appropriate

0

-Os; everything else is snake water.

To me it doesn't read like fascist garbage; it reads like a list of reminders by the author for the author.

Types: On ARM, char is signed unless declared unsigned. On Intel, char is unsigned unless declared signed. Either way it is 8 Bit, and so is UTF-8. I have to agree that types, even if equivalent, should convey intention, if only for the benefit of maintenance. Then again, the elegance of C is that it has only about 20 keywords, from which you can build anything but self-referential structs. I like to keep things small and simple, but I trust the compiler to optimize human maintainable code.

I'll use long and short where I need 32 and 16 Bit types, and int where size doesn't matter and byte should be enough and I trust the compiler to put it in a register only. Most for loops, for example. I know int is long these days; I don't care. The compiler should be free to use as int whatever the CPU is most comfortable with, and that is what I use it for.

I may be persuaded to use int32_t for short, but I really see no advantage in it. (I really wouldn't mind a byte keyword to supplement char, if only so the strlen of a character array can intuitively reflect something other than the number of bytes. But that is basically syntactic sugar, and I should watch my weight.)

Return types:

if(!array=realloc(array)){free(array);array=NULL;}return array;

"Do not do this." No shit! How about a gore warning? Children might be reading this! Horrible!

All in all, a useable write-up, though I should expect most people here to be already familiar with the content.

0

Well, I write code that I cross compile on the Atari ST so this doesn't apply to me anyway, but "never use char"?