Why are variables stored on the stack?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Willem

    #76
    Re: Why are variables stored on the stack?

    Keith wrote:
    ) It quacks like a stack, so yes, I'd call it (a rather unconvention
    ) implementation of) a stack.

    But not _the_ stack. See the subject header.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT

    Comment

    • Eric Sosman

      #77
      Re: Why are variables stored on the stack?

      jacob navia wrote:
      Keith Thompson wrote:
      >[...]
      >You insisted that this correct response was wrong. And that is what
      >led to this entire wasteful argument.
      >>
      >
      Yes, discussing with you and falconer is a waste of time
      You just say
      >
      "C has no stack"
      Can you provide a reference to the posting where
      Keith made this statement?

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      • Gordon Burditt

        #78
        Re: Why are bugs stored on the stack?

        The C standard does not require a C implementation to have bugs.
        The C standard doesn't even define the term "bug".

        Does this mean that C has no bugs? No.

        Does this mean that a given C implementation has no bugs? No, although
        not requiring an implementation to have bugs doesn't mean it's required
        to have no bugs.

        Does this mean that someone is wrong for saying "Most C implementations
        have bugs"? No.


        Comment

        • CBFalconer

          #79
          Re: Why are variables stored on the stack?

          Richard Heathfield wrote:
          Rod Pemberton said:
          >
          <snip>
          >>
          >How so? How is the OP's usage of "insist" any different from
          >Ritchie's usage of "suggest strongly"?
          >
          This looks familiar. I think you posted exactly the same question
          elsethread, yes? I answered it there.
          Again, time for a thread plonk.

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



          --
          Posted via a free Usenet account from http://www.teranews.com

          Comment

          • Richard Bos

            #80
            Re: Why are variables stored on the stack?

            jacob navia <jacob@nospam.c omwrote:
            Willem wrote:
            CJ wrote:
            ) But my question is: Why does C insist on storing local variables on the
            ) stack in the first place?

            It doesn't. Your question is moot.
            >
            This is wrong. Most C implementations use the hardware stack
            Well, here's the thing: you're both right. Willem is right that C
            doesn't require the use of the (single, hardware) stack. You are right
            that C implementors tend to use that stack anyway. So the OP really
            shouldn't blame C itself for all those buffer overflow attacks, but the
            authors of the C implementations which do use that stack.

            Richard

            Comment

            • Dik T. Winter

              #81
              Re: Why are variables stored on the stack?

              In article <slrnftnho3.2em t.willem@snail. stack.nlWillem <willem@stack.n lwrites:
              Richard wrote:
              ) santosh <santosh.k83@gm ail.comwrites:
              )Exactly which concept in C is explained easier by introducing a stack?
              )
              ) Just about everything. I am amazed you have to ask this. You have been
              ) in CLC too long.
              >
              You're dodging the question.
              >
              PS: It may be true that a lot of 'how does it do it' questions are most
              easily answered by 'it uses a stack', but for C programming concepts as
              such, I see no need.
              Moreover, I think that when somebody needs C to be explained and you wish
              to use the stack in explaining, you have first to explain the stack.
              --
              dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
              home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

              Comment

              • santosh

                #82
                Re: Why are variables stored on the stack?

                Richard Bos wrote:
                So the OP really
                shouldn't blame C itself for all those buffer overflow attacks, but
                the authors of the C implementations which do use that stack.
                Authors of C programs which overflow.

                Comment

                • Dik T. Winter

                  #83
                  Re: Why are variables stored on the stack?

                  In article <87d4pvf3n6.fsf @kvetch.smov.or gKeith Thompson <kst-u@mib.orgwrites :
                  ....
                  Fair enough. And by the "as if rule", if the implementation can prove
                  that no function is ever called recursively, it can use static storage
                  for everything.
                  Right. And this is done in Fortran 77. On the other hand, the system
                  may provide a different stack for each function.
                  --
                  dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                  home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                  Comment

                  • Kenneth Brody

                    #84
                    Re: Why are variables stored on the stack?

                    jacob navia wrote:
                    >
                    Ian Collins wrote:
                    jacob navia wrote:
                    Willem wrote:
                    >CJ wrote:
                    >) But my question is: Why does C insist on storing local variables on the
                    >) stack in the first place?
                    >>
                    >It doesn't. Your question is moot.
                    >>
                    >>
                    >SaSW, Willem
                    This is wrong. Most C implementations use the hardware stack
                    >
                    Please stop confusing practical implementation with requirements.
                    >
                    Please stop confusing people by using word games.
                    I have yet to see a SINGLE example of an implementation that
                    doesn't use a stack for the local variables. Yes, a single
                    one.
                    >
                    Until now, there wasn't any that the regulars could put forward.
                    While I haven't looked into this in great detail, here is a likely
                    candidate:



                    As I recall, the 6502 has only 256 bytes for its stack.
                    (Obviously in machines running now, and having a certain
                    minimum size. Coffee machines with less than 1K of
                    RAM and similars do not count)
                    Just curious why you eliminate such things?

                    --
                    +-------------------------+--------------------+-----------------------+
                    | Kenneth J. Brody | www.hvcomputer.com | #include |
                    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                    +-------------------------+--------------------+-----------------------+
                    Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                    Comment

                    • Willem

                      #85
                      Re: Why are variables stored on the stack?

                      Paul wrote:
                      ) A "hardware stack" is not equivalent to the C stack. If you say "the
                      ) stack" in this newsgroup, you must be referring to the ANSI C implicit
                      ) stack right? If you mean the *x86* stack, then you should say so.

                      The question was "why does C insist that variables are stored on the stack"
                      so 'the stack' in that context *cannot* mean 'the place the auto variables
                      are stored', because otherwise the question would be a tautology.

                      Reading further into the OP, it becomes obvious that 'the stack' in this
                      context means the call/return stack, where most C implementations do indeed
                      store most of the auto variables, but _that is not required_.


                      SaSW, Willem
                      --
                      Disclaimer: I am in no way responsible for any of the statements
                      made in the above text. For all I know I might be
                      drugged or something..
                      No I'm not paranoid. You all think I'm paranoid, don't you !
                      #EOT

                      Comment

                      • Jean-Marc Bourguet

                        #86
                        Re: Why are variables stored on the stack?

                        Keith Thompson <kst-u@mib.orgwrites :
                        Paul Hsieh <websnarf@gmail .comwrites:
                        That's a strawman argument. I've lost track of the number of times
                        that I've said that C *does* implicitly require a "stack" in the
                        computer science sense of a LIFO data structure.
                        Well, nothing prevents an implementation to dynamically allocate the
                        activation records and then garbadge collect them. This to make something
                        like

                        int *newInt() {
                        int i;
                        return &i;
                        }

                        having a reasonnable implementation defined behavior :-) Not really in the
                        spirit of C, but not impossible if it is a side effect of sharing the back
                        end of the compiler with a language having proper closures. Obviously, one
                        of the first optimization done by such a back end would be to use the
                        hardware stack if available for the activation records which don't need
                        dynamic allocation because they are not leaked. So the performance cost
                        of the feature would be quite minimal for properly written C.

                        A+

                        --
                        Jean-Marc

                        Comment

                        • Paul Hsieh

                          #87
                          Re: Why are variables stored on the stack?

                          On Mar 17, 1:49 pm, Willem <wil...@stack.n lwrote:
                          Paul wrote:
                          ) A "hardware stack" is not equivalent to the C stack. If you say "the
                          ) stack" in this newsgroup, you must be referring to the ANSI C implicit
                          ) stack right? If you mean the *x86* stack, then you should say so.
                          >
                          The question was "why does C insist that variables are stored on the stack"
                          so 'the stack' in that context *cannot* mean 'the place the auto variables
                          are stored', because otherwise the question would be a tautology.
                          Its only tautological, if you already know the answer. But a teacher
                          or other environment will typically refer to "the stack" or something
                          of that nature in C or other languages while not explaining to the
                          student that the stack is resultant, not an a priori structure that is
                          prerequisite.
                          Reading further into the OP, it becomes obvious that 'the stack' in this
                          context means the call/return stack, where most C implementations do indeed
                          store most of the auto variables, but _that is not required_.
                          Yeah ok, but that means his question makes a semantic confusion that
                          some people here, for some reason, are buying into.

                          --
                          Paul Hsieh
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


                          Comment

                          • Keith Thompson

                            #88
                            Re: Why are variables stored on the stack?

                            Paul Hsieh <websnarf@gmail .comwrites:
                            On Mar 18, 9:08 am, Keith Thompson <ks...@mib.orgw rote:
                            >Paul Hsieh <websn...@gmail .comwrites:
                            On Mar 16, 1:16 am, Keith Thompson <ks...@mib.orgw rote:
                            >Ian Collins <ian-n...@hotmail.co mwrites:
                            jacob navia wrote:
                            >Keith Thompson wrote:
                            >>You insisted that this correct response was wrong. And that is what
                            >>led to this entire wasteful argument.
                            >>
                            >Yes, discussing with you and falconer is a waste of time
                            >You just say
                            >>
                            >"C has no stack"
                            >>
                            Come on then, put up or shut up, where did Keith say that?
                            >>
                            >I'm fairly sure I've never said that. In fact, I've criticized
                            >CBFalconer for saying that. It's arguably a true statement (for one
                            >particular meaning of the word "stack"), and I think we both
                            >understand what he means by it, but it's also incomplete and
                            >potentially misleading.
                            >>
                            That's called equivocation.
                            >>
                            >No, it's called precision.
                            >
                            You are using the word precision to describe "I think we both
                            understand what he means by it"? What is this, alt.satire or
                            something?
                            >
                            In any event, that's besides the point. If you wish, I will grant
                            that you are being precise -- in fact you are elucidating your
                            equivocation very precisely by repeating your mistake over and over.
                            If I understand you correctly, you're saying that, at least in the
                            context of this newsgroup, the word "stack" can only refer to the
                            computer science concept of an abstract LIFO data structure, and that
                            using the word to refer to a particular contiguous hardware
                            implementation is wrong.

                            In my opinion, both uses of the term are legitimate. One of those
                            meanings is applicable to the C language, at least to the C abstract
                            machine. The other is not, but is applicable only to particular
                            implementations (most of them, as it happens).

                            My point in citing the derivation of the word was that "stack", like
                            any word, can and does have different meanings in different contexts.

                            I respect your opinion that one meaning is correct and the other is
                            wrong, but I don't share that opinion. As far as I can tell, that
                            disagreement (which I think is a minor one) is what's motivated you to
                            flame me rather than, say, try to help answer the OP's question.

                            [snip]
                            The confusion is a matter of perspective. You are implicitly bringing
                            in a vendor's CPU implementation of the stack, while ignoring the C
                            language defined stack. *In this newsgroup* that you, among others,
                            insist must not deviate from discussing C.
                            No. I *explicitly* discussed a vendor's CPU implementation of the
                            stack (and it's not just one vendor; I first encountered the concept
                            on the PDP-11, and most CPUs support something similar). I did so
                            because the original poster *implicitly* used the term "stack" in that
                            sense.

                            As for "ignoring the C language defined", perhaps you're confusing me
                            with somebody else, because I've discussed it at considerable length.
                            In this case, I endorse the distinction of the implicit stack, because
                            the details of a hardware stack are rarely relevant to any compiler.
                            Huh? They're not relevant to the language, but they're extremely
                            relevant to any compiler that has to generate code that uses it.
                            Also, modern compilers heavily make use of the as-if rule, to simplify
                            and even eliminate the storage of certain auto-variables,
                            Yes, of course.
                            making the
                            mapping to hardware implementation truly meaningless.
                            It's not completely meaningless, but it's outside the scope of the
                            language.
                            >It's very clear that the original poster was using the word "stack"
                            >the way Intel does; otherwise the question wouldn't make sense.
                            >
                            Yeah ok, but that's a mistake.
                            Which you could have corrected rather than flaming me.

                            [snip]
                            >That's a strawman argument. I've lost track of the number of times
                            >that I've said that C *does* implicitly require a "stack" in the
                            >computer science sense of a LIFO data structure.
                            >
                            Oh don't worry about that. I can trace back to when you original
                            started saying this. I think we both know exactly when that was.
                            Perhaps you do. Maybe you've kept better track of what I've written
                            here than I have. I have no idea what point you're trying to make.
                            >[...] Those responses were both correct (the
                            >C language doesn't insist on any such thing) and very relevant to the
                            >original question.
                            >>
                            No, those responses are just technically wrong. Its like saying that
                            ice cubes are not cubes because they can be made of various kinds of
                            water.
                            >>
                            >It's like saying (in fact, it is saying) that the C language, though
                            >it implicitly requires a LIFO mechanism to manage automatic storage
                            >(or at least requires programs to behave *as if* there is such a
                            >mechanism), it does not require a contiguous hardware stack that grows
                            >in a particular direction (what Intel, correctly or incorrectly, calls
                            >"the stack"). If you want to suggest a better way to express that
                            >distinction *without* ignoring the fact that people use the word
                            >"stack" in at least two different ways, please do so.
                            >
                            Yeah there's a simple way. Just be consistent, and in CLC, the stack
                            is the stack that is implicitly defined by the language. There's no
                            controversy if you do that, because its technically correct without
                            introducing any new or restrictive semantics, meaning or terminology.
                            Also because the correctness rate of anything else said about stacks
                            in this newsgroup, even in this thread alone is so ridiculous low,
                            that there's nothing of any substance lost in rejecting that alternate
                            form of discourse.
                            I said "*without* ignoring the fact that people use the word 'stack'
                            in at least two different ways". If you want to ignore that fact, go
                            ahead; I'd rather acknowledge it and help people understand what's
                            really going on.

                            --
                            Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            Working...