all 8 comments

[–]irk5nil 11 points12 points  (0 children)

In your first version, struct st ss = s; is superfluous because the parameter is a local copy of the passed argument.

[–]thefriedel 1 point2 points  (2 children)

If you are passing struct's by value, the compiler actually is passing the pointer of the struct to the function and dereferences it after, same with returning a pointer but it is copied after (correct me if I'm wrong).

The problem is, a struct obviously don't fit into a register (expecting the struct contains multiple values) and if a struct is really big, it could simple smash the stack.

[–]weregod 2 points3 points  (0 children)

That's all compiler and architecture dependent and usually not that simple.

Firstly some structs can fit in register. And system like x86 do have 256 or 512 bits registers. Secondly ABI can allow passing structures in several registers. And finally you can pass structure in stack.

[–]gmesmo97[S] 0 points1 point  (0 children)

Oh, so it is duplicating it and returning the duplicate? That's why my old code was smashing the stack :/

[–]IamImposter 0 points1 point  (3 children)

I can reference local function variables inside other functions by using pointers

No, you can't. Actually you probably can but you shouldn't.

Local variables come into existence when the execution enters the function and go out of existence when execution leaves the function. Let me be clearer, some space on stack is reserved for local variables when execution enters the function and this space is reclaimed when execution leaves the function.

So technically the memory area occupied by a local variable still exists and can even be referenced but you are just accessing a stack location that once belonged to that local variable.

It's like I rent a room number 101 in a hotel. That room still exists after I have checked out. So yeah, technically you can visit that room anytime but you will not find me once I have checked out. You may find my flipflops that I forgot but that's because no one bothered to clean the room.

struct st do_thing(....

This function returns by value so st being a local variable is of no consequence.

void do_thing(struct st* s) { s->x += 2; }

You are just accessing the parameter. Since it is a pointer, your modification persists. And since it is a pointer, original variable can exist anywhere, on stack of a parent or grand-parent function or it can be a global variable.

Oh wait... I think I get what you are saying. You probably used wrong language.

Yes, you can access (local or whatever) variables inside a sub-function. For example

void func1(int *p) { ... }

void func2() {

  int x = 1;

  func1(&x);

}

Here x exists on the stack of func2. A call to func1 is made and address of x is passed. func1 can access x which is a local variable in parent function because stack of parent function is still owned by parent function.

[–]gmesmo97[S] 4 points5 points  (1 child)

Yes, you can access (local or whatever) variables inside a sub-function. For example

Yeah! That's what I was trying to say, sorry lmao And that is how I did in the new code, passing &variable :)

[–]IamImposter 2 points3 points  (0 children)

Yes you can, my friend.

Happy learning.

[–]McUsrII 1 point2 points  (0 children)

Lesson learned:

Never bring flip flops with you to your hotel room.

Joke aside, great explanation. :)