*BSD News Article 37123


Return to BSD News archive

Xref: sserve comp.os.386bsd.questions:13912 comp.os.386bsd.misc:3769
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.cs.su.oz.au!news.adelaide.edu.au!gateway.dircsa.org.au!cleese.apana.org.au!apanix.apana.org.au!hart
From: hart@apanix.apana.org.au (Leigh Hart)
Newsgroups: comp.os.386bsd.questions,comp.os.386bsd.misc
Subject: Re: How UTTERLY Amazing! (Was Re: FreeBSD vs NetBSD)
Date: 20 Oct 94 02:11:24 GMT
Organization: Apanix Public Access Unix, +61 8 373 5485 (5 lines)
Lines: 101
Message-ID: <hart.782619084@apanix.apana.org.au>
References: <37fvk8$9mm@rivendell.apana.org.au> <37p5f2$kjl@cleese.apana.org.au> <hart.782272416@apanix.apana.org.au> <37ubqf$3ae@news.panix.com>
NNTP-Posting-Host: seldon.apanix.apana.org.au

berke@panix.com (Wayne Berke) writes:

>hart@apanix.apana.org.au (Leigh Hart) writes:

>>So in reality, flame expects a pointer to struct twit, whereas phil only
>>passed the struct itself.  Lets rewrite that code for the pedants (mark):

>>while(&jesus) flame(&jesus);

>Oh no!  Correct me if I'm wrong, but this seems like an infinite loop!
>Hopefully, the flame() function a judicious call to exit().

Hmmm, yes it does seem to go on and on a bit doesn't it - but if flame()
called exit() (judiciously or otherwise) it wouldn't be a very well
behaved function, now would it?  Think back to those long lectures
where we were all taught good ADT programming!

The best we could hope for is to re-write the flame() implementation,
to accept struct twit ** instead, that way we could make jesus a pointer
to struct twit, and then pass &jesus.  Then we could set the pointer to 
NULL inside flame() to achieve the same (desirable!) result.

eg:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

struct twit {
    char name[80];
};

unsigned char i=0;

void flame(struct twit **);
void perror(char *);
int printf(char *,...);

void flame(struct twit **t) {
    printf("Flaming %s\n",(*t)->name);
    sleep(1);
    i++;
    if(i==5)
       (*t)=NULL;
}

void main() {
    struct twit *JMJr;
    if((JMJr=(struct twit *)malloc(sizeof(struct twit)))==NULL) {
        perror("Yay! malloc() of JMJr failed");
        exit(0);
    }
    strcpy(JMJr->name,"Jesus Monroy Jr <jmonroy@netcom.com>");
    while(JMJr)
        flame(&JMJr);
}


Script started on Thu Oct 20 12:10:59 1994
% ./foo
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming Jesus Monroy Jr <jmonroy@netcom.com>

script done on Thu Oct 20 12:11:08 1994

Hm, that seems to work - however when I mistakenly checked for the
wrong thing in the while condition, I got this result:

Script started on Thu Oct 20 12:01:12 1994

% ./foo
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming Jesus Monroy Jr <jmonroy@netcom.com>
Flaming (null)
Flaming (null)
Flaming (null)
Flaming (null)
Flaming (null)
^C

script done on Thu Oct 20 12:01:23 1994

Hmm, I wonder, perhaps the bug wasn't such a bad thing after all!

:-) for the humour impaired.

Cheers

Leigh
--
| "By the time they had diminished | Leigh Hart                |
|  from 50 to 8, the other dwarves | <hart@eppie.apana.org.au> |
|  began to suspect 'Hungry' ..."  | C/- 195 Gilles Street     |
|   -- Gary Larson, "The Far Side" | Adelaide  SA  5006        |