1

So i am currently taking a course called data structures and algorithms, and for the first lesson i got a question that i can't quite rap my head around.. The teacher is trying to demonstrate the values of using a call by value and call by reference. He is passing a data struct to a function that prints the addresses of the data struct.

The code is basically this:

struct Exempelstruct{
    int m_intValue1;
    int m_intValue2;
    float m_array[1000];
};

void skrivAdresser1(Exempelstruct theStruct){
    writeAdresses( theStruct );
}
//and
void skrivAdresser2(const Exempelstruct &theStruct){
    writeAdresses( theStruct );
}

The question is why the addresses in the skrivAdresser1() function lower than the addresses that are printed by skrivAdresser2()?

Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
  • 1
    You would also have to show the `main` which shows the order those two functions are called, but you may be interested in understanding how the [stack frame](https://softwareengineering.stackexchange.com/questions/195385/understanding-stack-frame-of-function-call-in-c-c) works, which will explain why one address is lower than the other. – Cory Kramer Jan 21 '21 at 12:45
  • "He is passing a datastruct to a function that prints the adresses of the datastruct" One should be more precise here: an instance(!) of a data struct is passed here (by value and by reference). – Secundi Jan 21 '21 at 12:53
  • Well the functions are called in order of: Exempelstruct s; skrivAdresser1(s); skrivAdresser2(s); sorry forgot to include that. – Rasmus Andersson Kolmodin Jan 21 '21 at 12:55
  • Maybe only for the sake of clarity: Please also provide the writeAdresses() function! – Secundi Jan 21 '21 at 13:09
  • Maybe this is answering your questions: https://stackoverflow.com/questions/4560720/why-does-the-stack-address-grow-towards-decreasing-memory-addresses – Secundi Jan 21 '21 at 13:21
  • This is all platform-dependant stuff, you can't always depend on what your teacher to have said to be true. The addresses could be the same (probably due to compiler optimizations), they could be higher, they could be lower. –  Jan 21 '21 at 23:42

2 Answers2

1

Struct in skrivAddresser1 is constructed later, while the struct in skrivAddresser2 is the same as in the main which is created earlier. Cause main called earlier. The rule for stack variables is later the creation lower the address. So it does not matter which one you call earlier in main, because skrivAddress2 does not create a new object. And the original object is always created before calling skrivAddress functions.

Kao
  • 537
  • 5
  • 12
  • Thank you for the answer! This was the answer i was looking for, i couldn't figure out why the things which were done first got a higher address but that figured it out for me thanks! – Rasmus Andersson Kolmodin Jan 21 '21 at 13:53
  • 2
    "The rule for stack variables is later the creation lower the address": Please keep in mind, that this is platform-dependent! – Secundi Jan 21 '21 at 14:14
0

Basically, you need to study the working of call by value and call by reference.

In call by value, the function copies the actual value of an argument into the formal parameter of the function. So the address of the formal parameter will obviously be different from that of the actual value(It may be greater or less whichever address is empty).

In call by reference, the actual value is passed through & So it has to same as that of the original value.

Dharman
  • 30,962
  • 25
  • 85
  • 135
piyush172
  • 80
  • 9