什么类型减去2为size_t的? [英] What type for subtracting 2 size_t's?

查看:127
本文介绍了什么类型减去2为size_t的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种类型的C应该用于重新present两个对象'大小的区别?

由于为size_t 是无符号,像

 为size_t差异= sizeof的(small_struct) - 的sizeof(big_struct);

显然不会是正确的,在我看来,没有真正签署等价的。

ptrdiff_t的听起来很诱人,但


  1. 就像它的名字说,这是减去指针。

  2. 我读过,如类似DOS分段平台具有64K的最大对象的大小是由16位重新presentable。远指针,然而,是由一个16位段值和一个16位的偏移值。这大概不会令 ptrdiff_t的在这样一个平台上的32位呢?
    如果是这样,在大小两个对象之间的区别仅需要16位,但使用ptrdiff_t的会给你一个32位宽的变量,使得次优的。

那么,什么是合适的,便携式的这样一个价值的工作?

编辑:
我知道ssize_t供,但它


  1. 不是标准C的一部分。

  2. 其实不适用于上述用途,但用于返回无论是大小或(负)误差值。


解决方案

有不是唯一的保证是100%安全的你要怎样做一个标准的数据类型。对于证据,试想如果为size_t 真的只是 uint64_t中,这是完全可能的。然后还有就是保证有符合 uint64_t中积极的​​范围内没有标准的C数据类型,也处理负值。

那么生硬回答你的问题是没有数据类型(假设严格遵守标准C,你似乎渴望)。

您是不是对你的使用情况清楚,但是,它是可能的,你可能能够利用模运算来处理负的价值观。例如, D 2 以下code 结果正在 4 ,因为模运算,使code充当如果为size_t 签署:

 的#include<&stdio.h中GT;
#包括LT&;&stdint.h GT;诠释的main()
{
    为size_t D1 = sizeof的(int32_t) - 的sizeof(的int64_t);
    为size_t D2 = sizeof的(的int64_t)+ D1; //添加不同的(即使d1为负面*)    的printf(D1:%祖\\ n,D1);
    的printf(D2:%祖\\ n,D2);    返回0;
    // *由负面我的意​​思是D1是负的,如果为size_t签署
}

模运算可能是不够的,你在你的情况,但对于其他人那可就。

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

As size_t is unsigned, something like

size_t diff = sizeof (small_struct) - sizeof (big_struct);

obviously wouldn't be correct and it seems to me there's no real signed equivalent.

ptrdiff_t sounds kind of tempting, but

  1. like its name says it's for subtracting pointers.
  2. I've read that e.g. segmented platforms like DOS have a maximal object size of 64k which is representable by 16-bit. Far pointers, however, are composed of a 16-bit segment value and a 16-bit offset value. Wouldn't that make ptrdiff_t on such a platform 32-bit as well? If so, the difference between two objects in size only required 16-bit, but using ptrdiff_t would give you a 32-bit wide variable, making it sub-optimal.

So, what's a suitable, portable type for working with such a value?

Edit: I know about ssize_t, but it's

  1. not part of standard C.
  2. actually not intended for such use, but for returning either a size or a (negative) error value.

解决方案

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.

这篇关于什么类型减去2为size_t的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆