如何使用size_t类型的最大大小? [英] How to use maximum size of size_t type ?

查看:126
本文介绍了如何使用size_t类型的最大大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:


我对C编程有点新意,我正在尝试使用
为malloc写一个包装器。由于malloc需要

size_t作为参数,我希望能够

看看我的函数收到的参数是否小于

或者等于max size_t类型。


所以:


//我的包装函数mallocstr

char * mallocstr(size_t num)

{

// TEST

if(CODE HERES TO TEST FOR num< = max size of size_t){

printf(一些调试错误消息\ n);

返回NULL;

}

// END TEST

return(char *)malloc(num);

}

现在,忽略malloc将返回NULL的事实

不成功,我可能不需要测试。如果我想以上述方式构建我的代码,那么是否需要

任何_way_这样做?


IE ,替换CODE HERE ...与一些

测试相符,可以告诉我我的论点小于

等于size_t类型的最大大小。


我在gcc系统上,想要便携式地做这件事。

据我所知,没有gcc定义的宏

或任何值limit.h告诉我最大尺寸

size_t。


也许我错过了什么,但这看起来更难了

比它应该的好。


祝你好运,

--j

Hi:

I am a bit new to C programming and am trying to
write a wrapper around malloc. Since malloc takes
size_t as it''s parameter, I want to be able to
see if my function recieved an argument less than
or equal to the max size_t type.

So:

//my wrapper function mallocstr
char * mallocstr(size_t num)
{
//TEST
if (CODE HERE TO TEST FOR num <= max size of size_t) {
printf("some debug error message\n");
return NULL;
}
//END TEST
return (char *) malloc(num);
}
Now, ignore the fact that malloc will return NULL if
unsucessful and I may not need the test. If I _do_
want to structure my code in the above fashion, is there
any _way_ to do it ?

I.E., replace the "CODE HERE..." line with some
test that can tell me with my argument was less than
of equal to the max size of the size_t type.

I am on a gcc system and would like to do this portably.
As far as I can tell, there is no gcc defined macro
or any value in limit.h that tells me the max size of
size_t.

Maybe I am missing something, but this appears tougher
than it should be.

Best regards,

--j

推荐答案

javadesigner写道:
javadesigner wrote:

嗨:

我对C编程有点新意,我正在尝试编写一个包装器在malloc附近。由于malloc将
size_t作为参数,我希望能够看到我的函数是否收到了小于
或等于max size_t类型的参数。

所以:

//我的包装函数mallocstr
char * mallocstr(size_t num)
{
// TEST
if(CODE这里测试num< = max size of size_t){
printf(一些调试错误消息\ n);
返回NULL;
}
//结束测试
返回(char *)malloc(num);
}
现在,忽略如果
不成功则malloc将返回NULL的事实,我可能不需要考试。如果我想以上述方式构建我的代码,是否有任何_WAY>这样做?

I.E.,替换CODE HERE ...一些
测试可以告诉我我的论点小于
等于size_t类型的最大大小。

我在gcc系统上并希望为了便于这样做。
据我所知,没有gcc定义的宏
或limit.h中的任何值告诉我
size_t的最大大小。

也许我错过了一些东西,但这看起来比它应该更加坚固。

Hi:

I am a bit new to C programming and am trying to
write a wrapper around malloc. Since malloc takes
size_t as it''s parameter, I want to be able to
see if my function recieved an argument less than
or equal to the max size_t type.

So:

//my wrapper function mallocstr
char * mallocstr(size_t num)
{
//TEST
if (CODE HERE TO TEST FOR num <= max size of size_t) {
printf("some debug error message\n");
return NULL;
}
//END TEST
return (char *) malloc(num);
}

Now, ignore the fact that malloc will return NULL if
unsucessful and I may not need the test. If I _do_
want to structure my code in the above fashion, is there
any _way_ to do it ?

I.E., replace the "CODE HERE..." line with some
test that can tell me with my argument was less than
of equal to the max size of the size_t type.

I am on a gcc system and would like to do this portably.
As far as I can tell, there is no gcc defined macro
or any value in limit.h that tells me the max size of
size_t.

Maybe I am missing something, but this appears tougher
than it should be.




这是没有意义的。

你可以在函数中测试的任何值,

将在size_t的范围内。

适合size_t对象的最大值,

是((size_t)-1)。


C99中有一个SIZE_MAX宏。


-

pete



It''s pointless.
Any value that you can test within the function,
will be within the range of size_t.
The largest value that will fit in a size_t object,
is ((size_t)-1).

There''s a SIZE_MAX macro in C99.

--
pete


问候。


文章< bu ******* ****@netnews.upenn.edu>,javadesigner写道:
Greetings.

In article <bu***********@netnews.upenn.edu>, javadesigner wrote:
我在gcc系统上,并希望这样做便携。
目前为止我可以说,没有gcc定义的宏
或limit.h中的任何值告诉我
size_t的最大大小。

也许我错过了什么,但是这看起来比它应该更加坚固。
I am on a gcc system and would like to do this portably.
As far as I can tell, there is no gcc defined macro
or any value in limit.h that tells me the max size of
size_t.

Maybe I am missing something, but this appears tougher
than it should be.




C99(可能是更早的标准;我不记得了)定义SIZE_MAX

宏。最近版本的GCC支持它。


便携式后备是使用表达式(size_t)( - 1)。


问候,

Tristan


-

_

_V.-o Tristan Miller [ en,(fr,de,ia)]><空间有限

/ |` - '' - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =<> ;在ha句中,所以很难

(7_ \\ http://www.nothingisreal.com/ ><完成你的内容



C99 (and possibly earlier standards; I don''t remember) define the SIZE_MAX
macro. It''s supported by recent versions of GCC.

A portable fallback is to use the expression (size_t)(-1).

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-'' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it''s hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you


javadesigner< re ******** **********@yahoo.com>写道:
javadesigner <re******************@yahoo.com> wrote:
我对C编程有点新意,并且我试图围绕malloc写一个包装器由于malloc将
size_t作为参数,我希望能够看到我的函数是否收到了小于
或等于max size_t类型的参数。


Erm。如果你的函数需要一个size_t参数...

//我的包装函数mallocstr
char * mallocstr(size_t num)


....它确实如此,那么你无法获得一个不符合size_t的

参数。甚至如果有人试图将更多的数字传递给你的函数,那么它将被调制到
传递给mallocstr()之前的
大小。如果size_t可以签名,那么

大的参数会调用I-DB,但它需要是无符号的,所以

它总是被传递给你modulo SIZE_MAX。

return(char *)malloc(num);


除非你忘记了
#include< stdlib.h>,否则没有理由投出malloc(),btw。 malloc()返回一个void *,它完全有用

因为它可以容纳各种指针并且不需要强制转换。

现在,忽略这个事实如果
不成功,malloc将返回NULL,我可能不需要测试。如果我想以上述方式构建我的代码,是否有任何_way_这样做?


部分。取一个size_t,取一个uintmax_t并检查你的

参数是否为< = SIZE_MAX。因为在C89中都不存在这些,所以如果你没有C99,那么你将需要使用无符号长ans(size_t)-1。

这意味着你的函数不会像

malloc()那样采用相同的论证。

此外,size_t甚至可能大于unsigned long(至少,我

找不到它为什么不能的原因,这意味着mallocstr()

实际上比malloc()实际上有更小的范围。在C99中,使用

uintmax_t,这不会有问题。

据我所知,没有gcc定义的宏
或任何值limit.h告诉我
size_t的最大大小。
I am a bit new to C programming and am trying to
write a wrapper around malloc. Since malloc takes
size_t as it''s parameter, I want to be able to
see if my function recieved an argument less than
or equal to the max size_t type.
Erm. If your function takes a size_t argument...
//my wrapper function mallocstr
char * mallocstr(size_t num)
....which it does, then there''s no way you''re ever going to get an
argument that does _not_ fit in a size_t. Even if someone tries to pass
a larger number to your function, it''s going to be modulated down to
size before being passed to mallocstr(). If size_t could be signed, too
large arguments would invoke I-DB, but it''s required to be unsigned, so
it is always going to be passed to you modulo SIZE_MAX.
return (char *) malloc(num);
There is no reason to cast malloc(), btw, unless you have forgotten to
#include <stdlib.h>. malloc() returns a void *, which is useful exactly
because it can hold all kinds of pointers and doesn''t need casts.
Now, ignore the fact that malloc will return NULL if
unsucessful and I may not need the test. If I _do_
want to structure my code in the above fashion, is there
any _way_ to do it ?
Partially. Instead of a size_t, take a uintmax_t and check whether your
parameter is <=SIZE_MAX. Since neither of those exists in C89, you''ll
have to make do with unsigned long ans (size_t)-1 if you don''t have C99.
This means your function doesn''t take the same kind of argument as
malloc(), however.
Moreover, size_t could even be larger than unsigned long (at least, I
can''t find a reason why it couldn''t), which would mean that mallocstr()
would actually have a smaller range than malloc(). In C99, with
uintmax_t, that would not be a problem.
As far as I can tell, there is no gcc defined macro
or any value in limit.h that tells me the max size of
size_t.




如果你有一个预先C99版本的gcc,但是因为size_t是保证的

是无符号的,分配给无符号类型的值是模数改变的

类型的大小,如果它们超出范围,(size_t)-1会给你

size_t的最大值。当然,如果你首先拿一个

size_t,它仍然没什么用处。


Richard



Not if you have a pre-C99 version of gcc, but since size_t is guaranteed
to be unsigned and values assigned to unsigned types are changed modulo
the size of the type if they''re out of range, (size_t)-1 will give you
the maximum value of a size_t. It''s still not much use if you take a
size_t in the first place, of course.

Richard


这篇关于如何使用size_t类型的最大大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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