6 ms·
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 thi
by sdfdf4434r34r 5y ago
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.
- WalterBright 5y agoUse size_t to declare anything that will be used as an index.
- SavantIdiot 5y agoThis is literally the reason `size_t` was created.
- jstanley 5y agoWhy isn't it called index_t then?
- burnished 5y agoAs 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.
- jstanley 5y agoMy point was that it seems more likely that size_t was created to represent sizes than to represent indexes. 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.
- tialaramex 5y agoIt's named for sizeof, a C operator which returns a positive integer but annoyingly the core C language itself doesn't define what the type of that integer is, the standard library does though, naming it size_t Now, sizeof does measure the size of things, but, one of the obvious sizes you can measure is an array†, and that's definitely also the maximum index value for the array, so I do think it's fair to say that's (part of) literally why size_t exists. † One of C's treacherous footguns. In the scope where the array was defined it's an array, and sizeof(array) tells you how big that array is. But, passed as a parameter it becomes a pointer and sizeof(resulting_pointer) is the size of the pointer, not the array :(
- _kst_ 5y agoThe core language says that the result of sizeof is size_t, defined in <stddef.h> (and other headers). size_t is a typedef (alias) for an implementation-defined unsigned integer type. It could have been worded differently with the same meaning. For example, the core language (section 6 of the standard) could have said that sizeof yields a result of an implementation-defined unsigned integer type without referring to "size_t". The library section (section 7) already says that size_t "is the unsigned integer type of the result of the sizeof operator". Personally I think that referring to size_t in the language section adds clarity, even if it's slightly redundant. In a given implementation, a compiler might arrange for sizeof to yield a result of type unsigned long, for example. The corresponding <stddef.h> header must then define size_t as unsigned long for the implementation to be correct.
- Thorrez 5y ago>Now, sizeof does measure the size of things, but, one of the obvious sizes you can measure is an array†, and that's definitely also the maximum index value for the array Make sure to be careful and realize that sizeof my_array returns the number of bytes that my_array uses, not the number of elements. An array of 10 ints likely has sizeof == 40, while indexing past 9 would be undefined behavior.
- tialaramex 5y ago
- SAI_Peregrinus 5y agoBecause C doesn't really have indexes, it has pointers and offsets. offset_t would make more sense than index_t.
- ohazi 5y agoThere's also ssize_t and ptrdiff_t for offsets that might be negative. Use is pretty nuanced, read the docs before using, etc.
- SAI_Peregrinus 5y agoAnd intptr_t and uintptr_t for results of computations resulting in pointers. Sadly C's type system isn't really powerful enough to properly take advantage of these.
- deleted 5y ago[deleted]
- jamesfinlayson 5y agoI think the next (or current - I've lost track) version of C++ has an idx_t type which is an unsigned int of some sort, which will be the recommended type for for loops.
- _kst_ 5y agoI don't see a reference to "idx_t" anywhere in the latest draft of the C++ standard.
- jamesfinlayson 5y agoYep, I can't find anything either - I thought I read it four or five years ago in an interview with someone big in the C++ community.
- marcthe12 5y agoBecause its the return type of the sizeof operator