bug in stack

Discussion in 'C++' started by asit, Feb 27, 2011.

  1. asit

    asit Guest

    what's wrong in the following code ??

    #include <iostream>
    #include <string>

    using namespace std;

    struct node
    {
    int data;
    node *prev;
    };

    node* push(node * st, int data)
    {
    if(st == NULL)
    {
    st = new node;
    st->prev = NULL;
    st->data = data;
    }
    else
    {
    node* t = new node;
    t->prev = st;
    t->data = data;
    st = t;
    }
    return st;
    }
     
    asit, Feb 27, 2011
    #1
    1. Advertising

  2. asit <> writes:

    > what's wrong in the following code ??


    > struct node
    > {
    > int data;
    > node *prev;
    > };
    >
    > node* push(node * st, int data)
    > {
    > if(st == NULL)
    > {
    > st = new node;
    > st->prev = NULL;
    > st->data = data;
    > }
    > else
    > {
    > node* t = new node;
    > t->prev = st;
    > t->data = data;
    > st = t;
    > }
    > return st;
    > }


    Do you really think both cases are different? Is it really relevant to
    distinguish a null incoming stack and a non-null stack?

    (By the way, I would advise to not use function arguments as local
    variables. You'll save nothing, the compiler is anyway much better at
    optimizing than you are. Use as many local variables as you want/need,
    give them significant names, and avoid reassigning different values to
    the same variable through the course of the function. If you apply this
    rule to the code above, you'll see why I ask the questions above.)

    Finally, how do you call push()? Your version returns the "new stack",
    make sure you use that value.

    I guess you haven't learnt constructors yet. If you have, try to think
    about how you could use them here. If you have not, forget about them
    for now.

    -- Alain.
     
    Alain Ketterlin, Feb 27, 2011
    #2
    1. Advertising

  3. On 27 fév, 21:01, Paavo Helde <> wrote:
    > asit <> wrote in news:e9e79c27-af4b-41ea-91e9-
    > :
    >
    > > what's wrong in the following code ??

    >
    > Why do you think there is something wrong with this code?
    >
    >
    >
    > > #include <iostream>

    >
    > not needed
    >
    > > #include <string>

    >
    > not needed
    >
    >
    >
    > > using namespace std;

    >
    > not needed
    >
    >
    >
    > > struct node
    > > {
    > >         int data;
    > >         node *prev;
    > > };

    >
    > > node* push(node * st, int data)
    > > {
    > >         if(st == NULL)
    > >         {
    > >                 st = new node;
    > >                 st->prev = NULL;
    > >                 st->data = data;
    > >         }

    >
    > duplicate code, the else branch has exactly the same functionality.
    >
    > >         else
    > >         {
    > >                 node* t = new node;
    > >                 t->prev = st;
    > >                 t->data = data;
    > >                 st = t;
    > >         }
    > >         return st;
    > > }

    >
    > Other than lots of unneeded lines and missing specifications about the
    > purpose of the struct and the function, I do not see nothing wrong with
    > this code.


    He is assigning the function parameter value 'st'. It shoukld either
    be a pointer-pointer or a pointer-reference.

    A coworker of mine thinks all parameter values should be const; I
    disagree for readability reasons but it could be a interesting
    compiler option.

    [snip]

    --
    Michael
     
    Michael Doubez, Feb 27, 2011
    #3
  4. asit

    Paul Guest

    "asit" <> wrote in message
    news:...
    > what's wrong in the following code ??
    >
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    > struct node
    > {
    > int data;
    > node *prev;
    > };
    >
    > node* push(node * st, int data)
    > {
    > if(st == NULL)
    > {
    > st = new node;
    > st->prev = NULL;
    > st->data = data;
    > }
    > else
    > {
    > node* t = new node;
    > t->prev = st;
    > t->data = data;
    > st = t;
    > }
    > return st;
    > }
    >
    >

    If you pass another stack as a node any original stack nodes are
    unreachable.
    The problem is that the data-node and the stack are both the same object.

    HTH
     
    Paul, Feb 28, 2011
    #4
  5. * Christian Hackl:
    > asit ha scritto:
    >
    >> what's wrong in the following code ??

    >
    > Most importantly, the fact you don't use a standard C++ container class,
    > such as std::stack (if you really want a stack).


    Is that so? I think it's a bit of a condescending remark. There are a
    lot of reasons why you'd want to implement a stack (or a list, or a
    set) yourself - including a didactic one:

    You won't appreciate the value of a calculator if you never did any number
    crunching by hand.

    --
    Martijn van Buul -
     
    Martijn van Buul, Feb 28, 2011
    #5
  6. asit

    Paul Guest

    "Christian Hackl" <> wrote in message
    news:ikebd5$pnj$-september.org...
    > asit ha scritto:
    >
    >> what's wrong in the following code ??

    >
    > Most importantly, the fact you don't use a standard C++ container class,
    > such as std::stack (if you really want a stack).
    >
    >

    I agree with Martin , I find this comment a bit annoying..

    How did C++ programmers even manage pre-STL?
     
    Paul, Feb 28, 2011
    #6
  7. asit

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 28/02/2011 00:33, Paul wrote:
    >>
    >> "asit" <> wrote in message
    >> news:...
    >>
    >>> what's wrong in the following code ??
    >>>
    >>> #include <iostream>
    >>> #include <string>
    >>>
    >>> using namespace std;
    >>>
    >>> struct node
    >>> {
    >>> int data;
    >>> node *prev;
    >>> };
    >>>
    >>> node* push(node * st, int data)
    >>> {
    >>> if(st == NULL)
    >>> {
    >>> st = new node;
    >>> st->prev = NULL;
    >>> st->data = data;
    >>> }
    >>> else
    >>> {
    >>> node* t = new node;
    >>> t->prev = st;
    >>> t->data = data;
    >>> st = t;
    >>> }
    >>> return st;
    >>> }
    >>>
    >>>

    >> If you pass another stack as a node any original stack nodes are
    >> unreachable.
    >> The problem is that the data-node and the stack are both the same object.

    >
    > What utter nonsense. 'push' returns a pointer to the new top of the stack
    > *passed to it*; it doesn't affect any other pointers or stacks; you do not
    > know what the OP is doing with the return value of 'push'.
    >


    Utter nonsense is your deparment.

    You probably don't even understand so I will explain:
    Node* st1, st2;
    st2->prev = push(push(push(s1->prev,0),0),0);
    st1 = push(push(st2->prev,0),0);
     
    Paul, Feb 28, 2011
    #7
  8. * Leigh Johnston:
    > I need to hammer in a nail. I have a hammer in my toolbox but I prefer
    > to bash nails in with this heavy rock instead. The moral of the story:
    > don't make work for yourself.


    But many parts of STL (and even moreso: boost!) aren't really compareable
    with a hammer. They're more like a powerdrill. Useful, convenient to have
    around, and there's tons of people who think you need nothing else.

    --
    Martijn van Buul -
     
    Martijn van Buul, Feb 28, 2011
    #8
  9. * Paul:

    > You probably don't even understand so I will explain:
    > Node* st1, st2;
    > st2->prev = push(push(push(s1->prev,0),0),0);


    Segmentation fault, core dumped.

    --
    Martijn van Buul -
     
    Martijn van Buul, Feb 28, 2011
    #9
  10. asit

    Paul Guest

    "Martijn van Buul" <> wrote in message
    news:...
    >* Paul:
    >
    >> You probably don't even understand so I will explain:
    >> Node* st1, st2;
    >> st2->prev = push(push(push(s1->prev,0),0),0);

    >
    > Segmentation fault, core dumped.
    >

    It's ok its only for Leigh.
     
    Paul, Feb 28, 2011
    #10
  11. asit

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 28/02/2011 20:12, Paul wrote:
    >>
    >> "Leigh Johnston" <> wrote in message
    >> news:...
    >>> On 28/02/2011 00:33, Paul wrote:
    >>>>
    >>>> "asit" <> wrote in message
    >>>> news:...
    >>>>
    >>>>
    >>>>> what's wrong in the following code ??
    >>>>>
    >>>>> #include <iostream>
    >>>>> #include <string>
    >>>>>
    >>>>> using namespace std;
    >>>>>
    >>>>> struct node
    >>>>> {
    >>>>> int data;
    >>>>> node *prev;
    >>>>> };
    >>>>>
    >>>>> node* push(node * st, int data)
    >>>>> {
    >>>>> if(st == NULL)
    >>>>> {
    >>>>> st = new node;
    >>>>> st->prev = NULL;
    >>>>> st->data = data;
    >>>>> }
    >>>>> else
    >>>>> {
    >>>>> node* t = new node;
    >>>>> t->prev = st;
    >>>>> t->data = data;
    >>>>> st = t;
    >>>>> }
    >>>>> return st;
    >>>>> }
    >>>>>
    >>>>>
    >>>> If you pass another stack as a node any original stack nodes are
    >>>> unreachable.
    >>>> The problem is that the data-node and the stack are both the same
    >>>> object.
    >>>
    >>> What utter nonsense. 'push' returns a pointer to the new top of the
    >>> stack *passed to it*; it doesn't affect any other pointers or stacks;
    >>> you do not know what the OP is doing with the return value of 'push'.
    >>>

    >>
    >> Utter nonsense is your deparment.
    >>
    >> You probably don't even understand so I will explain:
    >> Node* st1, st2;
    >> st2->prev = push(push(push(s1->prev,0),0),0);
    >> st1 = push(push(st2->prev,0),0);
    >>

    >
    > Any why on earth would you write that code (ignoring the fact that it is
    > UB)? What exactly are you trying to achieve? Like I said: "utter
    > nonsense".
    >
    > Node* st1 = push(push(push(0, 0), 0), 0);
    > st1 = push(push(push(st1, 0), 0), 0);
    >
    > I see no problems here (nothing unreachable).
    >

    This code proves that.......you are an idiot!
     
    Paul, Feb 28, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surinder Singh
    Replies:
    1
    Views:
    1,237
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,134
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,377
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    234
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    285
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page