“效率”传递 size_t 作为参数

发布于 2024-10-15 17:57:19 字数 195 浏览 5 评论 0原文

由于 size_t 可以是 32 位或 64 位,具体取决于当前系统,因此最好始终将 size_t 作为引用或 const 引用传递给函数,以便它始终为 4 个字节? (如果是 8 字节,则必须复制)我看过的许多开源代码都没有这样做,但是如果他们的编译器支持 64 位整数,则这些 64 位整数始终作为引用传递。他们为什么不对 size_t 这样做呢?我想知道你的意见是什么。

Since size_t can be 32-bit or 64-bit depending on the current system, would it be best to always pass size_t to a function as a reference or const reference so it is always 4 bytes? (if it is 8 bytes you would have to make a copy) Lots of open source code I've looked at do not do this, however if their compiler supports 64-bit integers, those 64-bit integers are always passed as references. Why don't they do this for size_t? I'm wondering what is your opinion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

驱逐舰岛风号 2024-10-22 17:57:19

通常按值传递所有基元类型,因为复制它们所需的操作通常只是单个汇编指令。因此,按值传递 size_t 优于按引用传递 size_t

It's customary to pass all primitive types by value because the operations necessary to copy them are typically just a single assembly instruction. Passing size_ts by value is therefore preferable over passing size_ts by reference.

泅渡 2024-10-22 17:57:19

在大多数实现 size_t 中,对象指针和对象引用的大小完全相同。

这样想: size_t 可以保存任何对象的大小,并且您可以使用 char* 来寻址任何对象中的任何字节,因此这意味着 size_t< /code> 和 char* 必须具有密切相关的大小。因此,您的想法在大多数实现中没有意义。

On most implementations size_t, pointers to objects and references to objects are exactly of the same size.

Think of it this way: size_t can hold size of any object and you can use char* to address any byte in any object, so it implies that size_t and char* must have closely related sizes. Thus your idea makes no sense on most implementations.

∞琼窗梦回ˉ 2024-10-22 17:57:19

我不太符合你的逻辑。如果通过引用传递,则地址将为 32 位或 64 位,具体取决于当前系统。

无论如何,我认为通过引用传递它没有任何好处。

I didn't quite follow your logic. If you pass by reference then the address will be either 32-bit or 64-bit, depending on the current system.

At any rate, I see no advantage to passing it by reference.

廻憶裏菂餘溫 2024-10-22 17:57:19

size_t 保证能够保存可以在内存中分配的任何对象的大小(以字节为单位)。这通常意味着它与指针的大小相同,而指针通常又是 CPU 寄存器的大小。

通过引用传递没有帮助;几乎可以肯定,指针至少与 size_t 一样大(如果不是,则 size_t 可以变小而不会出现问题)。无论如何,大多数 64 位 ABI 都会在 64 位寄存器中传递整数参数,因此堆栈占用空间没有区别。

size_t is guarenteed to be able to hold the size in bytes of any object you can allocate in memory. This usually tends to imply that it is the same size as a pointer, which in turn is typically the size of a CPU register.

Passing by reference doesn't help; a pointer is almost certainly at least as large as a size_t (if not, the size_t could be made smaller without problems). And in any case, most 64-bit ABIs pass integer arguments in 64-bit registers anyway, so there's no difference in stack footprint.

执着的年纪 2024-10-22 17:57:19

通过引用传递的问题在于,它需要编译器将值存储在内存中并将该存储值的地址作为引用传递。在 64 位架构上,调用约定允许在寄存器(6 个寄存器)中传递更多信息,而无需将值存储在内存中,因此您可以通过引用传递小值来抑制优化。

这个问题还有更多内容,您可能想从以下位置开始:

http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

http://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_Calling_Conventions

The problem with passing by reference is that it would require the compiler to store the value in memory and pass the address of that stored value as a reference. On a 64-bit architecture the calling conventions allow to pass much more information in registers (6 registers) without having to store values in memory, so you would inhibit optimizations by passing small values by reference.

There is more to this question, you might like to start at:

http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

http://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_Calling_Conventions

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