在 C 中按值传递结构而不是传递指针有什么缺点吗? [英] Are there any downsides to passing structs by value in C, rather than passing a pointer?

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

问题描述

在 C 中按值传递结构而不是传递指针有什么缺点吗?

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

如果结构体很大,显然有复制大量数据的性能方面,但对于较小的结构体,它应该与向函数传递多个值基本相同.

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 只有一个函数的返回值,但你经常需要几个.因此,一个简单的解决方案是将它们放入一个结构体中并返回.

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 指向的内容.并且没有什么可以让编译器检查值实际上是通过该指针返回的.所以下个月,当其他人在没有正确理解它的情况下修改代码(因为他没有阅读文档?)它会在没有人注意到的情况下被破坏,或者它开始随机崩溃.

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; }

示例可以这样重写:

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 带一个指向struct 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 编程是针对嵌入式系统的,其中内存非常宝贵,堆栈大小可能以 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天全站免登陆