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 Stack The 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;  ...

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 betw...