Posts

Incompetent C Programming Example

I have been bothered by the C coding samples I commonly find on the Web. Almost all of them contain grievous defects. The following example of an array-based stack using C is based upon C code published at http://www-cs.ccny.cuny.edu/~peter/dstest.html

The C code published at http://www-cs.ccny.cuny.edu/~peter/dstest.html supports the book Advanced Data Structures authored by Peter BraƟ and published by Cambridge University Press. Array StackThe C code for Array Stack is shown below. Example 1 ArrayStack.c #include <stdio.h> #include <stdlib.h>
typedef int item_t;
typedef struct {item_t *base; item_t *top; int size;} stack_t;
stack_t *create_stack(int size) {stack_t *st; st = (stack_t *) malloc( sizeof(stack_t) ); st->base = (item_t *) malloc( size * sizeof(item_t) ); st->size = size; st->top = st->base; return( st ); }
int stack_empty(stack_t *st) {return( st->base == st->top ); }
int push( item_t x, stack_t *st) {if ( st->top < st->base + st…

An Ada Publish-Subscribe Producer-Consumer Exercise

There are many ways to express the classic producer-consumer problem in Ada. Following is a problem which illustrates some of the interesting features of Ada protected objects. Problem Statement This producer-consumer problem has several requirements: ·There shall be one producer and many consumers. ·The number of consumers shall be established at run-time through user input. ·The producer shall produce a series of random floating point numbers ·All consumers shall read each number produced by the producer exactly once. ·The producer shall not know the number of consumers The requirement that the producer shall not know the number of consumers prohibits the use of the Ada Rendezvous mechanism for direct communication between the producer and all the consumers. An alternative compliant with this requirement is to use a protected object as a shared buffer between the producer and all the consumers. In fact, the solution below uses a protected object that implements a publish-subscribe mecha…