是否有任何缺点按值用C传递结构,而不是传递指针? [英] Are there any downsides to passing structs by value in C, rather than passing a pointer?

查看:141
本文介绍了是否有任何缺点按值用C传递结构,而不是传递指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何缺点按值用C传递结构,而不是传递指针?

Are there any downsides to passing structs by value in C, rather than passing a pointer?

如果该结构是大的,有明显的拷贝大量的数据的performancd方面,但对于更小的结构,它应该基本上是相同的传递数值的函数。

If the struct is large, there is obviously the performancd aspect of copying lots of data, but for a smaller struct, it should basically be the same as passing several values to a function.

作为返回值使用时,它也许是更加有趣。仅C已经从单一功能的返回值,但你经常需要几个。因此,一个简单的办法就是把它们放在一个struct和返回。

It is maybe even more interesting when used as return values. C only has single return values from functions, but you often need several. So a simple solution is to put them in a struct and return that.

是否有支持或反对任何这样的原因?

Are there any reasons for or against this?

由于它可能不是很明显大家有什么我谈论这里,我举一个简单的例子。

Since it might not be obvious to everyone what I'm talking about here, I'll give a simple example.

如果您在C语言进行编程,你迟早会开始写看起来像这样的功能:

If you're programming in C, you'll sooner or later start writing functions that look like this:

void examine_data(const char *ptr, size_t len)
{
    ...
}

char *p = ...;
size_t l = ...;
examine_data(p, l);

这是没有问题的。唯一的问题是,你必须与你的同事同意在该命令的参数应该是这样,你使用相同的约定中的所有功能。

This isn't a problem. The only issue is that you have to agree with your coworker in which the order the parameters should be so you use the same convention in all functions.

但是,当你想返回相同类型的信息会发生什么?通常,您可以得到这样的:

But what happens when you want to return the same kind of information? You typically get something like this:

char *get_data(size_t *len);
{
    ...
    *len = ...datalen...;
    return ...data...;
}
size_t len;
char *p = get_data(&len);

这工作得很好,但更成问题。返回值是一个返回值,不同的是在此实施事实并非如此。有没有办法从上面,告诉该函数GET_DATA不允许看什么LEN点。并没有什么,使编译器检查的值通过该指针实际返回。所以下个月,当别人修改code,而不理解它是正确的(因为他没有阅读文档?),它就会不说话打破,或者它开始随机崩溃。

This works fine, but is much more problematic. A return value is a return value, except that in this implementation it isn't. There is no way to tell from the above that the function get_data isn't allowed to look at what len points to. And there is nothing that makes the compiler check that a value is actually returned through that pointer. So next month, when someone else modifies the code without understanding it properly (because he didn't read the documentation?) it gets broken without anyone noticing, or it starts crashing randomly.

所以,我提出的解决方案是简单的结构

So, the solution I propose is the simple struct

struct blob { char *ptr; size_t len; }

这些例子可以写成这样:

The examples can be rewritten like this:

void examine_data(const struct blob data)
{
    ... use data.tr and data.len ...
}

struct blob = { .ptr = ..., .len = ... };
examine_data(blob);

struct blob get_data(void);
{
    ...
    return (struct blob){ .ptr = ...data..., .len = ...len... };
}
struct blob data = get_data();

由于某些原因,我认为大多数人会本能地做出examine_data一个指针指向一个结构BLOB,但我不明白为什么。它仍然得到一个指针和一个整数,它只是更清楚,他们一起去。并在GET_DATA情况下,它不可能在我以前描述的方式陷入困境,因为存在用于长度没有输入值,并且必须有一个返回的长度

For some reason, I think that most people would instinctively make examine_data take a pointer to a struct blob, but I don't see why. It still gets a pointer and an integer, it's just much clearer that they go together. And in the get_data case it is impossible to mess up in the way I described before, since there is no input value for the length, and there must be a returned length.

推荐答案

有关小结构(如点,矩形)路过的值是完全可以接受的。但是,除了速度,还有另外一个原因,你应该小心传递/按值返回大的结构:堆栈空间

For small structs (eg point, rect) passing by value is perfectly acceptable. But, apart from speed, there is one other reason why you should be careful passing/returning large structs by value: Stack space.

很多C语言编程的是嵌入式系统中,内存是在premium和堆栈大小可以在KB,甚至字节来衡量......如果你正在传递或返回值结构,副本这些结构将得到安置在堆栈上,可能导致该本网站是后...命名情况

A lot of C programming is for embedded systems, where memory is at a premium, and stack sizes may be measured in KB or even Bytes... If you're passing or returning structs by value, copies of those structs will get placed on the stack, potentially causing the situation that this site is named after...

如果我看到好像有过多的堆栈使用的应用程序,按值传递结构是的东西,我找的第一个。

If I see an application that seems to have excessive stack usage, structs passed by value is one of the things I look for first.

这篇关于是否有任何缺点按值用C传递结构,而不是传递指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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