简体   繁体   中英

memory referencing in C using structure

I am learning about structures in C. when we pass a structure as an argument to another function, what exactly are we doing? are we copying all the member values in another function? When we pass an array as argument, we are passing the first address of the array - array[0]. things don't seem so simple with structure. My guess is that we are neither copying any value nor passing any address. we are telling compiler to be ready to use, as and when required, the addresses of the members of the structure being passed. I am not sure though.. any help is appreciated.

Structres can be passed both by value and reference. If there is a big structure its advisable to pass by reference else huge amount of stack space may be consumed as a copy of the passed structure will be made in the called function Read this to know more about structure passing

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

It depends on how the struct is passed:

  • if passed by value a copy is made and changes made inside the function to the struct are not visible to the caller
  • if passed by reference a copy is not made and changes made inside the function to the struct are visible to the caller

For example:

#include <stdio.h>

struct X {
  int value;
};

void pass_by_value(struct X a_x)
{
    a_x.value = 10;
    printf("by_value(address=%p, value=%d)\n", &a_x, a_x.value);
}

void pass_by_ref(struct X* a_x)
{
    a_x->value = 20;
    printf("by_ref(address=%p, value=%d)\n", a_x, a_x->value);
}

int main()
{
    struct X my_x = { 1 };

    printf("my_x(address=%p, value=%d)\n\n", &my_x, my_x.value);

    pass_by_value(my_x);
    printf("my_x(address=%p, value=%d)\n\n", &my_x, my_x.value);

    pass_by_ref(&my_x);
    printf("my_x(address=%p, value=%d)\n\n", &my_x, my_x.value);

    return 0;
}

Output:

my_x(address=0013FF74, value=1)

by_value(address=0013FF78, value=10)
my_x(address=0013FF74, value=1)

by_ref(address=0013FF74, value=20)
my_x(address=0013FF74, value=20)
#include <stdio.h>

struct foo {
  int a;
}

void by_value(struct foo x) {
  x.a = 10;
}

void by_reference(struct foo* x) {
  x->a = 10;
}

int main(int argc, char* argv[]) {
  struct foo v;
  v.a = 5;

  by_value(v);
  printf("%d\n", v.a);

  by_reference(&v);
  printf("%d\n", v.a);

  return 0;
}

This should answer your question if you compile it and run it.

Before we call a function, the compiler pushes the parameters into the stack first, and then it invokes the function. So, the function can get the parameter from the stack.

When passed by address , the address is passed to the function. And the compiler will also process the parameter memory, which is called "Memory alignment" or something like that, to improve performence. So, in your function, you can easily covert that block of memory to a structure.

When passed by value, the compiler copes the parameter and pass the coped parameter address to your function.

By the way, in one process, the memory address is the same. Any function can use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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