[Tutor] Stacks and Stack underflow/Stack overflow

Joe Batt joebatt at hotmail.co.uk
Sat Nov 19 20:37:49 CET 2011


Hi All 
Could some kind soul please explain why you get a stack underflow and a stack overflow.
I am getting the following error in Python 3
Traceback (most recent call last):  File "/Users/joebatt/Desktop/python/pickling puzzle 5.py", line 39, in <module>    a=pickle.load(file)_pickle.UnpicklingError: unpickling stack underflow
when I am running the following
import picklefile=open('///Users/joebatt/Desktop/banner.p.webarchive','rb')a=pickle.load(file)file.close()print (a)
Now I am very much a learner so please correct and explain my misunderstanding. I am visualising my program as taking the 'banner.p.webarchive' and and pushing it onto the stack byte by byte until it reaches the end of the file i.e. A B C ---> push C then B then A 
A B C --->	A		B		C
Then the program pops the stack C does what it needs to do to unpickle it then pops B does what it needs to unpickle then A.
A ----->C B ABC
My understanding is that the stack underflow means that it is trying to pop from the top of the stack and there is nothing to pop i.e. it is empty. Why though if the stack has been loaded with the file 'banner.p' in my program does it say there is nothing to pop and thus a stack underflow?
Also my understanding of the stack overflow is that the stack itself is a finite size and when it has tried to push the file to the stack it didn't fit because it was too big, i.e. in my example if the stack was only big enough for 2 letters and I tried to push ABC it would give a stack overflow because I was trying to push 3. How would I deal with this? Can I make the stack bigger or a way to just push A B pop AB then push C and pop it.
Thank you guys Im sorry for probably what are very silly basic questions.
Joe 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111119/97a7280d/attachment.html>


More information about the Tutor mailing list