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

Generic programming in C

Name: Anonymous 2012-01-12 3:49

I love C so much, and I really want to hate sepples, but I can't help but think that generic programming in C is shit! Macros are no good for generic data structures, as they are clunky and blow out code size, nor are void pointers, as you need to allocate separate memory just to store an integer (don't stuff ints into pointers; zeros won't work on architectures where the null pointer constant is non-zero). I really want to write my data structure library in sepples, where templates allow type genericism without issue. What should I do, /prague/; what should I do?!

Name: Anonymous 2012-01-14 5:36

Generic programming in C
Back in my time, when men were men, we simply used void pointers and an enum that indicated the real type of the pointer if needed.

Name: Anonymous 2012-01-14 6:07

>>81
Back in my time, we used two unused lower bits of pointer to hold type.

Name: Anonymous 2012-01-14 6:27

>>81
sounds like an ugly hack

Name: Anonymous 2012-01-14 6:56

A generic linked list that will contain anything you could think of. No macros! It assumes struct node has the strictest alignment.

#include <stdlib.h>
#include <stddef.h>
#include <string.h>

// API
struct list;

struct list *make_list(size_t elem_size);
void *prepend(struct list *l);
void *append(struct list *l);
void *insert_after(struct list *l, void *node);
void *insert_before(struct list *l, void *node);
void *head(struct list *l);
void *tail(struct list *l);
void *next(void *node);
void *previous(void *node);

// implementation

struct list
{
    size_t elem_size;
    struct node head, tail;
};

struct node
{
    struct node *prev, *next;
};

struct list *make_list(size_t elem_size)
{
    struct list *l;

    l = malloc(sizeof *l);
    l->elem_size = elem_size;
    l->head.next = &l->tail;
    l->tail.prev = &l->head;
    return l;
}

static struct node *make_node(size_t elem_size)
{
    struct node *n;
    size_t size;

    size = sizeof *n + (elem_size + sizeof *n - 1) / sizeof *n;
    n = malloc(size);
    return n;
}

static void insert(struct node *i, struct node *p, struct node *n)
{
    p->next = i;
    i->prev = p;
    i->next = n;
    n->prev = i;
}


void *prepend(struct list *l)
{
    struct node *n;

    n = make_node(l->elem_size);
    insert(n, &l->head, l->head.next);
    return n + 1;
}

void *append(struct list *l)
{
    struct node *n;

    n = make_node(l->elem_size);
    insert(n, l->tail.prev, &l->tail);
    return n + 1;
}

void *insert_after(struct list *l, void *node)
{
    struct node *m, *n;

    m = (struct node *)node - 1;
    n = make_node(l->elem_size);
    insert(n, m, m->next);
    return n + 1;
}

void *insert_before(struct list *l, void *node)
{
    struct node *m, *n;

    m = (struct node *)node - 1;
    n = make_node(l->elem_size);
    insert(n, m->prev, m);
    return n + 1;
}

void *head(struct list *l)
{
    return &l->head + 1;
}

void *tail(struct list *l)
{
    return &l->tail + 1;
}

void *next(void *node)
{
    struct node *n;

    n = (struct node *)node - 1;
    return n->next + 1;
}

void *previous(void *node)
{
    struct node *n;

    n = (struct node *)node - 1;
    return n->prev + 1;
}

// example

struct person
{
    char name[60];
    int age;
};

void init_person(struct person *p, const char *name, int age)
{
    strcpy(p->name, name);
    p->age = age;
}

int main()
{
    struct list *l;
    struct person *p;

    l = make_list(sizeof *p);
    init_person(append(l), "Charlie", 8);
    init_person(append(l), "Bob", 42);
    init_person(append(l), "Niels", 9);
    init_person(append(l), "Jon", 11);
    init_person(append(l), "Becky", 10);
    init_person(append(l), "Ashley",9);

    for (p = next(head(l)); p != tail(l); p = next(p)) {
        printf("%s %d\n", p->name, p->age);
    }

    exit(EXIT_SUCCESS);
}

I wrote this on my phone, so I probably made some mistakes. I didn't bother to add remove() or error handling, but that should be straight-forward.

Name: Anonymous 2012-01-14 7:06

>>84
linked is shit

Name: Anonymous 2012-01-14 14:18

>>85
If it ain't linked, it's crap.

Name: Anonymous 2012-01-14 14:58

>>86
If it is linked, it's shit, unless it's a lock-free concurrent structure then it's acceptable.

Name: Anonymous 2012-01-14 15:01

>>85-87
Lists are shit.

Name: Anonymous 2012-01-14 15:01

>>14,15
This is how GNU does it afaik.

Name: Anonymous 2012-01-14 15:16

>>84
It assumes undefined behaviour
Enjoy your segfaults.

Name: Anonymous 2012-01-14 18:53

>>88
1) If it ain't Lisp, it's crap.
2) Lisp is shit.

Name: Anonymous 2012-01-14 19:00

http://chaos-pp.cvs.sourceforge.net/viewvc/chaos-pp/order-pp/example/array_ops.c?view=markup
You can do generic programming using macro libraries like Chaos, Order, and Boost Preprocessor.

Name: Anonymous 2012-01-14 20:59

#define GENERIC_TYPE Jew
#include <work_camp.h>
#define GENERIC_TYPE Nigger
#include <work_camp.h>
#define GENERIC_TYPE Faggot
#include <work_camp.h>
#define GENERIC_TYPE Communist
#include <work_camp.h>
enum WorkCamp { Auschwitz, Dachau };
int Goldberg(void) {
#define Mr_Goldberg_TYPE Jew
  Jew Mr_Goldberg = get_vitals(Jew, "Itzhak Goldberg");
  send_to_camp(Mr_Goldberg, Dachau);
  return Mr_Goldberg->serialNumber;
}

Name: Anonymous 2012-01-14 21:38

>>93
2/10

Name: Anonymous 2012-01-14 21:51

>>93
lol, nice

Name: Anonymous 2012-01-14 22:11

>>93
9.5/10

Name: Anonymous 2012-01-15 0:59

>>93
http://en.wikipedia.org/wiki/Whoopi_Goldberg
She adopted the traditionally German/Jewish surname Goldberg as a stage name because her mother felt the original surname of Johnson was not "Jewish enough" to make her a star.

She had a smart mother.

Name: Anonymous 2012-01-15 1:18

>>97
Goldberg made a sexual joke about President George W. Bush, by waving a bottle of wine, pointing toward her pubic area and saying: "We should keep Bush where he belongs, and not in the White House."

Name: Anonymous 2012-01-16 0:15

>>98
Awesome.

She is still annoying.

Name: DUBS LIBERATION FRONT 2012-03-24 18:30

NON-DUBS SHALL BE CLEANSED FROM THE EARTH!

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