Stacks?

This is a discussion on Stacks? within the C++ Programming forums, part of the General Programming Boards category; I was reading the tutorial page on stacks and well, to be frank, it was "bare". it just covered what ...

  1. #1
    Registered User Cloud6's Avatar
    Join Date
    Jul 2006
    Location
    USA
    Posts
    11

    Stacks?

    I was reading the tutorial page on stacks and well, to be frank, it was "bare". it just covered what a stack was but not how it was useful or how to implement. I was wondering if its a work in progress or if it was it. If thats all there is, than can someone suggest a place for further reading? I've searched but I can't seem to find any good articles.

    Thanks in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Last edited by matsp; 11-27-2007 at 02:47 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Cloud6's Avatar
    Join Date
    Jul 2006
    Location
    USA
    Posts
    11
    Actually yes, very helpful. Can't believe I didn't check Wikipedia. Me and my stupidity I guess.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The "back" button on your Internet browser uses a stack. It's "last-in-first-out" (LIFO)... It starts at the most-recently visited page, and works backwards to the beginning. The stack starts-out empty. Every time you visit a new page, the web address of the page you are leaving is popped onto the stack, and the stack-pointer points to it.

    Stacks are used internally by computers to store temporary data. It's kind-of hard to explain why... When a computer program calls a function, it needs to save a return pointer and the function may need some stack-space to save it's variables. If that function calls another function, more stack space is needed, and that data is added to the top of the stack.

    When a function returns, its stack-space is no longer needed. The last-called function is the first to return... Everything goes in reverse order as the functions "unwind". The stack will grow and shrink as needed.

    When it comes to applications, I'd say a queue (first-in-first-out = FIFO) is more common than a stack... When you write programs, you are more likely to need FIFO storage than LIFO storage. For example, when you send jobs to a printer, they are stored in a queue. When you are working with audio/video, the data is often stored in a FIFO buffer (a queue).

    I was reading the tutorial page on stacks and well, to be frank, it was "bare".
    Most tutorials are very brief. You may need to visit several websites to get the kind of detail and and explanation that you get in a 300 - 700 page beginning C++ book.

    Most stack-explanations describe a stack of dinner plates. The important thing to remember is that you want the data written on the plate. You can only add or remove pates to the top of the stack. You normally write data as you "push" an additional plate onto the top of the stack, and you read the data as you "pop" the top-plate off of the stack. ...The analogy doesn't really hold-up that well, because you can manipulate the stack-pointer and access data in the middle of the stack.
    Last edited by DougDbug; 11-27-2007 at 03:59 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    @DougDbug: You shouldn't manipulate things in the middle of the stack in a proper stack-based implementation [I'm not talking about the call-stack in the processor here, where that sort of thing is very much the right way to behave].

    @all:
    Stacks are often used for implementing math expression evaluation, e.g. evaluate using standard mathematical precedence: 1 + 3 * 2 + 4 / 2. There are other ways to solve the same problem, but a stack does this nicely.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 08:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 02:28 AM
  3. Avioding Stacks
    By LoafOfBread34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 06:20 AM
  4. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 06:48 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 03:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21