What type for subtracting 2 size_t's?

后端 未结 4 1542
花落未央
花落未央 2020-12-30 00:46

Which type in C should be used to represent the difference between two objects\' sizes?

As size_t is unsigned, something like

size_t dif         


        
相关标签:
4条回答
  • 2020-12-30 01:19

    There isn't a standard datatype that's guaranteed to be 100% safe for what you're trying to do. For evidence, imagine if size_t is really just uint64_t, which is entirely possible. Then there's no standard C data type that is guaranteed to have a positive range that matches uint64_t and also handles negative values.

    So the blunt answer to your question is "no datatype" (assuming strict adherence to standard C, which you seem to be desiring).

    You're not clear on your use-case, however, and it's possible you may be able to take advantage of modular arithmetic to handle "negative" values. For example, the following code results in d2 being 4 because of modular arithmetic, allowing the code to act as if size_t were signed:

    #include <stdio.h>
    #include <stdint.h>
    
    int main()
    {
        size_t d1 = sizeof(int32_t) - sizeof(int64_t);
        size_t d2 = sizeof(int64_t) + d1; // Add difference (even if d1 is "negative"*)
    
        printf("d1: %zu\n", d1);
        printf("d2: %zu\n", d2);
    
        return 0;
        // * By "negative" I mean that d1 would be negative if size_t were signed
    }
    

    Modular arithmetic may not be enough for you in your case, but for others it may.

    0 讨论(0)
  • 2020-12-30 01:33

    There is no signed C integer type that can hold all the values from a size_t value.

    0 讨论(0)
  • 2020-12-30 01:37

    When I am really worried about overflow issues like that (especially when working in modular arithmetic where "negative" values wrap somewhere other than ~0) I just split it into two cases:

    if (a > b) {
        size_t diff = a - b;
    } else {
        size_t diff = b - a;
        // code here subtracts diff rather than adds it, etc.
    }
    
    0 讨论(0)
  • 2020-12-30 01:40

    You could do this:

    size_t diff = std::abs(static_cast <int> (sizeof (small_struct) - sizeof (big_struct)));
    
    0 讨论(0)
提交回复
热议问题