Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #167067

Re: variable addresses on the stack

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: variable addresses on the stack
Date 2022-08-19 10:59 -0700
Organization None to speak of
Message-ID <871qtc868m.fsf@nosuchdomain.example.com> (permalink)
References <54f99ed2-fe4d-4cab-944c-cb61799d64ecn@googlegroups.com>

Show all headers | View raw


Thiago Adams <thiago.adams@gmail.com> writes:
> I am curious with the following..
>
> void f(int i) {
>    if (i > 1)  {
>        int j ='a';       
>    }
>    if (i < 1)  {
>        int k ='b';       
>    }
> }
> When I see the assembly ouput I can see the compiler
> use the address  rbp-4 for j and rbp-8 for k
>
> Why not the same address considering that j and k don't exist 
> at the same time?
> Any reason?

No particular reason.  They don't exist at the same time, and
a compiler certainly *could* allocate them at the same address.
It's valid either way as long as the code behaves correctly.

I played with it myself.  With gcc -O0, I got the same result
you did.  With -O1 and higher, it eliminated j and k.  When I
added putchar calls to prevent that, they were both stored in the
same register.

When I changed j and k to arrays, gcc -O0 allocated them at the
same address.

And yes, block local variables have been supported for a long time.
A 1975 C reference manual allowed declarations only at the top of
a function body, but K&R1 (1978) allows declarations within any
compound statement.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

variable addresses on the stack Thiago Adams <thiago.adams@gmail.com> - 2022-08-19 10:22 -0700
  Re: variable addresses on the stack Thiago Adams <thiago.adams@gmail.com> - 2022-08-19 10:27 -0700
  Re: variable addresses on the stack Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-19 10:59 -0700
    Re: variable addresses on the stack Thiago Adams <thiago.adams@gmail.com> - 2022-09-21 05:09 -0700
      Re: variable addresses on the stack Thiago Adams <thiago.adams@gmail.com> - 2022-09-21 05:18 -0700
        Re: variable addresses on the stack Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-21 15:30 -0700
      Re: variable addresses on the stack David Brown <david.brown@hesbynett.no> - 2022-09-21 15:34 +0200
      Re: variable addresses on the stack Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-21 15:23 -0700
  Re: variable addresses on the stack Kaz Kylheku <480-992-1380@kylheku.com> - 2022-08-19 20:58 +0000
  Re: variable addresses on the stack Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2022-08-19 15:47 -0600
  Re: variable addresses on the stack Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-19 18:47 -0700

csiph-web