Appropriate approaches.


R. Clayton (rclayton@clayton.cs.monmouth.edu)
(no date)


  I am working on the first assignment and I am using dynamically allocated
  array; is that an appropriate approach?

It depends on what you're using dynamic memory for. Given that you have no a
priori knowledge about word size or count, using dynamic memory is an
appropriate approach.

However, appropriateness also depends on the tools you have at hand. For
example, having read chapter 2, you know about string objects, so you could use
either string objects or dynamically allocated char arrays to represent words
of unknown length. Which is appropriate for this problem? I will argue
tomorrow that strings are always appropriate and char arrays are not because
char arrays are easy to use incorrectly and difficult to use correctly.

Appropriateness also depends on environments. In 509, as in most other
environments, it is important that your code be correct - that is, your code
should work as required by the problem statement. Unlike 509 however, other
environments may have other important characteristics that may shift
appropriateness from one tool to another.

For example, palm-top environments are resource constrained, and if you're
writing code for a plam-top, you may not have the memory or cpu cycles to spare
for string objects (which are much more expansive in time and space than are
char arrays). In a palm-top environment, it may be appropriate to use char
arrays instead of string objects.

Even in 509, though, appropriateness is not that simple. For example, if you
read my code, you'll see that I use char arrays for strings, despite what I
wrote above. If you read more closely, however, you'll see that I only use
char arrays for constant strings; that is, strings that don't (and can't)
change. The compiler correctly handles everything, so using char arrays for
constant strings is as simple and as safe as using string objects, and it's
much cheaper.

Anyway, to get back to assignment 1, because you haven't read chapter 3 yet,
the only way you know how to deal with an unknown number of words (that is,
tuples) is by a dynamically allocated array of something (string objects or
char arrays or pointers). In this case, the only tool you have is dynamic
memory, and so by default it is appropriate.



This archive was generated by hypermail 2.0b3 on Fri Dec 21 2001 - 17:00:04 EST