Frequently Asked Questions about HW4


Q.
How big should we make the stack?

A.
It has no predetermined size. Since it is to be implemented as a linked list, it should grow as big as there is memory.


Q.
Can we use a doubly-linked list in this homework?

A.
You must do it with a singly-linked list as specified.


Q.
In the stack initialize function, do we need to allocate memory for the stack? It does not seem logical to do so. I would assume memory is allocated as needed for nodes?

A.
Your assumption is correct.


Q.
What kind of sentinels (error codes) should we use in our module?

A.
The Stack functions don't return any sentinels on errors. The main program must handle all errors before they reach the stack.


Q.
In declaring a stack, is:
stackT s1;
sufficient?

A.
Yes. Remember we have provided a sample main program that demonstrates how to use the stack.


Q.
How should the StackPush function behave if there is no room in memory for a new node?

A.
Print an error message and exit the program, i.e., the same as we did with the array implementation in lab when space could not be allocated.


Q.
How can we deal with the arguments to the program? Aren't they read in as a single string?

A.
You don't read in all the arguments as a single string. You get each argument as a separate string, in an array of strings (see Lab 5 - Program Arguments).


Q.
How can we distinguish whether an argument is an operator or number?

A.
You'll have to look at the characters in the string to distinguish the two. For example, a string that begins with "n" is obviously the "neg" operator (or an error). Something that starts with a digit or "." is a number (of course, there are other valid starts to numbers)....and so on.


Q.
I read your e-mail about not poking into the stack in the main program. I have a function in the main program (let's call it EnoughElements()) that does something like that to determine whether the required number of elements for an operation are present in the stack. Should I just move this function into the Stack Module then?

A.
You can't move it to the stack module either since we only allow the functions we asked you to implement to be part of the interface (you can, of course, have helper functions in the implementation file that are not accessible in the main program).

What you'll need to do is either rewrite EnoughElements() so that it only uses the stack functions we asked you to write (or just get rid of it and do it in terms of the stack functions). It's really not difficult to determine whether there are enough elements for a unary or binary operator using the stack functions we are allowing.


Q.
How do we get the arguments into argv?

A.
You don't have to write any code to put the arguments into argv (and their count in argc). Once you declare them as parameters in main(), then when you run the program, they will automatically have the arguments. You just need to access them.

Try running a simple test program with some arguments and then print out what's in argc, argv[0], etc. (Be careful though. Obviously, you can't access argv[3] if there are less than 4 arguments. Remember the 1st argument is the program name.)


Q.
If argv[i] is a single argument (as a string), how do I access a single character in one of those arguments?

A.
Normally, if you have a string variable, you just use subscripts to access an individual character, so add another subscript to argv[i].


Q.
What's the difference between returning from main() and calling exit()? What value should be returned in both cases?

A.
Returning from main() has the same effect as calling exit() from any other function in the program--they both terminate the program and return a value back to the operating system.

You should return the value 0 when all went well. Return a non-zero number (positive) when an error occurs. Either exit with the same error code for all errors (usually 1) or come up with different numbers for different classes of errors.


Q.
What do we do if the user enters integers, like:
% h4calc 7 5 / .....
?

A.
Assume all values are doubles (i.e., convert the strings to doubles), even if they don't have a fractional part.


Q.
What do the following warnings mean?
h4stack.c: In function `StackPush':
h4stack.c:36: warning: implicit declaration of function `malloc'
h4stack.c: In function `StackPop':
h4stack.c:66: warning: implicit declaration of function `free'

A.
They indicate that you have not included the header file stdlib.h for the prototypes of malloc/free.


Q.
In the stack destroy function, do we need to free each node of the linked list, or can we just point the pointer to NULL?

A.
You must free them. Setting the pointer to NULL without deallocating the nodes would leave them sitting out in memory (until the program ends on most systems). If fact, some weird systems might not even free allocated memory when the program ends. That's why it is important you do it.

We'll at least expect you to destroy all stacks before the program ends successfully. In reality, you should destroy stacks when the program ends due to errors too (i.e., under all conditions), but we won't be that picky this time.


Q.
When I compile, I get:
gcc -ansi -pedantic -Wall -lm -g -c h4calc.c
gcc: -lm: linker input file unused since linking not done
gcc -ansi -pedantic -Wall -lm -g -o h4calc h4calc.o h4stack.o

A.
It's complaining that it's useless to "link in the math library" (-lm) for commands that "only compile" (-c). -lm should only be with the command that does the linking.


Q.
When I call isdigit, as in:
if (isdigit(argv[i][0]))
  ..
I get a "warning: subscript has type 'char'"?

A.
It might be that there is a weird definition of isdigit() that requires an int. Store the character in an int and then pass that to isdigit().


Q.
What should we do if there is more than one operand left at the end? For example,
h4calc 20.0 22.0 + 4.2
Should the above line print an error message saying too many operands or should it just output 4.2?

A.
Whatever is at the top of the stack at the end is the result. You need not do anything with extra operands on the stack.


Q.
Do we need to handle when the user enters no arguments to the program?

A.
Your program should do something reasonable under that case, but you need not demonstrate it as one of your test runs.


Q.
I get an error like:
h4makefile:5: *** missing separator.  Stop.
when I use my make file?

A.
Do the compile/link lines not begin with Tab's? Remember spaces won't work.


BU CAS CS 113 - FAQ - HW4