strlen 返回 size_t?
在C中:
我的字符串长度函数返回 size_t
值?
为什么它不返回传统的整数?我注意到的另一件事是,当我尝试将此字符串与另一个字符串连接时,我在运行程序时收到了总线错误。
背景:我正在使用 gmp 库并将大数字转换为字符串,最终遇到了上述情况。
那是一根什么样的绳子?我的操作系统是否在这个问题中发挥了作用?我使用的是 MAC 64 位操作系统。
编辑:我收到的错误消息是:
: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘size_t’
谢谢!
@all:感谢您的回答,但我想我会将总线错误作为另一个问题,因为这似乎是一个不同的问题。
In C:
My string length function is returning a size_t
value?
Why is it not returning a integer which is conventional? And one more thing I noticed was that when I was trying concatenate this string with another string I received a bus error when I ran the program.
Context: I was kind of playing with gmp library and converting big numbers to strings and I end up with the above situation.
What kind of a string is that? Is my operating system playing a role in this issue? I use a MAC, 64-bit OS.
Edited: The error message I received was:
: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘size_t’
Thanks!
@all: Thanks for the answers but I thought I will put the bus error as another question because it seems to be a different issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题是
int
可能不够宽,无法存储可能长度值的整个范围。例如,在 64 位上,您可以拥有超过 4 GB 的字符串,如果int
是 32 位,您不可能通过int
返回如此长字符串的长度多变的。The problem is
int
might be not wide enough to store the whole range of possible length values. For example on 64-bit you can have a string longer than 4 gigabytes and ifint
is 32 bit you can't possibly return length of such a long string via anint
variable.strlen()
总是返回size_t
...并且 POSIX 标准也这么说。我猜原因是
int
有符号,即使是unsigned int
的容量也可能不足以容纳元素的大小(假设你有一个 32 位 int) x86-64 和 16GB RAM) ...这个例子是极端的,但也是可能的。strlen()
always returnedsize_t
... and the POSIX standard also says that.I guess the reason is that
int
has sign and the capacity of even anunsigned int
might not be enough for holding size of an element (say if you have a 32bit int on x86-64 with 16GB RAM) ... the example is extreme, but possible.POSIX
strlen()
确实返回size_t< /代码>
。
至于导致总线错误的原因,如果不查看代码并了解有关更改的确切性质的更多详细信息,就不可能说出来。一种可能性是您导致了缓冲区溢出或使用
NULL
指针执行了不应该执行的操作。POSIX
strlen()
does returnsize_t
.As to what's caused the bus error, it's impossible to say without seeing the code and knowing more details about the exact nature of your changes. One possibility is that you've caused a buffer overrun or did something with a
NULL
pointer you shouldn't have done.要解决您的警告(这实际上是一个错误 - 您通过将错误的类型传递给
printf
调用了未定义的行为),您应该使用%zu
而不是% d
用于打印size_t
值。To address your warning (which is actually an error - you've invoked undefined behavior by passing the wrong type to
printf
) you should use%zu
rather than%d
for printingsize_t
values.strlen()
返回一个size_t
,至少从 ISO C90 开始——我刚刚签入了我的副本。而且这个标准应该和ANSI C89没有技术上的区别。约定发生了变化(
size_t
不在 K&RC 中),但那是很久以前的事了。strlen()
returns asize_t
since at least ISO C90 -- I just checked in my copy. And this standard should have no technical difference with ANSI C89.There was a change of convention (
size_t
wasn't in K&R C), but it was a long time ago.当涉及到内存块的长度时,标准库中的所有函数都可以与
size_t
一起使用,有一个非常简单且合乎逻辑的原因 - 内置的sizeof
运算符也会产生size_t
结果。此外,size_t 是无符号的,具有特定的大小,与体系结构相关,并且在语义上与通用的 int 不同,后者用于存储树计数中的任何数字在您的办公室周围,以提高您的声誉。
There is a very simple and logical reason for all of the functions from the standard library to work with
size_t
when it comes to lengths of memory blocks - the built-insizeof
operator yields asize_t
result as well.Moreover,
size_t
is unsigned, of a particular size, tied to the architecture and is semantically different than just a genericint
which is meant for storing any number from the count of trees around your office to your SO reputation.