Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bug in stack

Reply
Thread Tools

bug in stack

 
 
Paul
Guest
Posts: n/a
 
      02-28-2011

"Leigh Johnston" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> On 28/02/2011 20:12, Paul wrote:
>>
>> "Leigh Johnston" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) ...
>>> On 28/02/2011 00:33, Paul wrote:
>>>>
>>>> "asit" <(E-Mail Removed)> wrote in message
>>>> news:(E-Mail Removed)...
>>>>
>>>>
>>>>> 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
Why stack overflow with such a small stack? Kenneth McDonald Ruby 7 09-01-2007 04:21 AM
"stack level too deep"... because Threads keep their "starting" stack Sam Roberts Ruby 1 02-11-2005 04:25 AM



Advertisments