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

Common Lisp in C

Name: Anonymous 2012-10-03 17:23

Lets write Common Lisp functions in generic C code.


/* (map #'func array) => map(array,length_of_array,sizeof(array),function_pointer) */
void *map(void *base,size_t len,size_t elemnsize, void (*func)(void *,void *)) {
  void *b = malloc(elemsize*len);
  size_t i;
  for(i=0;i<len;++i){
    func(b+(elemsize*1),base);
    base += elemsize;
  }
  return b;
}

Name: Anonymous 2012-10-04 12:54

>>17
Or you can just write a destructive version of map and stop being autistic .
What if you need another array? I'm all for just writing a god damned for loop anyway.

I highly doubt a single map function would act as an API hidden from the 'user' to begin with.
Normally it would be bundled with other functional stuff, seems like OP is trying to implement some. And obviously it wouldn't be hidden, it would be part of the API.

In the end your issue come down to a codemonkey that didnt read the api docs or source code to realize he needs to free the reault.
This isn't the issue, people who know you have to use free with malloc sometimes forget to use free because humans make mistakes also when large bodies of code create something complex it might be difficult to trace where you should free anyway, it's just a good principle to never let pointers to things you have to clean up escape from your control.

Look, it's pretty clear, one implementation of this function can fail through use of malloc and easily leads to errors, it's also less flexible in terms of control for the end user and it has more code, whilst the other can't fail and the user controls the allocation and has less code, as I said, the advantages are obvious.

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