memory used while declaring function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • c.lang.myself@gmail.com

    memory used while declaring function

    Is there any memory allocated in stack when declaring a function

    e.g void fun(int a,int b,int c);

    Is it true that compiler reserves 3 stack spaces for parameters a ,b
    and c....
  • Fred

    #2
    Re: memory used while declaring function

    On Nov 13, 7:18 am, "c.lang.mys...@ gmail.com"
    <c.lang.mys...@ gmail.comwrote:
    Is there any memory allocated in stack when declaring a function
    >
    e.g void fun(int a,int b,int c);
    >
    Is it true that compiler reserves 3 stack spaces for parameters a ,b
    and c....
    No. There isn't even a requirement that such a thing as a stack even
    exists.
    --
    Fred

    Comment

    • Chris Dollin

      #3
      Re: memory used while declaring function

      c.lang.myself@g mail.com wrote:
      Is there any memory allocated in stack when declaring a function
      >
      e.g void fun(int a,int b,int c);
      What stack?

      The code isn't running, so even if there's a stack at runtime,
      there isn't one when the declaration gets processed by the
      compiler.

      (Unless you're asking about the /compiler's/ stack, if any; if
      so, why?)
      Is it true that compiler reserves 3 stack spaces for parameters a ,b
      and c....
      No.

      When the compiler processes that /declaration/, it needn't reserve
      stack space for anything. The declaration just says "there's this
      function `fun` with arguments `(int, int, int)` and return-type void."

      Whether arguments are passed on a stack or not doesn't matter, since
      we're not compiling a call of `fun` or a definition of it either.

      When we compile `void fun(int a, int b, int c) { ...the body ... }`,
      /then/ we might think about stack allocation, if that's how the
      implementation operates. What's /required/ is that the code behaves
      as if there's three auto variables a, b, c, initialised to the values
      passed in a call, and which can be forgotten when the function exits.

      Consider

      int fun( int a, int b, int c ) { return a + b + c; }

      where I've returned `int` to have something happening. When the
      compiler compiles this code it need not use any stack /at all/,
      not even a bit:

      add r0, r1
      add r0, r2
      mov pc, r14

      The arguments are passed in registers r0, r1, and r2, and the
      result returned in r0. The addition can be done in-place into
      the result/first-argument register. Since `fun` doesn't call
      anything else, we don't need to stack a return address or copies
      of the arguments.

      So, while an implementation /might/ use a stack for function
      business, specific cases can be stack-free.

      The moral of the stoty is that one shouldn't confuse an
      implementation technique (stacks) with a specification (variables
      which vanish when their scope is left), nor declarations (let
      me tell you about X) with definitions (compile me this X) and
      executions (call X and pass these three values).

      --
      "It's just the beginning we've seen." - Colosseum, /Tomorrow's Blues/

      Hewlett-Packard Limited registered office: Cain Road, Bracknell,
      registered no: 690597 England Berks RG12 1HN

      Comment

      • Eric Sosman

        #4
        Re: memory used while declaring function

        c.lang.myself@g mail.com wrote:
        Is there any memory allocated in stack when declaring a function
        >
        e.g void fun(int a,int b,int c);
        The C language does not describe how an implementation does
        what is required of it, so it's impossible to answer your question
        in full generality. For what it's worth, I have never come across
        an implementation that used any stack space at all for a function
        declaration.
        Is it true that compiler reserves 3 stack spaces for parameters a ,b
        and c....
        As above: Possibly, but almost certainly not. The language
        standard does not forbid reserving stack space for declarations,
        but I've never seen an implementation that did so.

        --
        Eric.Sosman@sun .com

        Comment

        • Richard Tobin

          #5
          Re: memory used while declaring function

          In article <a6384f5b-b419-4e87-adfb-34c9daf55360@w2 4g2000prd.googl egroups.com>,
          c.lang.myself@g mail.com <c.lang.myself@ gmail.comwrote:
          >Is there any memory allocated in stack when declaring a function
          >
          >e.g void fun(int a,int b,int c);
          >
          >Is it true that compiler reserves 3 stack spaces for parameters a ,b
          >and c....
          It depends what you mean by "allocated" and "reserved". The compiler
          might well choose three positions on the stack that will be used for
          the arguments at run time. But the actual allocation won't happen
          until run time, and it will happen at each invocation of the function,
          rather than once for the declaration.

          -- Richard


          --
          Please remember to mention me / in tapes you leave behind.

          Comment

          • S M Ryan

            #6
            Re: memory used while declaring function

            In article <a6384f5b-b419-4e87-adfb-34c9daf55360@w2 4g2000prd.googl egroups.com>,
            "c.lang.myself@ gmail.com" <c.lang.myself@ gmail.comwrote:
            Is there any memory allocated in stack when declaring a function
            >
            e.g void fun(int a,int b,int c);
            >
            Is it true that compiler reserves 3 stack spaces for parameters a ,b
            and c....
            Perhaps. But the memory associated with functions is not something you control,
            allocate, or deallocate. Unless you're working on a machine with tight memory,
            don't worry about it.

            --
            I'm not even supposed to be here today.

            I ASSURE YOU WE'RE OPEN!

            Comment

            • c.lang.myself@gmail.com

              #7
              Re: memory used while declaring function

              So what actually all things compiler do when it encounters declaration
              of function first time....

              Comment

              • Ian Collins

                #8
                Re: memory used while declaring function

                c.lang.myself@g mail.com wrote:

                [please keep enough of the context for your reply to make sense]
                So what actually all things compiler do when it encounters declaration
                of function first time....
                If the declaration isn't a definition, it simply parses and remembers it.

                --
                Ian Collins

                Comment

                • Chris Dollin

                  #9
                  Re: memory used while declaring function

                  c.lang.myself@g mail.com wrote:
                  So what actually all things compiler do when it encounters declaration
                  of function first time....
                  The compiler remembers the declaration, so that it can check uses of the
                  function against the declaration and compile the appropriate code for the
                  call.

                  --
                  "We are on the brink of a new era, if only --" /The Beiderbeck Affair/

                  Hewlett-Packard Limited registered office: Cain Road, Bracknell,
                  registered no: 690597 England Berks RG12 1HN

                  Comment

                  • Nick Keighley

                    #10
                    Re: memory used while declaring function

                    On Nov 13, 4:19 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                    In article <a6384f5b-b419-4e87-adfb-34c9daf55...@w2 4g2000prd.googl egroups..com>,
                    >
                    c.lang.mys...@g mail.com <c.lang.mys...@ gmail.comwrote:
                    Is there any memory allocated in stack when declaring a function
                    >
                    e.g void fun(int a,int b,int c);
                    >
                    Is it true that compiler reserves 3 stack spaces for parameters a ,b
                    and c....
                    >
                    It depends what you mean by "allocated" and "reserved".  The compiler
                    might well choose three positions on the stack that will be used for
                    the arguments at run time.  But the actual allocation won't happen
                    until run time, and it will happen at each invocation of the function,
                    rather than once for the declaration.
                    but those stack positions must be relative rather than absolute
                    otherwise I can't see how recursive functions are to be implemented.

                    --
                    Nick Keighley

                    Comment

                    • Richard Tobin

                      #11
                      Re: memory used while declaring function

                      In article <c6675922-ee9d-49c8-be54-33f1a96f0b82@c2 2g2000prc.googl egroups.com>,
                      Nick Keighley <nick_keighley_ nospam@hotmail. comwrote:
                      >It depends what you mean by "allocated" and "reserved".  The compiler
                      >might well choose three positions on the stack that will be used for
                      >the arguments at run time.  But the actual allocation won't happen
                      >until run time, and it will happen at each invocation of the function,
                      >rather than once for the declaration.
                      >but those stack positions must be relative rather than absolute
                      Yes of course. That's why they're on the stack!

                      -- Richard
                      --
                      Please remember to mention me / in tapes you leave behind.

                      Comment

                      • CBFalconer

                        #12
                        Re: memory used while declaring function

                        Nick Keighley wrote:
                        rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                        >c.lang.mys...@ gmail.com <c.lang.mys...@ gmail.comwrote:
                        >>
                        >>Is there any memory allocated in stack when declaring a function
                        >>e.g void fun(int a,int b,int c);
                        >>>
                        >>Is it true that compiler reserves 3 stack spaces for parameters
                        >>a ,b and c....
                        >>
                        >It depends what you mean by "allocated" and "reserved". The
                        >compiler might well choose three positions on the stack that will
                        >be used for the arguments at run time. But the actual allocation
                        >won't happen until run time, and it will happen at each invocation
                        >of the function, rather than once for the declaration.
                        >
                        but those stack positions must be relative rather than absolute
                        otherwise I can't see how recursive functions are to be implemented.
                        This has been stated before, but this thread seems to be ignoring
                        it. THERE IS NO STACK. At least none is specified. If one
                        exists, it was put there for the convenience of that particular
                        implementor. The things that do exist are static, allocated, and
                        automatic storage.

                        --
                        [mail]: Chuck F (cbfalconer at maineline dot net)
                        [page]: <http://cbfalconer.home .att.net>
                        Try the download section.

                        Comment

                        • Richard Tobin

                          #13
                          Re: memory used while declaring function

                          In article <491DF224.83D1F C94@yahoo.com>,
                          CBFalconer <cbfalconer@mai neline.netwrote :
                          >This has been stated before, but this thread seems to be ignoring
                          >it.
                          That's because we're not all mindless cretins.

                          -- Richard
                          --
                          Please remember to mention me / in tapes you leave behind.

                          Comment

                          • Antoninus Twink

                            #14
                            Re: memory used while declaring function

                            On 14 Nov 2008 at 22:12, Richard Tobin wrote:
                            CBFalconer <cbfalconer@mai neline.netwrote :
                            >>This has been stated before, but this thread seems to be ignoring
                            >>it.
                            >
                            That's because we're not all mindless cretins.
                            Exactly.

                            It's been stated before. It was bullshit then, and it's bullshit now.

                            Comment

                            • Chris Dollin

                              #15
                              Re: memory used while declaring function

                              CBFalconer wrote:
                              This has been stated before, but this thread seems to be ignoring
                              it. THERE IS NO STACK. At least none is specified.
                              I said as much in my response. Did you read it?

                              --
                              "It took a very long time, much longer than the most /Sector General/
                              generous estimates." - James White

                              Hewlett-Packard Limited Cain Road, Bracknell, registered no:
                              registered office: Berks RG12 1HN 690597 England

                              Comment

                              Working...