Thread: stack

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    stack

    I'm trying to write a stack implementation as a seperate namespace (actually completeing the code from The C++ Programming Language book).

    First of all, i dont understand whats
    typedef Rep& stack; in the code.

    What does this do? Makes Rep reference to stack? I've got a C background and i never saw something like this.

    Also, i'm not sure how to finish the create() function.

    What exactly should the function return? Reference to the allready existing array of structures. So im confused how to do that.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    //Stack interface
    namespace Stack {
              struct Rep;
              typedef Rep& stack;
              
              stack create();
              void destroy(stack s);
              
              void push(stack s, char c);
              char pop(stack s);
    }
    
    namespace Stack {
              const int max_size = 1024;
              
              struct Rep {
                     char v[max_size];
                     int top;
              };
              const int max = 16;
              Rep stacks[max];
              bool used[max] = { false };
    }
    
    int main()
    {
         Stack :: Rep omg;
         omg = Stack :: create();
         
         
         getchar();
         return 0;
    }
    
    
    Stack :: stack Stack :: create() 
    {
          int i;
          
          for(i = 0; i < Stack :: max; i++)
             if(Stack :: used[i] == false)
                break;
          if(i >= Stack :: max)
             throw "all stacks in usage!\n";
          Stack :: stacks[i].top = 0;
          Stack :: used[i] = true;
          return ...
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tool
    First of all, i dont understand whats
    typedef Rep& stack; in the code.

    What does this do? Makes Rep reference to stack?
    It makes stack to be an alias for Rep&, i.e., a reference to Rep.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    And what should create() return?

    The book says a reference to the stack in Stack ::.

    But writing return &stacks[i] isnt working.
    Last edited by Tool; 06-13-2010 at 02:33 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tool View Post
    And what should create() return?

    The book says a reference to the stack in Stack ::.

    But writing return &stacks[i] isnt working.
    No, but sans the & it works. References are slightly different than pointers. So in this case, omg is a Rep, and since create returns a reference to such, it works (see, different).

    The next thing I would try is to check and see whether omg turns out to be a copy (probably not).

    Take all that with a grain of salt. I haven't used references this way and that typedef just looks silly to me. It's as bad as this in C:
    Code:
    typedef *char chptr;
    What is the point of doing that? It's practically obfuscation.
    Last edited by MK27; 06-13-2010 at 02:56 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please don't put spaces either side of your scope resolution operator (that's the ::). It makes is very hard to read.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM