Live data from Hacker News

Comp.lang.c Frequently Asked Questions

c-faq.com

11–20 of 56 posts

Re: Comp.lang.c Frequently Asked Questions

#11
post #7
post #3

Also worth reading is C infrequently asked questions https://www.seebs.net/faqs/c-iaq.html

For entertainment? Tone seems a bit over the top sarcastic.

If it is only the tone that bothers you, maybe you should abstain from programming C. ;-)

Re: Comp.lang.c Frequently Asked Questions

#12

1.1. How should I decide which integer type to use? Programmer: Hmmm, I think these three should be shorts and these 10 can fit in chars. Modern compiler: Fuck that, you're all 32-bits. I ain't got time for unaligned memory access...

And then you get to the unfortunate situation that is the x64 ABIs - int is still 32-bit, leading to a bunch of extra movsx instructions if you use ints for things like indexing.

Re: Comp.lang.c Frequently Asked Questions

#13

Earlier quoted context omitted.

Modern compilers generally prefer register-sized data.

Ah thanks, that's what I was thinking. But somehow I got caught up in unaligned cacheline accesses, which are much larger than 32-bits. It's early where I am. :)

I mean there are 4 different concepts at play here:

Unaligned memory access, types generally have an alignment which is the same as their length and structs have the alignment of their largest member because many architectures get quite cross with unaligned access e.g. trying to load a 32b integer from memory to register while it's not aligned to 32b, this is where compilers will pad structures to ensure everything is aligned properly by default, and where (in C/C++ anyway) you want to take care of your struct layout to avoid unnecessary padding. That doesn't prevent "these three should be shorts and these 10 can fit in chars." at all, but if you intersperse them the structure will increase in size due to padding.

Then there's C being specified in terms of lowest capabilities so while "char" and "short" are at least 8 and at least 16 bits… they can also be larger (POSIX does specify that char must be exactly 8 bits but non-POSIX platforms don't require that). That matches everything being 32b. It's a somewhat common occurrences for DSPs to have char of 16 or 32 bits, it really has nothing to do with the compiler (except insofar as the compiler does what the architecture description specifies).

Then there's the preferred data size, which is probably a function of the architectural ALUs than the native registers e.g. if an architecture only works on 32b datum then computations on 8 bit data will require loading 8 bits, zeroing the upper 24, performing the computation in 32b, copying whichever bit is concerned to the overflow flag, then zeroing that. Whereas performing the computation on a 32b-native type would require loading 32b, performing the computation, done (that was never a concern on x86/64 but IIRC older revisions of ARM could only natively work in 32b, and not all possible arithmetic operations got expanded to 8/16b at the same time).

And finally there's the cache line alignment. There are actually two opposite issues with cacheline alignments: you usually want your structure to not span cachelines (as that gets more expensive) but sometimes you want your structures be always start at the start of a cacheline (and possibly take the entirety of the cacheline) to avoid false sharing issues: if two unrelated structures are on the same cache line and they're used from different cores, the cores will need a lot more synchronisation than if they were on different cache lines.

Re: Comp.lang.c Frequently Asked Questions

#14

1.1. How should I decide which integer type to use? Programmer: Hmmm, I think these three should be shorts and these 10 can fit in chars. Modern compiler: Fuck that, you're all 32-bits. I ain't got time for unaligned memory access...

And then you get to the unfortunate situation that is the x64 ABIs - int is still 32-bit, leading to a bunch of extra movsx instructions if you use ints for things like indexing.

Use size_t to declare anything that will be used as an index.

Re: Comp.lang.c Frequently Asked Questions

#15

Earlier quoted context omitted.

And then you get to the unfortunate situation that is the x64 ABIs - int is still 32-bit, leading to a bunch of extra movsx instructions if you use ints for things like indexing.

Use size_t to declare anything that will be used as an index.

This is literally the reason `size_t` was created.

Re: Comp.lang.c Frequently Asked Questions

#17

While I don’t particularly like the structure of the FAQ (one page per item is definitely tedious to read through - I wish it came in a PDF or scrollable form) this is a goldmine of succinct and zero frills information. I learned a few things.

http://c-faq.com/versions.html has downloads for ascii versions (that may or may be up to date)

http://www.faqs.org/faqs/C-faq/faq/ has an ascii version from July 3, 2004 (again, I wouldn’t know whether that is up to date, but given its age, it may not be)

Re: Comp.lang.c Frequently Asked Questions

#18
post #3

Also worth reading is C infrequently asked questions https://www.seebs.net/faqs/c-iaq.html

Worth noting that parts of it are trying to be funny, parts of it are plain wrong, and parts of it are dangerously misleading. There may be one or two useful bits in it somewheres too.
Post reply on HN