4 ms·
Third case, dealing with raw data from networks, from devices, from flash memory etc. where you actually need to manipulate every bit accurately. Fourth case -
by Nursie 13y ago
Third case, dealing with raw data from networks, from devices, from flash memory etc. where you actually need to manipulate every bit accurately.
Fourth case - situations in which you have a scalar quantity, and do not care about positive or negative.
Overflow is a separate issue and you must either make sure you either know the range of the data and pick an appropriately sized storage type, or explicitly check for overflows.
- audunw 13y ago> Third case, dealing with raw data from networks, from devices, from flash memory etc. where you actually need to manipulate every bit accurately. I just started working on this kind of application (well, USB protocols, but same issues) in Nimrod. First of all: just do "import unsigned" and you can work with unsigned just fine. I don't see why this is an issue, any more than that you have to do "import libusb". Second: I found that, knowing that unsigned types are a second-class citizen lead me towards writing a much better library. It encourages you to expose only signed integers to the next level up in the app. The next thing I realized is that this will very quickly cause Nimrod programmers to write and adopt a "Protocol Buffers"-like library for dealing with network protocols, binary files and similar things. And that will be a much better solution than dealing with things like unsigned integers and endianess in manual code.
- Nursie 13y agoSorry, wasn't saying it was an issue with the language, just another case where unsigned, raw data was useful. I don't really see how unsigned are that much of a problem that they have to be dealt with specially...
- rbehrends 13y ago> Fourth case - situations in which you have a scalar quantity, and do not care about positive or negative. This is actually where you have to be really careful, because there can be corner cases surrounding zero. E.g.: for (i=len-1; i>=0; i--) { ... } Yes, either ints or unsigned numbers can overflow, but underflowing a natural number is much more common. Don't take my word for it, ask Bjarne Stroustrup or Chandler Carruth: http://stackoverflow.com/questions/18795453/why-prefer-signed-over-unsigned-in-c http://stackoverflow.com/questions/18795453/why-prefer-signe...
- Nursie 13y agoIn that loop one should check that len is at least 1 first... I read that page, or at least some of it, it mostly seemed to be opinion rather than solid reason.
- rbehrends 13y ago> In that loop one should check that len is at least 1 first... No. The code is correct in that regard if you use int for i and len. Having to check len is an artifact of using unsigned. > I read that page, or at least some of it, it mostly seemed to be opinion rather than solid reason. I'm not talking about the comments. I'm talking about what Bjarne Stroustrup (the designer of C++) and Chandler Carruth (Google's LLVM lead) are saying in the video (and which is summarized in the top comment).
- Nursie 13y agoOnly if a sub 1 len is valid input, which it may not be. Either way the example is contrived and does not cover all other situations in which a scalar quantity is required. Not all are loop controllers. I did not watch the video, no time right now. The top comment tells us what they think, not why.
- pcwalton 13y agoI think this demonstrates a problem with C-style for loops (as opposed to iterators) more than unsigned integers.
- gnuvince 13y agoIt seems unreasonable to me that you need to write out the iteration variable 3 times in a C for-loop; I had a bug a couple weeks ago that took me longer to figure out than I care to admit. I wrote `for (int j = 0; i < N; ++j)`.
- rbehrends 13y agoThe problem lies with len-1 underflowing, not the loop that uses the result. For another example, to test congruence of x and y modulo m, you check if (x-y) % m == 0. This also blows up for unsigned integers if y > x unless m is a power of 2. Even iteration is not always over containers or some other structure in a way where the boundaries can be inferred and do not have to be computed, and not always in an order that is implemented by an iterator (for example, some in-place sorting algorithms).