Difference between the stack and the heap?

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

    Difference between the stack and the heap?

    Could someone please illustrate this with some ANSI C code? :-)

    Zach

  • Lew Pitcher

    #2
    Re: Difference between the stack and the heap?

    On Feb 19, 1:00 pm, "Zach" <net...@gmail.c omwrote:
    Could someone please illustrate this with some ANSI C code? :-)
    Nope.

    Primarily because the ANSI and ISO standards do not mention (let alone
    distinguish between) stack and heap.

    Sorry
    --
    Lew

    Comment

    • Clever Monkey

      #3
      Re: Difference between the stack and the heap?

      Zach wrote:
      Could someone please illustrate this with some ANSI C code? :-)
      >
      Do you mean implemented a stack and a heap, or how some runtime
      environments may utilize things called "the stack" and "the heap".

      The former can be addressed in any good book or Google search. The
      latter does not have anything to do with ANSI C, I think.

      Comment

      • Zach

        #4
        Re: Difference between the stack and the heap?

        On Feb 19, 1:18 pm, Clever Monkey
        <clvrmnky.inva. ..@hotmail.com. invalidwrote:
        >
        The former can be addressed in any good book or Google search. The
        latter does not have anything to do with ANSI C, I think.
        Ah, been reading some posts in here and see heap and stack mentioned.
        I have a vague understanding of what they are. Thought there was way
        to illustrate this with some code in C.

        Zach


        Comment

        • Zach

          #5
          Re: Difference between the stack and the heap?

          On Feb 19, 1:27 pm, "Zach" <net...@gmail.c omwrote:
          >
          Ah, been reading some posts in here and see heap and stack mentioned.
          I have a vague understanding of what they are. Thought there was way
          to illustrate this with some code in C.
          To answer Clever Monkey:

          I was thinking about the latter: "the stack" and "the heap".
          Never saw or learned the former either: "stack" and "heap" in C
          yet :-)

          Zach

          Comment

          • Rachael

            #6
            Re: Difference between the stack and the heap?

            On Feb 19, 6:00 pm, "Zach" <net...@gmail.c omwrote:
            Could someone please illustrate this with some ANSI C code? :-)
            >
            Zach
            If you just declare a variable or array like this:
            int n;
            char ac[5];
            then it's on the stack. When it goes out of scope (i.e. when you exit
            the function or block in which it was declared) the memory which it
            took up is automatically given back.

            If you allocate memory using malloc, like this:
            char * pc = malloc(5);
            then the memory is allocated on the heap, and will not automatically
            be given back when the variable goes out of scope, so you have to
            explicitly free the memory:
            free(pc);

            Rachael

            Comment

            • Default User

              #7
              Re: Difference between the stack and the heap?

              Rachael wrote:
              On Feb 19, 6:00 pm, "Zach" <net...@gmail.c omwrote:
              Could someone please illustrate this with some ANSI C code? :-)

              Zach
              >
              If you just declare a variable or array like this:
              int n;
              char ac[5];
              then it's on the stack.
              This is not required by the standard, but is often not even true when
              the system uses a stack. Automatic variables can be and sometimes are
              held in registers.
              When it goes out of scope (i.e. when you exit
              the function or block in which it was declared) the memory which it
              took up is automatically given back.
              Again, no such behavior is mandated.
              If you allocate memory using malloc, like this:
              char * pc = malloc(5);
              then the memory is allocated on the heap, and will not automatically
              be given back when the variable goes out of scope, so you have to
              explicitly free the memory:
              free(pc);
              What you say about the lifetime is correct, but no such entity as a
              heap is required to do this.





              Brian

              Comment

              • Zach

                #8
                Re: Difference between the stack and the heap?

                Thanks for the code illustration Rachael.

                Zach

                Comment

                • Thad Smith

                  #9
                  Re: Difference between the stack and the heap?

                  Zach wrote:
                  On Feb 19, 1:27 pm, "Zach" <net...@gmail.c omwrote:
                  >
                  >>Ah, been reading some posts in here and see heap and stack mentioned.
                  >>I have a vague understanding of what they are. Thought there was way
                  >>to illustrate this with some code in C.
                  >
                  To answer Clever Monkey:
                  >
                  I was thinking about the latter: "the stack" and "the heap".
                  Never saw or learned the former either: "stack" and "heap" in C
                  yet :-)
                  Provide enough context, usually be quoting, so that you meaning becomes
                  apparent. It is not clear from the message what the latter and former are.

                  --
                  Thad

                  Comment

                  • CBFalconer

                    #10
                    Re: Difference between the stack and the heap?

                    Zach wrote:
                    >
                    Could someone please illustrate this with some ANSI C code? :-)
                    What stack? What heap? No such things are defined in ISO C.

                    --
                    <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                    <http://www.securityfoc us.com/columnists/423>

                    "A man who is right every time is not likely to do very much."
                    -- Francis Crick, co-discover of DNA
                    "There is nothing more amazing than stupidity in action."
                    -- Thomas Matthews


                    Comment

                    Working...