Although I describe it as a queue in my list, the first few versions of this are a simple stack implemented in memory. Later versions will implement FIFO functionality.
Here's the stack:
base = malloc(arraySize * sizeof(int)); // Allocate memory for stack. NOTE: Memory is not freed.
"base" seems like a funny name for a stack pointer, but I expect to create a separate value for pointer and keep "base" for other stuff - like checking whether there have been more pops than pushes.
There's a push and a pop:
void push(int value)
{
*base = value; // Assign value to location.
base = base + 1; // Increase location one element.
}
int pop()
{
base = base - 1; // Decrease location to previous location
return *base; // Get value at current location
}
And I wrote various testing code, which lives in main(), such as the following:
for (i = 0; i (less than) 100; i++)
{
push(i);
}
This code (and some other I wrote) tests the program as it is intended to be used (no more pops than pushes and no pushes beyond the size of the stack.)
And here's some code that breaks it:
for (i = 0; i (less than) 1000000; i++)
{
push(i);
}
This got me a fun (and expected) "The memory could not be written." error.
The next version of the program will handle the edge cases - probably by throwing an exception if the stack tries to pop below 0 or push above 100.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment