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


Groups > comp.lang.c > #13880

Re: Variables allocated on stack

From "BartC" <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: Variables allocated on stack
Date 2011-11-25 11:05 +0000
Organization A noiseless patient Spider
Message-ID <jansun$pcb$1@dont-email.me> (permalink)
References <53159da6-37f6-4742-9ad0-2e3477ca8b0b@v8g2000yqk.googlegroups.com> <aae5e2ca-0716-489f-9ad5-4ab494a16841@o1g2000vbe.googlegroups.com>

Show all headers | View raw


"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message 
news:aae5e2ca-0716-489f-9ad5-4ab494a16841@o1g2000vbe.googlegroups.com...
> On Nov 24, 11:28 pm, Bogdan <crist...@gmail.com> wrote:

>> char* some_function(char c)
>> {
>>      char *out = malloc(10);
>>      return out;
>>
>> }
>>
>> On the local stack are there one char and 2 pointers to chars ?
>>
> A modern compiler would probably just optimise that function to a call
> to malloc().
>
> However the abstract C compiler needs a stack for the function calls,
> and a stack for variables. When you call a function, the address of
> the instruction to return to in caller is pushed onto the call stack.
> Then the parameters are pushed onto the variable stack (in this case,
> c). Then the local variables are pushed onto the variable stack (in
> this case, out). The function then does its calculations, the variable
> stack is popped, the return address is popped off the call stack and
> the function returns. So what happens to the return value? Almost
> always, it is returned in a register.

If you're going to talk about abstract call and variable stacks, then why 
not put the return value onto the abstract variable stack too?

> However pushing and popping stacks is not very efficient. So compilers
> use registers as much as possible. On most processors, the first four
> parameters will be passed in registers.

That's fine until the registers are needed for something else or you need to 
make another function call. Then they need to be saved, perhaps by pushing 
onto the stack, then popping again. Except this might be done a million 
times within the function, instead of just once.

> Also, the call stack can be
> the same as the variable stack.

Some compilers have an option to display the code they generate. Then it is 
possible to see some actual code.

-- 
Bartc 

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


Thread

Variables allocated on stack Bogdan <cristeab@gmail.com> - 2011-11-24 13:28 -0800
  Re: Variables allocated on stack Ian Collins <ian-news@hotmail.com> - 2011-11-25 10:40 +1300
  Re: Variables allocated on stack jacob navia <jacob@spamsink.net> - 2011-11-24 23:30 +0100
    Re: Variables allocated on stack Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-12-02 22:33 +0000
      Re: Variables allocated on stack jacob navia <jacob@spamsink.net> - 2011-12-03 00:54 +0100
      Re: Variables allocated on stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-12-02 23:33 -0800
        Re: Variables allocated on stack Keith Thompson <kst-u@mib.org> - 2011-12-03 02:25 -0800
  Re: Variables allocated on stack jt@toerring.de (Jens Thoms Toerring) - 2011-11-24 23:37 +0000
  Re: Variables allocated on stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-25 01:53 -0800
    Re: Variables allocated on stack "BartC" <bc@freeuk.com> - 2011-11-25 11:05 +0000
      Re: Variables allocated on stack jacob navia <jacob@spamsink.net> - 2011-11-25 12:11 +0100
        Re: Variables allocated on stack "BartC" <bc@freeuk.com> - 2011-11-25 11:40 +0000
          Re: Variables allocated on stack jacob navia <jacob@spamsink.net> - 2011-11-25 12:52 +0100
            Re: Variables allocated on stack Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-27 02:31 -0800
              Re: Variables allocated on stack jacob navia <jacob@spamsink.net> - 2011-11-27 13:28 +0100
              Re: Variables allocated on stack André Gillibert <MetaEntropy.removeThis@gmail.com> - 2011-11-27 13:44 +0100
                Re: Variables allocated on stack jacob navia <jacob@spamsink.net> - 2011-11-27 15:44 +0100
                Re: Variables allocated on stack André Gillibert <MetaEntropy.removeThis@gmail.com> - 2011-11-27 17:34 +0100
                Re: Variables allocated on stack jacob navia <jacob@spamsink.net> - 2011-11-27 18:28 +0100
                Re: Variables allocated on stack Ian Collins <ian-news@hotmail.com> - 2011-11-28 07:48 +1300
                Re: Variables allocated on stack gwowen <gwowen@gmail.com> - 2011-11-29 01:11 -0800
                Re: Variables allocated on stack jacob navia <jacob@spamsink.net> - 2011-11-29 17:02 +0100
          Re: Variables allocated on stack Seebs <usenet-nospam@seebs.net> - 2011-11-26 07:21 +0000
            Re: Variables allocated on stack "BartC" <bc@freeuk.com> - 2011-11-26 11:07 +0000
              Re: Variables allocated on stack Markus Wichmann <nullplan@gmx.net> - 2011-11-26 21:16 +0100
                Re: Variables allocated on stack "BartC" <bc@freeuk.com> - 2011-11-26 23:00 +0000
                Re: Variables allocated on stack Ian Collins <ian-news@hotmail.com> - 2011-11-27 12:12 +1300
                Re: Variables allocated on stack "BartC" <bc@freeuk.com> - 2011-11-26 23:35 +0000
                Re: Variables allocated on stack Ian Collins <ian-news@hotmail.com> - 2011-11-27 12:48 +1300
                Re: Variables allocated on stack Markus Wichmann <nullplan@gmx.net> - 2011-11-27 10:56 +0100
                Re: Variables allocated on stack Keith Thompson <kst-u@mib.org> - 2011-11-28 16:17 -0800
                Re: Variables allocated on stack Markus Wichmann <nullplan@gmx.net> - 2011-11-30 17:04 +0100
                Re: Variables allocated on stack Keith Thompson <kst-u@mib.org> - 2011-11-30 09:36 -0800
                Re: Variables allocated on stack Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 05:11 -0800
                Re: Variables allocated on stack Keith Thompson <kst-u@mib.org> - 2012-01-25 12:23 -0800
                Re: Variables allocated on stack Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 19:11 -0800
                Re: Variables allocated on stack Ian Collins <ian-news@hotmail.com> - 2011-11-27 13:06 +1300
                Re: Variables allocated on stack André Gillibert <MetaEntropy.removeThis@gmail.com> - 2011-11-27 11:20 +0100
                Re: Variables allocated on stack David Thompson <dave.thompson2@verizon.net> - 2012-01-19 02:43 -0500
              Re: Variables allocated on stack Seebs <usenet-nospam@seebs.net> - 2011-11-28 08:32 +0000
    Re: Variables allocated on stack Bogdan <cristeab@gmail.com> - 2011-11-26 01:18 -0800
      Re: Variables allocated on stack Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-26 08:36 -0500
  Re: Variables allocated on stack Seebs <usenet-nospam@seebs.net> - 2011-11-26 07:21 +0000
  Re: Variables allocated on stack Noob <root@127.0.0.1> - 2011-11-29 11:51 +0100

csiph-web