Better HN
topnewbestaskshowjobs
Log inSign up

mklencke

6 karma · joined May 14, 2011 (15y ago) · 6 submissions

Submissions

comment 11y ago · link · parent

In some cases, 2x or 4x is worth the risk. Imagine having a warehouse-scale computer where you can use 50,000 CPUs instead of 200,000.

2.

Open Access needs Open Education and Vice-Versa(malariaworld.org ↗)

1 points · by mklencke · 15y ago · discuss

comment 15y ago · link · parent

I'm just making it up as I go along, it's not the prettiest thing. What would you do, in C?

comment 15y ago · link · parent

True, but that can be overcome by another function that just gives you an address to put a new item. You would say, for example:

  *(int *)vector_expand(v) = 2;

comment 15y ago · link · parent

In this case, I'm not storing void pointers in the data structure, but I'm using the void pointer as a direct address into "elements". The memory layout is just a sequence of items of length elem_size. vector_push copies the item into that memory (and doubles the size of "elements" with realloc when necessary).

comment 15y ago · link · parent

The RAII benefits are definitely there and STL containers are pretty awesome, but you can still create generic containers in C by using void pointers. For example, a simple vector API:

  typedef struct { void *elements; int elem_size; int n; } Vector;

  Vector *vector_new(int elem_size);
  void vector_free(Vector *v);
  void vector_push(Vector *v, void *elem);
  void vector_pop(Vector *v);
  int vector_size(Vector *v);
  void *vector_at(Vector *v, int i);
etc. This is off the top of my head, but you get the idea. You can basically put anything you want in there as long as you say how large the elements are upon initialization.

There is a good book on generic C programming: http://sites.google.com/site/cinterfacesimplementations/