Earlier quoted context omitted.
Use size_t to declare anything that will be used as an index.
This is literally the reason `size_t` was created.
Comp.lang.c Frequently Asked Questions
21–30 of 56 posts
Re: Comp.lang.c Frequently Asked Questions
#22Also 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.
Re: Comp.lang.c Frequently Asked Questions
#23Interesting that modern languages like rust, go, and zig all lean towards using integer types with well defined sizes. I think that we've learned that the less exact definitions in c can cause problems. For example, if you develope and test in an environment where int is 32 bits, you could easily end up with bugs that only exist if int is 16 bits.
Re: Comp.lang.c Frequently Asked Questions
#24Re: Comp.lang.c Frequently Asked Questions
#25> Most programs do not need precise control over these sizes; many programs that do try to achieve this control would be better off if they didn't. Interesting that modern languages like rust, go, and zig all lean towards using integer types with well defined sizes. I think that we've learned that the less exact definitions in c can cause problems. For example, if you develope and test in an environment where int is…
I'm fond of Ada's approach: generally you just specify the range you want and let the compiler figure out the best integer size to use. You only need to specify a size if you're doing low-level work or FFI.
Re: Comp.lang.c Frequently Asked Questions
#26Why do some people write if(0 == x) instead of if(x == 0)?
Re: Comp.lang.c Frequently Asked Questions
#27> Most programs do not need precise control over these sizes; many programs that do try to achieve this control would be better off if they didn't. Interesting that modern languages like rust, go, and zig all lean towards using integer types with well defined sizes. I think that we've learned that the less exact definitions in c can cause problems. For example, if you develope and test in an environment where int is…
In my experience, modern code is either written almost entirely in terms of sized values like int32_t (when assuming a "normal" platform), or almost entirely in terms of _least/_fast values (when going for maximum portability). The only case that "naked" int/long is still common in this model is in a few specific uses such as loop indices where it's idiomatic.
The _fast and _least types are especially interesting, pragmatically. "int_fast8_t" means "I need a signed value that can store values between -128 and 127, doesn't have defined behavior on overflow, and is allowed to fill a register / use a word worth of memory." "int_least8_t" means "I need a signed value that can store values between -128 and 127, doesn't have defined behavior on overflow, and I am likely to have a significant number of these in memory so please pack them as efficiently as reasonable." (And of course we have bit packing when we need to pack more efficiently than reasonable; bit-packing with int_fast8_t allows you to represent "I need this to take exactly 8 bits of memory, whatever it costs compute-wise, because I'm basically filling memory with these.")
In general, I find that these types give modern C a good balance between declaring requirements on types and not overly trying to control their size.
Re: Comp.lang.c Frequently Asked Questions
#28Newsgroup FAQs are a goldmine. I still find myself referring to the comp.graphics.algorithms FAQ repeatedly: http://www.faqs.org/faqs/graphics/algorithms-faq/
Re: Comp.lang.c Frequently Asked Questions
#29Earlier quoted context omitted.
This is literally the reason `size_t` was created.
Why isn't it called index_t then?
Re: Comp.lang.c Frequently Asked Questions
#30Earlier quoted context omitted.
Why isn't it called index_t then?
As a fellow student of "why is this named that, what does this name mean" I have found that asking about counterfactuals is rarely satisfying. Naming stuff is hard and the question assumed a level of intent that I think is rarely present. Asking "how did this thing get named that" works out better, cause it seems that occasionally gets written down.
I don't disagree that size_t is an appropriate type for indexes, but I don't think indexes are literally the reason it was created.