Skip to main content
added 243 characters in body
Source Link
ed_me
  • 3.5k
  • 2
  • 26
  • 36

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?

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.

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?

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?

Source Link
ed_me
  • 3.5k
  • 2
  • 26
  • 36

Passing a struct to a function in 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.

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?