Re: Why leave the error handling to the caller?




"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote in message news:crOdnbKFN_t84-bbnZ2dnUVZ8vmdnZ2d@xxxxxxxxx
Malcolm McLean said:

<snip>

No, the specification was to produce a function that filled out the
structure giving a list of strings, from an input consisting of a line
of strings separated by commas.

Separate the concept of a list of strings from the concept of input
parsing.

Now separate the concept of a list from the concept of a string.

Now the error handling is becoming manageable (you will recall that you
originally wrote that code to demonstrate that the error handling was a
PITN to write), and the program's complexity is being reduced as more
and more work is farmed out to re-usable library modules.

What you end up with is rather elegant (if done properly), and eminently
re-usable.

The problem is you start introducing dependencies.
The example is a little bit artificial because we haven't said what we are using our list of strings for.
However a programming interface

STRINGS *list = makestrings("One,two,three,four");
if(!list)
/* that old problem again - a 2GB computer is more likely to break
than to execute this */
exit(EXIT_FAILURE);
/* use strings */
killstrings(list);

is perfectly reasonable; you might want to pass a series of strings in one go and one parameter rather than mess about with add_string functions. Certainly an object constructor very often needs to take a list.

The question is the internals. High-level languages achieve their neatness by using dynamic arrays. We can do that in C, but only by seriously messing about with the language.

/*
add an item to a dynamic list.
Params: ptr - pointer allocated with malloc(), can be null
N - number of items pointer currently holds
item - item to add to list
sz - element size
Returns: pointer to new array on success, 0 on fail.
*/
void *dlist_add(void *ptr, int N, void *item, size_t sz)
{
unsigned char *temp;

temp = realloc(ptr, (N + 1) * sz);
if(!temp)
return 0;
memcpy(temp + N * sz, item, sz);
return temp;
}

Now we've got the equivalent of an STL-style vector, but it is horrid, horrid. No one is going to use that.
Then if we put dlist_add into a separate file, suddenly we got a dependency, and the old problem of you want a list of strings and that requires pulling in a whole hierarchy of files.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

.



Relevant Pages

  • Re: Combobox help
    ... then put text strings as the ... > Hi Ken - I want ALL THREE options to display in the combobox as THREE ... > separate lines, and then I want the user to select ONE of the three ... >>> Sue Compelling ...
    (microsoft.public.access.forms)
  • Re: Symbols, metacircular evaluation, external representation.
    ... they treated as strings instead of a separate data type? ... a separate or more limited language to do your macros, ... I doubt that's all the application of external representations. ...
    (comp.lang.scheme)
  • Re: Search utility?
    ... Different text strings. ... Most web sites with search engines look for wordone OR wordtwo. ... Perhaps there is a way to search two separate strings in one pass. ...
    (comp.os.os2.apps)
  • Re: Why leave the error handling to the caller?
    ... structure giving a list of strings, from an input consisting of a line ... of strings separated by commas. ... Now separate the concept of a list from the concept of a string. ... and more work is farmed out to re-usable library modules. ...
    (comp.lang.c)
  • Re: function
    ... The logical operators all return 1 to mean true and 0 to mean false. ... when x is a pointer, ... the two strings were the same. ... the strcpy() that you had above the test, ...
    (comp.lang.c)