0

I have defined two functions foo1, foo2, and foo3 each function receives one argument (xval),(yval) and I also declared only one variable in each function (x),(y). why when printing x address, and y address. I find that the address is constant. And the same reason is given with xval, yval.

Here is the code:

#include <stdio.h>

void foo1(int xval) {
  int x;
  x = xval;
  printf("X Address == 0x%x, Value of x == %d\n", &xval, x);
}

void foo2(int dummy) {
  int y = dummy;
  printf("Y Address == 0x%x, Value of y == %d\n", &dummy, y);
}

int main() {
  foo1(7);
  foo2(11);

  return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ahmd Sami
  • 1
  • 3
  • Please show your output, and explain what is confusing you in it. – Eugene Sh. Nov 03 '21 at 16:13
  • I've been told by someone with a diamond next to their username that there is no minimum effort required to ask questions, so I can't actually request that you make the effort to format your code next time. – sweenish Nov 03 '21 at 16:13
  • 3
    Becasue that's the location *on the stack* of the argument passed. Consecutive calls, same stack usage. – Weather Vane Nov 03 '21 at 16:13
  • Why shouldn't it be the same address? The `int` inside the function can be stack-allocated, goes immediately out of scope and a new `int` is allocated, so the stack pointer moves up, down, up. – RL-S Nov 03 '21 at 16:14
  • 3
    ["_two functions foo1, foo2, and foo3_"](https://youtu.be/Cj8n4MfhjUc?t=55) – the busybee Nov 03 '21 at 16:14
  • 3
    @thebusybee "*There are 10 kinds of people, these who understand binary and these who don't*" – Eugene Sh. Nov 03 '21 at 16:15
  • Recommendation: Write question titles that describe the problem. This makes the question and its answerers easier to find by future askers with the same or similar problem. The current title conveys no information. – user4581301 Nov 03 '21 at 16:32
  • 1
    take note of the [correct way to print an address](https://stackoverflow.com/questions/30354097/how-to-printf-a-memory-address-in-c) – yano Nov 03 '21 at 16:35
  • @user4581301 ok i will edit it. – Ahmd Sami Nov 03 '21 at 16:36
  • 1
    @yano ok thank you. – Ahmd Sami Nov 03 '21 at 16:39
  • @thebusybee Five is right out... ;-) – Andrew Henle Nov 03 '21 at 18:09
  • Did you know sometimes people use the same parking spot you do when your car is not there? – Eric Postpischil Nov 03 '21 at 21:07
  • @EricPostpischil. haha (LOL) yeah. it is right that is clear explanation – Ahmd Sami Nov 03 '21 at 22:03
  • For all not illuminated: [Thrrree shall be the number](https://www.youtube.com/watch?v=-IOMNUayJjI), I tip my hat to Andrew. -- @AhmdSami Please mark the answer that fits best. – the busybee Nov 04 '21 at 08:22

2 Answers2

1

In foo1 and foo2 you are printing out the addresses of local variables (xval and dummy respectively) that are placed on the stack.

If I remember correctly, the C++ specification does not require these local variables to have different addresses. This should not concern you either.

The address of those variables has meaning only within the function in which the variable is defined, so comparing the addresses accross two different functions is meaningless.

What technically happens under the hood is, the compiler has placed 7 on the stack in your main() function before calling foo1(). Then foo1() prints out the address of that variable in the stack.

When foo1() is finished, the variable is popped from the stack and then 11 is placed on the stack before calling foo2(). This accidentally has used the same memory address but you should not count on this. For example, if you call foo2() from within foo1() this would affect the stack and the address would be different. Try the following:

#include <stdio.h>
extern void foo2(int dummy);
void foo1(int xval)
{
    int x;
    x = xval;
    printf("X Address == 0x%x, Value of x == %d\n", &xval, x);
    
    foo2(11);
}
void foo2(int dummy)
{
    int y = dummy;
    printf("Y Address == 0x%x, Value of y == %d\n", &dummy, y);
}

int main()
{

    foo1(7);

    return 0;
}

In summary, do not try to analyze what the concrete address value is. Simply use the address without looking into it. This is platform and compiler dependent.

D-FENS
  • 1,438
  • 8
  • 21
0

xval and dummy are temporary variables. ("automatic variables" is the C term for them.) They're only guaranteed to exist while the function is executing. So there's no reason they couldn't be at the same address.

On your standard home computer, they would be found on the stack. Given they are both the first argument to the function, and given that the functions are called in sequence, I would be surprised if they didn't have the same address.

In fact, if you didn't take the address of the parameter, it's entirely possible for the argument to be passed to the function using a register so that no memory would be used at all.

ikegami
  • 367,544
  • 15
  • 269
  • 518