[英]strlen to return size_t?
In C: 在C中:
My string length function is returning a size_t
value? 我的字符串长度函数返回size_t
值?
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. 上下文:我有点喜欢使用gmp库并将大数字转换为字符串,但最终遇到了上述情况。
What kind of a string is that? 那是什么样的弦? Is my operating system playing a role in this issue? 我的操作系统是否在此问题上起作用? I use a MAC, 64-bit OS. 我使用的是MAC 64位操作系统。
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. @all:谢谢您的回答,但我认为我会将总线错误作为另一个问题,因为这似乎是一个不同的问题。
The problem is int
might be not wide enough to store the whole range of possible length values. 问题是int
可能不够宽,无法存储可能的长度值的整个范围。 For example on 64-bit you can have a string longer than 4 gigabytes and if int
is 32 bit you can't possibly return length of such a long string via an int
variable. 例如,在64位上,您可以拥有一个大于4 GB的字符串,如果int
为32位,则无法通过int
变量返回如此长的字符串的长度。
POSIX strlen()
does return size_t
. POSIX strlen()
确实返回size_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. 一种可能是您造成了缓冲区溢出或使用NULL
指针做了一些您不应该做的事情。
strlen()
always returned size_t
... and the POSIX standard also says that. strlen()
总是返回size_t
...,POSIX标准也这样说。
I guess the reason is that int
has sign and the capacity of even an unsigned 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. 我猜想原因是int
有符号,即使是unsigned int
的容量也可能不足以容纳元素的大小(例如,如果在x86-64上具有16GB RAM的32bit int)...这个例子是极端的,但可能。
strlen()
returns a size_t
since at least ISO C90 -- I just checked in my copy. 至少从ISO C90开始, strlen()
返回size_t
-我刚刚签入副本。 And this standard should have no technical difference with ANSI C89. 并且该标准与ANSI C89应该没有技术上的区别。
There was a change of convention ( size_t
wasn't in K&R C), but it was a long time ago. 约定发生了变化(在K&R C中未使用size_t
),但这是很久以前的事情了。
要解决您的警告(实际上是一个错误-您通过将错误的类型传递给printf
来调用未定义的行为),您应该使用%zu
而不是%d
来打印size_t
值。
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-in sizeof
operator yields a size_t
result as well. 对于内存块的长度,标准库中的所有函数都可以使用size_t
是一个非常简单且合乎逻辑的原因-内置的sizeof
运算符也会产生size_t
结果。
Moreover, size_t
is unsigned, of a particular size, tied to the architecture and is semantically different than just a generic int
which is meant for storing any number from the count of trees around your office to your SO reputation. 此外, size_t
是无符号的,具有特定的大小,与体系结构相关,并且在语义上不同于仅用于存储从办公室周围树木到您的SO名声的任何数字的通用int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.