Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a struct to a function in C

Tags:

c

If I have the following: -

struct foo
{
  int a;
  int *b;
} bar;

void baz(struct foo qux)
{

}

Am I right in thinking that passing bar to baz() results in a local copy of bar being pushed onto the stack? If so, what kind of copy is this? in C++, I assume it would call the copy constructor, or the default copy constructor but I don't really know how this would work in C.

Does C have any notion of a default copy constructor and does this have a name? Could something be done to perform a deep copy? (hypothetically). The only way I could think of is to actually do a deep copy and then pass it to the function.

Typically, I would be passing a pointer to a foo but I'm just curious as to how it is working. Furthermore I am under the impression that passing a pointer is faster, saves memory and is the recommended course of action to take when doing this kind of operation. I would guess that it is a shallow copy; can this be changed?

like image 502
chrisw Avatar asked Apr 18 '13 22:04

chrisw


People also ask

Can you pass a struct into a function in C?

Structures can be passed as function arguments like all other data types. We can pass individual members of a structure, an entire structure, or, a pointer to structure to a function. Like all other data types, a structure or a structure member or a pointer to a structure can be returned by a function.

Can we pass a structure to a function?

Passing of structure to the function can be done in two ways: By passing all the elements to the function individually. By passing the entire structure to the function.

Are structs passed by value C?

A struct can be either passed/returned by value or passed/returned by reference (via a pointer) in C. The general consensus seems to be that the former can be applied to small structs without penalty in most cases.

How do you pass an object to a structure function?

Passing structure to function in C++ Then, the structure variable p is to passed to a function using. displayData(p); The return type of displayData() is void and a single argument of type structure Person is passed. Then the members of structure p is displayed from this function.


1 Answers

Am I right in thinking that passing bar to baz() results in a local copy of bar being pushed onto the stack?

Yes.

I don't really know how this would work in C.

Substantially as it would with the default copy constructor in C++; every field of the copy is initialized with the corresponding field of the original. Of course, because of the "as if" rule the whole thing may boil down to a memcpy.

I am under the impression that passing a pointer is faster, saves memory and is the recommended course of action to take when doing this kind of operation.

For larger structs it's often the case, but it's not always like that; if you have a small struct of few small fields the overhead of copying will probably be smaller than that of indirection (also, using pointer parameters can be costly because the aliasing rules of C and C++ can prevent some optimizations).

I would guess that it is a shallow copy; can this be changed?

No, a shallow copy (blindly copy each field) is what happens with the default copy constructor (while with "deep copy" you usually mean also creating a copy of each object referenced in pointer/reference fields).

What you mean is "pass by reference", and it's not the default to allow the maximum flexibility (and for coherency with the passing of primitive types). If you want pass by reference you pass a pointer (or a reference in C++), often const if you are in just for the performance, otherwise you pass the object itself.

like image 151
Matteo Italia Avatar answered Oct 12 '22 09:10

Matteo Italia