Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Co-routine

Name: Anonymous 2012-01-23 17:40

Is this iterator implementation well-defined?


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

typedef struct it {
    int *start;
    int len;
    struct it *next;
} iterator_t;

static iterator_t *head = NULL;

static iterator_t *iterator(int array[], int length)
{
    static iterator_t *ptr = NULL;

    if (head == NULL) {
        head = malloc(sizeof(iterator_t));
        ptr = head;
    } else {
        ptr->next = malloc(sizeof(iterator_t));
        ptr = ptr->next;
    }

    ptr->start = array;
    ptr->len = length;
    ptr->next = NULL;

    return ptr;
}

static int iterate(iterator_t *it)
{
    static iterator_t *current = NULL;
    static int state = 0, *ptr;

    if (it != NULL) {
        /* Initialize */
        current = it;
        ptr = it->start;
        state = 0;
    } else {
        /* Co-routine */
        switch (state) {
            case 0: while (ptr != (current->start + current->len)) {
                        state = 1;
                        return *ptr;
                       
                        case 1: ++ptr;
                    }
        }
    }

    return -1;
}

int main(void)
{
    iterator_t *it, *ptr;
    int n, numbers[] = { 4, 5, 6, 0, 1, 9, 0, 0, 8 };

    it = iterator(numbers, sizeof(numbers)/sizeof(int));

    iterate(it);
    while ((n = iterate(NULL)) != -1) {
        printf("%d\n", n);
    }

    it = head;
    while (it) {
        ptr = it->next;
        free(it);
        it = ptr;
    }
    return 0;
}

Name: Anonymous 2012-01-23 20:08

>>39
Aren't you suppose to have mental midgets to think for you since you're a SOFTWARE ENGINEER?

Name: Anonymous 2012-01-23 20:08

OP here, well this thread went sour. Off to bed.

Name: Anonymous 2012-01-23 20:10

>>40
who am I then?

Name: Anonymous 2012-01-23 20:11

>>41
You forget that I work at Kodak. The mental midgets either end up in tech support, marketing, or they get laid off.

Name: Anonymous 2012-01-23 20:12

>>43
The douche that doesn't use internet slang?

Name: Anonymous 2012-01-23 20:14

>>44
Do you think I could get a internship working under you kodak-kun?

Name: Anonymous 2012-01-23 20:15

>>45
Apparently you can't tell either ``mental midget''

Name: Anonymous 2012-01-23 20:19

>>46
Are you capable of modifying an existing linux program that handles flash? In particular, you would need to modify it in such a way that when used with a series of scripts, it could automate the book making process on our website. In addition, we would need you to be able to port the shit over to OSX.

This is because a large portion of our customer base uses this OS.

Name: Anonymous 2012-01-23 20:21

>>48
Never mind I think I'll just remain a toilet scrubber.

Name: Anonymous 2012-01-24 3:13

>>48
The glorious Kodak-kun does this for a living? What a joke.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List