什么是找到为size_t的最大值的便携式方法? [英] What is a portable method to find the maximum value of size_t?

查看:138
本文介绍了什么是找到为size_t的最大值的便携式方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的程序运行的系统为size_t的最大值。
我的第一直觉是使用负1,像这样:

I'd like to know the maximum value of size_t on the system my program is running. My first instinct was to use negative 1, like so:

size_t max_size = (size_t)-1;

但我猜有一个更好的办法,还是地方定义的常量。

But I'm guessing there's a better way, or a constant defined somewhere.

推荐答案

一个清单常数(宏)存在于C99,它被称为 SIZE_MAX 。有没有在C89没有这样的常数/ 90虽然。

A manifest constant (a macro) exists in C99 and it is called SIZE_MAX. There's no such constant in C89/90 though.

不过,你有你的原来的职位是什么找到为size_t 的最大值的完美移植方法。这是保证任何无符号类型的工作。

However, what you have in your original post is a perfectly portable method of finding the maximum value of size_t. It is guaranteed to work with any unsigned type.

这篇关于什么是找到为size_t的最大值的便携式方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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