简体   繁体   English

在 function 中将结构作为参数传递

[英]Passing structure as an argument in function

I have a question about passing arguments in function. Like I discovered that I can pass arguments of structure in two ways:我有一个关于在 function 中传递 arguments 的问题。就像我发现我可以通过两种方式传递结构的 arguments 一样:

First :第一

name_t* foo(name_t* foo)
{
    //do some changes with structure
    return foo->something
}

Second :第二

name_t foo(name_t foo)
{
    //do some changes with structure
    return foo.something
}

What is difference between this approaches?这些方法有什么区别?

How does it put in memory? memory是怎么写的?

Whether there are underwater rocks when I use first approach with second in the same function?当我在同一个function中使用一种方法和第二种方法时是否有水下岩石? For example, when two arguments passed in same function and first would equal to second and vice versa.例如,当两个 arguments 通过相同的 function 时,第一个等于第二个,反之亦然。

Like that:像那样:

name_t* foo(name_t* foo, name_t foo2)
{
    //do some changes with structures
    foo->something = foo2.something;
    return foo;
}

Thanks.谢谢。

The First version passes a pointer to the caller's structure.一个版本传递一个指向调用者结构的指针。 Any changes made through the pointer will affect the caller's structure.通过指针所做的任何更改都会影响调用者的结构。

The Second version passes a copy of the caller's structure, because C is a call-by-value language.第二个版本传递调用者结构的副本,因为 C 是一种按值调用的语言。 Any changes only affect the local copy, not the caller's structure.任何更改只会影响本地副本,不会影响调用者的结构。 However, it's a shallow copy;但是,它是一个浅拷贝; if the structure contains pointer to other structures, they are not copied recursively, and changes to those referenced structures will affect the originals.如果结构包含指向其他结构的指针,则不会递归复制它们,并且对那些引用结构的更改将影响原始结构。

The last example should be fine.最后一个例子应该没问题。 foo2 is a copy of the caller's structure, you can make changes to it without affecting the caller. foo2是调用者结构的副本,您可以对其进行更改而不影响调用者。 Then you can modify the caller's structure when you assign through the foo pointer.然后你可以在通过foo指针赋值的时候修改调用者的结构体。 There's no aliasing problem since one is a copy.没有别名问题,因为一个是副本。

In addition to the vary important points raised in above comments (parameter name hides function name, and inconsistent function signature in first and second clips).除了上面评论中提出的不同要点(参数名称隐藏 function 名称,第一和第二个剪辑中的 function 签名不一致)。

I would like to offer that you should consider the physical size of your structure when deciding how to create and pass instances of structures:我想建议您在决定如何创建和传递结构实例时应该考虑结构的物理大小:

typedef struct name_t {
  char title[256];
  int something;
} name_t;

int my_function(name_t n) {
    // this function cannot change the sender's copy of n
    return strlen(n.title);
}

int my-by_ref(name_t * n) {
    // this function has the ability to change n and reflect changes back
    // do work
    return ...
}

On a system that represents int as 4 bytes, each instance of this structure will occupy 260 bytes.在将int表示为 4 个字节的系统上,此结构的每个实例将占用 260 个字节。

name_t person = {"John Doe", 1}; // 260 bytes on stack
int len_title = my_function(person); // pushes another 260 bytes onto the stack
int result = my_by_ref(&person); // pushes the number of bytes a pointer takes 
...

This is a contrived example, but sending by reference will require the CPU to copy the address of person (typically 8 bytes, but depends on you OS), rather than 260 bytes.这是一个人为的例子,但通过引用发送将需要 CPU 复制人的地址(通常为 8 个字节,但取决于您的操作系统),而不是 260 个字节。 That can add up if this structure needs to be processed frequently.如果需要经常处理此结构,则可以加起来。

There are many more efficient ways to implement such a task, each of which carries certain responsibilities, such as making sure you do not return pointers to data that will go out of scope after the function completes, which are outside the scope of your question.有许多更有效的方法来实现这样的任务,每个方法都有一定的责任,例如确保在 function 完成后,您不会返回指向 scope 中的 go 的数据指针,这些数据在您问题的 scope 之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM