C:何时按值返回或传递引用 [英] C: When to return by value or pass reference

查看:89
本文介绍了C:何时按值返回或传递引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管已经多次讨论了该主题,但到目前为止,我还没有找到令人满意的答案.什么时候通过 return 从函数返回数据或传递引用来更改地址数据?经典的答案是当变量变大时将变量作为对函数的引用(以避免堆栈复制).对于诸如结构或数组之类的任何事物,这看起来都是正确的.但是,从函数返回指针并不罕见.实际上有些功能从C库到确切的东西.例如:

Although the subject is discussed many times, I haven't found any satisfying answer so far. When to return data from a function by return or to pass a reference to change the data on address? The classic answer is to pass a variable as reference to a function when it becomes large (to avoid stack copying). This looks true for anything like a structure or array. However returning a pointer from a function is not uncommon. In fact some functions from the C library to the exact thing. For example:

char *strcat(char *dst, const char *src);

即使发生错误,也始终返回指向目标的指针.在这种情况下,我们可以只使用传递的变量,而保留返回值(就像大多数情况一样).

Always returns a pointer to destination even in case of an error. In this case we can just use the passed variable and leave the return for what it is (as most do).

查看结构时,我看到了同样的事情.当仅在变量初始化中使用函数 时,我经常返回指针.

When looking at structures I see the same thing happening. I often return pointers when functions only need to be used in variable initialization.

char *p = func(int i, const char *s);

然后有一个论点,认为堆栈应对变量很昂贵,因此可以使用指针代替.但是,如此处所述,一些编译器可以自行决定(假设这也适用于C语言).是否有一条通用规则,或者至少何时使用一种或另一种不成文的约定?我重视性能胜过设计.

Then there is the argument that stack coping variables is expensive, and so to use pointers instead. But as mentioned here some compilers are able to decide this themselves (assuming this goes for C as well). Is there a general rule, or at least some unwritten convention when to use one or the other? I value performance above design.

推荐答案

首先,确定哪种方法在逻辑级别上最有意义,而不管您认为性能影响可能是什么.如果按值返回struct最清楚地传达了代码的意图,则可以这样做.

Start by deciding which approach makes the most sense at the logical level, irrespective of what you think the performance implications might be. If returning a struct by value most clearly conveys the intent of the code, then do that.

这已经不是1980年代了.从那时起,编译器变得更加聪明,并且在优化代码方面做得非常出色,尤其是以清晰,直接的方式编写的代码.同样,参数传递和值返回约定也变得相当复杂.简单的基于堆栈的模型并不能真正反映现代硬件的实际情况.

This isn't the 1980s anymore. Compilers have gotten a lot smarter since then and do a really good job of optimizing code, especially code that's written in a clear, straightforward manner. Similarly, parameter passing and value return conventions have become fairly sophisticated as well. The simplistic stack-based model doesn't really reflect the reality of modern hardware.

如果生成的应用程序不符合您的性能标准,则通过探查器运行它以查找瓶颈.如果事实证明,按值返回struct会引起问题,请然后进行尝试,以引用方式传递给函数.

If the resulting application doesn't meet your performance criteria, then run it through a profiler to find the bottlenecks. If it turns out that returning that struct by value is causing a problem, then you can experiment with passing by reference to the function.

除非您在高度受限的嵌入式环境中工作,否则您实际上不必计数每个字节和CPU周期.您不想不必要地浪费资源,但是出于同样的原因,您也不想沉迷于低水平的工作方式,除非a)您对性能的要求非常严格,并且b)您非常熟悉 em>熟悉您特定平台的详细信息(意味着您不仅了解内部和外部平台的函数调用约定,而且还知道编译器如何使用这些约定).否则,您只是在猜测.让编译器为您完成艰苦的工作.那就是它的用途.

Unless you're working in a highly constrained, embedded environment, you really don't have to count every byte and CPU cycle. You don't want to be needlessly wasteful, but by that same token you don't want to obsess over how things work at the low level unless a) you have really strict performance requirements and b) you are intimately familiar with the details of your particular platform (meaning that you not only know your platform's function calling conventions inside and out, you know how your compiler uses those conventions as well). Otherwise, you're just guessing. Let the compiler do the hard work for you. That's what it's there for.

这篇关于C:何时按值返回或传递引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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