size_t 到 unsigned int (来自 API 函数)

发布于 2024-11-02 03:08:07 字数 565 浏览 3 评论 0原文

我正在使用 Oracle API 访问数据库,该 API 有一个函数 readBuffer(char * buffer, unsigned int size); ,我无法对其进行任何更改。

我有一个使用此 API 的类,并且我的函数的签名当前采用 std::stringunsigned int 作为大小,问题是当我通过std::string.size() 到我的函数的 size 参数,我从编译器收到警告,从 size_t 转换为 unsigned int 可能会导致数据丢失。

我想知道是否有有效的方法size_t转换为unsigned int,这样我就可以将它传递给我的API而不会收到警告来自编译器?

我理解 size_t 的目的,并且在 google 上搜索此转换会出现很多结果,显示“更改函数以采用 size_t arg”,但在这种情况下我无法更改 API 的签名。

有什么建议吗?

I am using the Oracle API to access a database and this API has a function readBuffer(char * buffer, unsigned int size); to which I cannot make any changes.

I have a class that uses this API and the signature of my function currently takes a std::string and an unsigned int for the size, the problem is that when I pass std::string.size() to the size argument of my function, I get a warning from my compiler that converting from size_t to unsigned int could cause data loss.

I wondered if there is a valid way to convert the size_t to an unsigned int so I can pass it to my API and not get a warning from the compiler?

I understand the purpose of size_t and searching google for this conversion turns up a lot of results that say "change the function to take a size_t arg" but I CANNOT change the signature of my API in this case.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

‘画卷フ 2024-11-09 03:08:07

是的,编写一个辅助函数来检查此类转换是否有效,否则抛出异常。像这样的东西:

unsigned int convert( size_t what )
{
    if( what > UINT_MAX ) {
       throw SomeReasonableException();
    }
    return static_cast<unsigned int>( what );
}

Yes, write a helper function that will check whether such conversion is valid and throw an exception otherwise. Something like:

unsigned int convert( size_t what )
{
    if( what > UINT_MAX ) {
       throw SomeReasonableException();
    }
    return static_cast<unsigned int>( what );
}
那小子欠揍 2024-11-09 03:08:07

好吧,执行一个static_cast(mystring.size())

原因是 std::size_t 通常是指针大小,但在 64 位平台上,int 仍然是 32 位。在这种情况下,数据丢失的唯一原因是相关字符串的长度超过 2^32 字节。

如果您知道这种情况不会发生,请在某处放置一个 assert 来捕获这种情况,并将编译器 static_cast 置于静默状态。

Well, do a static_cast<unsigned int>(mystring.size()).

The reason is that std::size_t is usually pointer-size, but there are 64 bit platforms on which an int is still 32 bits. In this case, the only reason for data loss would be if the string in question had a length of more than 2^32 bytes.

If you know that this won't happen, put an assert somewhere to catch this case and static_cast the compiler to silence.

許願樹丅啲祈禱 2024-11-09 03:08:07
static_cast<unsigned int>(str.size());

如果你想变得偏执:

if (static_cast<unsigned int>(str.size()) != str.size()) 
  throw ...
static_cast<unsigned int>(str.size());

If you want to be paranoid:

if (static_cast<unsigned int>(str.size()) != str.size()) 
  throw ...
春花秋月 2024-11-09 03:08:07

您可以使用构造强制转换

static_cast<unsigned int>(your_variable)

。当然,正确的方法是 API 接受 size_t...

You can force the conversion using the

static_cast<unsigned int>(your_variable)

construct. Of course the correct way would be for the API to accept size_t...

辞别 2024-11-09 03:08:07

这里的风险是 size_t 可能大于 (unsigned) int,因此在这种情况下您无法安全地进行转换。

例如,可以想象 int 是 32 位,而 size_t 是 64 位。我不知道这样的系统/配置在我的脑海中,但它可能会发生。

对于大多数“合理”的系统,两者都至少为 32 位,并且单个 4 GB 字符串仍然(也许)不太可能出现。

因此,您可以直接进行强制转换,这将是有效的,但对于所有可能的系统和极端情况来说,它并不“安全”。

The risk here is that size_t might be larger than (unsigned) int, and thus you cannot safely convert if that is the case.

For instance, it's conceivable that int is 32 bits, while size_t is 64 bits. I don't know such a system/configuration off the top of my head, but it could occur.

For most "reasonable" systems, both will be at least 32 bits, and a single 4 GB string is still (perhaps) unlikely to occur.

So then you could just cast, that would be valid but it would not be "safe" for all possible systems and corner cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文