strlen 返回 size_t?

发布于 2024-11-02 03:54:16 字数 412 浏览 4 评论 0原文

在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 技术交流群。

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

发布评论

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

评论(6

流心雨 2024-11-09 03:54:16

问题是 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 if int is 32 bit you can't possibly return length of such a long string via an int variable.

紫罗兰の梦幻 2024-11-09 03:54:16

strlen() 总是返回 size_t ...并且 POSIX 标准也这么说。

我猜原因是 int 有符号,即使是 unsigned int 的容量也可能不足以容纳元素的大小(假设你有一个 32 位 int) x86-64 和 16GB RAM) ...这个例子是极端的,但也是可能的。

strlen() always returned size_t ... and the POSIX standard also says that.

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.

兲鉂ぱ嘚淚 2024-11-09 03:54:16

POSIX strlen() 确实返回 size_t< /代码>

至于导致总线错误的原因,如果不查看代码并了解有关更改的确切性质的更多详细信息,就不可能说出来。一种可能性是您导致了缓冲区溢出或使用 NULL 指针执行了不应该执行的操作。

POSIX strlen() does return 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.

无力看清 2024-11-09 03:54:16

要解决您的警告(这实际上是一个错误 - 您通过将错误的类型传递给 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 printing size_t values.

掩饰不了的爱 2024-11-09 03:54:16

strlen() 返回一个 size_t,至少从 ISO C90 开始——我刚刚签入了我的副本。而且这个标准应该和ANSI C89没有技术上的区别。

约定发生了变化(size_t 不在 K&RC 中),但那是很久以前的事了。

strlen() returns a size_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.

弄潮 2024-11-09 03:54:16

当涉及到内存块的长度时,标准库中的所有函数都可以与 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-in sizeof operator yields a size_t result as well.

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.

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