Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

bug in stack

 
 
asit
Guest
Posts: n/a
 
      02-27-2011
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;
}


 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      02-27-2011
asit <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Michael Doubez
Guest
Posts: n/a
 
      02-27-2011
On 27 fév, 21:01, Paavo Helde <(E-Mail Removed)> wrote:
> asit <(E-Mail Removed)> wrote in news:e9e79c27-af4b-41ea-91e9-
> (E-Mail Removed):
>
> > 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
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-28-2011

"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.

HTH

 
Reply With Quote
 
Martijn van Buul
Guest
Posts: n/a
 
      02-28-2011
* 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-28-2011

"Christian Hackl" <(E-Mail Removed)> wrote in message
news:ikebd5$pnj$(E-Mail Removed)-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?

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-28-2011

"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);



 
Reply With Quote
 
Martijn van Buul
Guest
Posts: n/a
 
      02-28-2011
* 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 - (E-Mail Removed)
 
Reply With Quote
 
Martijn van Buul
Guest
Posts: n/a
 
      02-28-2011
* 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 - (E-Mail Removed)
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-28-2011

"Martijn van Buul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>* 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.

 
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