pushing and popping

New to FreeBASIC? Post your questions here.
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

pushing and popping

Post by ivory348 »

Code: Select all

for i = 1 to 5
    iValue = i * 2
    pushValue stack(), top, iValue
    print "Stack top : ";ubound(stack),"Value : ";iValue
next
I don't understand the ubound staying 0 all the time
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: pushing and popping

Post by MrSwiss »

Just the For-Loop on its own doesn't make any sense ...
How is the Array stack() defined?
How is Procedure pushValue() implemented?
e.t.c.
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

Re: pushing and popping

Post by ivory348 »

Code: Select all

dim stack(0)as integer 
dim top as integer
dim i as integer
dim iValue as integer
sub pushValue(aStack() as integer, stackTop as integer, value as Integer)
    stacktop += 1
    redim preserve aStack(stackTop)
    ''Element data can be preserved during  
    ''resizing of variable-length arrays with
    ''the new Preserve specifier
    aStack(stackTop) = value
end sub

function popValue(aStack()as integer, stackTop as integer) as integer
    dim ret as  integer
    if stackTop = 0 then 
        ret = 0 
    else 
        ret = aStack(stackTop) 
        stackTop -= 1 
        redim preserve aStack(stackTop)
    end if
    return ret
end function

print "pushing values onto the stack"

for i = 1 to 5
    iValue = i * 2
    pushValue stack(), top, iValue
    print "Stack top : ";ubound(stack),"Value : ";iValue
    ''I don't understand the ubound staying 0 all the time
next

print

print "size before popping : "; ubound(stack)

print
iValue = popValue(stack(),top)
do while iValue > 0
    print "stack value : ";iValue
    iValue = popValue(stack(),top)
loop
print "size after the popping : "; ubound(stack)



sleep
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: pushing and popping

Post by MrSwiss »

Lets take this step by step:
dim stack(0)as integer
Is a fixed size Array with exactly 1 element (can't be re-sized, at all).
Therefore, you'll have no chance of succeeding, right from the start.

You need a resizable Array (aka: dynamic Array):
ReDim As Integer stack(0) --or--
Dim As Integer stack() --or--
Dim As Integer stack(Any)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: pushing and popping

Post by MrSwiss »

But, as stack's typically are of fixed size and work top-down, you could simply define more elements
and take it from there (stack's are usually capable of growing "uncontrolled", downwards).

Meaning: in real, it might just overwrite other code/data (use with care!)

(with user defined array, you might (just) induce a compiler error: "array out of bounds")
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

Re: pushing and popping

Post by ivory348 »

"No way of succeeding right from the start" - this is an example in a theory book on freeBasic; so it cannot be total garbage, I would say.
The code compiles alright.
When I run the code I get a list of values.
So the stack must contain a list of values.
Then I don't understand ubound being zero, both for every line in the displayed list and after the listing.
If the ubound is zero how can you pop values off the list? As the example makes you believe is happening.

Then also: what is the use of all this, in a program, given the existence of arrays.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: pushing and popping

Post by fxm »

MrSwiss is right.

If you compile with the option -exx, you will get a runtime error, because you attempt to resize an array declared as a fix-len array.
ivory348 wrote:"No way of succeeding right from the start" - this is an example in a theory book on freeBasic; so it cannot be total garbage, I would say.
What is the reference of this book ?

Note:
If you want that the 2 procedures modify the value of the passed argument 'top', this one must be passed to the procedures by reference (and not by value as by default):

Code: Select all

sub pushValue(aStack() as integer, byref stackTop as integer, value as Integer)
function popValue(aStack()as integer, byref stackTop as integer) as integer
Last edited by fxm on Jan 10, 2020 16:06, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: pushing and popping

Post by MrSwiss »

Then also: what is the use of all this, in a program, given the existence of arrays.
Well, first of all, the concept of push/pop comes from assembly language (not really BASIC).
Secondly, at times, examples might not serve any 'real live' purpose, they may only try to
explain a coding concept.
Thirdly, I've rewritten the code to show the stack concept:

Code: Select all

Dim As Integer  stack(1 To 10), top = 10, iValue

Sub pushValue(aStack() as integer, ByRef stackTop as integer, ByVal value as Integer)
    aStack(stackTop) = value
    stacktop -= 1
End sub

function popValue(aStack() As integer, ByRef stackTop as integer) as Integer
    if stackTop < LBound(astack) OrElse stacktop > UBound(astacK) Then
        Return 0
    Else
        stackTop += 1
    end if
    return aStack(stackTop)
end function


print "pushing values onto the stack"

for i As UInteger = 1 to 5
    iValue = i * 2
    pushValue(stack(), top, iValue)
    print "pushed Value: "; iValue, "current top: "; top
Next

Print : Print "array size before popping : "; ubound(stack)
Print : print "poping values from the stack"

For j As UInteger = UBound(stack) To UBound(stack) - 4 Step -1  
    iValue = popValue(stack(),top)
    print "poped value: "; iValue, "current top: "; top
Next

Print
print "array size after the popping: "; ubound(stack)

Sleep
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

Re: pushing and popping

Post by ivory348 »

Ok so it is of no use, but I still want to understand.

When I run the code I get a list of values.
So the stack must contain a list of values.
Then I don't understand ubound being zero, both for every line in the displayed list and after the listing.
If the ubound is zero how can you pop values off the list? As the example makes you believe is happening.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: pushing and popping

Post by fxm »

Your initial code, with just the 3 erroneous lines corrected:

Code: Select all

redim stack(0)as integer  '' corrected
dim top as integer
dim i as integer
dim iValue as integer
sub pushValue(aStack() as integer, byref stackTop as integer, value as Integer)  '' corrected
    stacktop += 1
    redim preserve aStack(stackTop)
    ''Element data can be preserved during 
    ''resizing of variable-length arrays with
    ''the new Preserve specifier
    aStack(stackTop) = value
end sub

function popValue(aStack()as integer, byref stackTop as integer) as integer  '' corrected
    dim ret as  integer
    if stackTop = 0 then
        ret = 0
    else
        ret = aStack(stackTop)
        stackTop -= 1
        redim preserve aStack(stackTop)
    end if
    return ret
end function

print "pushing values onto the stack"

for i = 1 to 5
    iValue = i * 2
    pushValue stack(), top, iValue
    print "Stack top : ";ubound(stack),"Value : ";iValue
    ''I don't understand the ubound staying 0 all the time
next

print

print "size before popping : "; ubound(stack)

print
iValue = popValue(stack(),top)
do while iValue > 0
    print "stack value : ";iValue
    iValue = popValue(stack(),top)
loop
print "size after the popping : "; ubound(stack)



sleep
Otherwise, it is difficult to explain the behavior of a program when the code is thereby buggy ('runtime error 4 (out of memory) at line 7'), because by principle, the program behavior is not predictable.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: pushing and popping

Post by badidea »

ivory348 wrote:Ok so it is of no use, but I still want to understand.
What is of no use, the example or the concept?
Stacks (with dynamic memory size) are used in many software programs.
ivory348
Posts: 49
Joined: Dec 14, 2019 12:07
Location: Groningen, Netherlands

Re: pushing and popping

Post by ivory348 »

"Otherwise, it is difficult to explain the behavior of a program when the code is thereby buggy ('runtime error 4 (out of memory) at line 7'), because by principle, the program behavior is not predictable."

The code I have posted compiles without errors.
Although I am truly grateful for your contributions, my questions remain unanswered.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: pushing and popping

Post by MrSwiss »

ivory348 wrote:... my questions remain unanswered.
Nobody wants to discuss not working as expected code (aka: buggy code).
(whether it compiles or not, is not the match deciding factor)

Study the codes from fxm/myself and ask again, if there is anything funny there ...
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: pushing and popping

Post by fxm »

If I compile your initial code (bugged) with fbc 64-bit with option -exx, the program hangs some times when executing (otherwise 'runtime error 4 (out of memory) at line 7').
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: pushing and popping

Post by speedfixer »

re: getting a list of values

You can always get a value from memory the system has allocated to you.

If that memory of NOT part of a declared function or variable, it has no useful meaning.

By allocating only one position at "dim stack(0)as integer" - only ONE memory position is valid.

You read beyond that one valid position.
Those other values could be anything from boot code to data from a pic in your last browser read: not valid as meaningful data in your program.

A very 'tight' language wold not allow you to do this.
FB is more flexible, as most full languages are, so many types of programmer 'errors' are allowed to let you be free to do what you want.

This is really, really basic programming and old guys sometimes forget that we had to learn these tiny, small things when we started, also.
Post Reply