Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array implementation of Stack

Reply
Thread Tools

Array implementation of Stack

 
 
Rui Maciel
Guest
Posts: n/a
 
      06-16-2011
Willem wrote:

> ) Why should pop return a value?
>
> For the same reason as push() takes two arguments.
>


And what reason is that?


Rui Maciel
 
Reply With Quote
 
 
 
 
Rui Maciel
Guest
Posts: n/a
 
      06-16-2011
Chris M. Thomasson wrote:

> That does create a "gap" between `top()' and `pop()'; no problem for
> single-threaded access...


And what leads you to believe that writing pop() so that it returns a value
will make the code immune to concurrency issues, or at least more vulnerable
than the version which doesn't return a value?


Rui Maciel
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-16-2011
Rui Maciel <(E-Mail Removed)> writes:
> Willem wrote:
>> ) Why should pop return a value?
>>
>> For the same reason as push() takes two arguments.

>
> And what reason is that?


Convenience.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Chris M. Thomasson
Guest
Posts: n/a
 
      06-16-2011
"Rui Maciel" <(E-Mail Removed)> wrote in message
news:4dfa7ac7$0$23523$(E-Mail Removed)...
> Chris M. Thomasson wrote:
>
>> That does create a "gap" between `top()' and `pop()'; no problem for
>> single-threaded access...

>
> And what leads you to believe that writing pop() so that it returns a
> value
> will make the code immune to concurrency issues, or at least more
> vulnerable
> than the version which doesn't return a value?


NO IMMUNITY!


I simply prefer the conditional `try_pop()' function because it can be so
easily used by the inherent pattern contained within the almighty
"eventcount" algorithm:

http://groups.google.com/group/comp....f4655c4e0b7c95

;^)


 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      06-17-2011
Rui Maciel wrote:
) Willem wrote:
)
)> ) Why should pop return a value?
)>
)> For the same reason as push() takes two arguments.
)>
)
) And what reason is that?

I don't know. Ask the person who proposed the API upthread.

If you insist that pop() shouldn't return a value, then push()
should not take a value either, just a stack. Otherwise it
is plainly inconsistent.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-17-2011
Willem <(E-Mail Removed)> writes:
> Rui Maciel wrote:
> ) Willem wrote:
> )
> )> ) Why should pop return a value?
> )>
> )> For the same reason as push() takes two arguments.
> )>
> )
> ) And what reason is that?
>
> I don't know. Ask the person who proposed the API upthread.
>
> If you insist that pop() shouldn't return a value, then push()
> should not take a value either, just a stack. Otherwise it
> is plainly inconsistent.


pop() not returning a value makes some sense if you think that
shrinking the stack and retrieving the top element should be
separate operations.

push() without an argument would have to push an indeterminate or
default value onto the stack.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      06-17-2011
Keith Thompson wrote:
) pop() not returning a value makes some sense if you think that
) shrinking the stack and retrieving the top element should be
) separate operations.

push() not taking a value makes some sense if you think that
expanding the stack and setting the top element should be
separate operations.

) push() without an argument would have to push an indeterminate or
) default value onto the stack.

pop() without a return value would have to discard the value it
takes from the stack.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Mark Storkamp
Guest
Posts: n/a
 
      06-17-2011
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:

> Willem <(E-Mail Removed)> writes:
> > Rui Maciel wrote:
> > ) Willem wrote:
> > )
> > )> ) Why should pop return a value?
> > )>
> > )> For the same reason as push() takes two arguments.
> > )>
> > )
> > ) And what reason is that?
> >
> > I don't know. Ask the person who proposed the API upthread.
> >
> > If you insist that pop() shouldn't return a value, then push()
> > should not take a value either, just a stack. Otherwise it
> > is plainly inconsistent.

>
> pop() not returning a value makes some sense if you think that
> shrinking the stack and retrieving the top element should be
> separate operations.
>
> push() without an argument would have to push an indeterminate or
> default value onto the stack.


Maybe the names are just poorly chosen. I always think of pop()
returning the top value and shrinking the stack. I would use drop() to
just shrink the stack. A push() without arguments could just duplicate
the top of the stack, but then you'd be better off calling that dup().
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-17-2011
Willem <(E-Mail Removed)> writes:
> Keith Thompson wrote:
> ) pop() not returning a value makes some sense if you think that
> ) shrinking the stack and retrieving the top element should be
> ) separate operations.
>
> push() not taking a value makes some sense if you think that
> expanding the stack and setting the top element should be
> separate operations.


*And* if you don't mind the new top of the stack having a meaningless
value. The two cases are not equivalent.

> ) default value onto the stack.
>
> pop() without a return value would have to discard the value it
> takes from the stack.


Of course. Sometimes that's what you want to do.

I'm not arguing that pop() *shouldn't* have a return value, just
that it's not entirely insane for it not to.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      06-17-2011
Willem <(E-Mail Removed)> writes:

> Keith Thompson wrote:
> ) pop() not returning a value makes some sense if you think that
> ) shrinking the stack and retrieving the top element should be
> ) separate operations.
>
> push() not taking a value makes some sense if you think that
> expanding the stack and setting the top element should be
> separate operations.


This would be an extraordinarily idiosyncratic usage.

> ) push() without an argument would have to push an indeterminate or
> ) default value onto the stack.
>
> pop() without a return value would have to discard the value it
> takes from the stack.


pop() without a return value wouldn't actually have to take a value from
the stack; it just moves the pointer.

> SaSW, Willem

 
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
Re: Array implementation of Stack luser- -droog C Programming 2 07-07-2011 06:00 PM
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
Confusion on an Array based stack implementation. Chris Mabee C++ 4 12-26-2004 03:08 PM



Advertisments