diff --git a/examples/main.c b/examples/main.c
index 07e1360ce6e73639f062e3b70fd3d2621ba9e243..0fce7500af2e3d4fe98feb7e4d3f484658bd2ded 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -414,7 +414,7 @@ int main(int argc, char *argv[]) {
     message("space %s periodic.", s.periodic ? "is" : "isn't");
     message("highest-level cell dimensions are [ %i %i %i ].", s.cdim[0],
             s.cdim[1], s.cdim[2]);
-    message("%i parts in %i cells.", s.nr_parts, s.tot_cells);
+    message("%zi parts in %i cells.", s.nr_parts, s.tot_cells);
     message("maximum depth is %d.", s.maxdepth);
     // message( "cutoffs in [ %g %g ]." , s.h_min , s.h_max ); fflush(stdout);
   }
diff --git a/src/debug.c b/src/debug.c
index 716fa66934f988196b9ebfa8f037c4c8c28d92d2..48a0995a5d2992c2b23c7294833309508ee5b59d 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -51,7 +51,7 @@
  */
 
 void printParticle(struct part *parts, struct xpart *xparts, long long int id,
-                   int N) {
+                   size_t N) {
 
   int found = 0;
 
@@ -67,7 +67,7 @@ void printParticle(struct part *parts, struct xpart *xparts, long long int id,
   if (!found) printf("## Particles[???] id=%lld not found\n", id);
 }
 
-void printgParticle(struct gpart *parts, long long int id, int N) {
+void printgParticle(struct gpart *parts, long long int id, size_t N) {
 
   int found = 0;
 
diff --git a/src/debug.h b/src/debug.h
index 3e9fcb6c50603e03ea675176bb0d1a0c02cd12bf..fba5cb68680472715025f9fee39a5bf06abacf81 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -23,9 +23,9 @@
 #include "cell.h"
 #include "part.h"
 
-void printParticle(struct part *parts, struct xpart *xparts, long long int i,
-                   int N);
-void printgParticle(struct gpart *parts, long long int i, int N);
+void printParticle(struct part *parts, struct xpart *xparts, long long int id,
+                   size_t N);
+void printgParticle(struct gpart *parts, long long int id, size_t N);
 void printParticle_single(struct part *p, struct xpart *xp);
 
 #ifdef HAVE_METIS
diff --git a/src/engine.c b/src/engine.c
index 23884f821b49d7c2f0ec48ee870ea7ff8d88b021..0739609170eceeeae08f1523330e1d7436688a0b 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -153,7 +153,8 @@ void engine_redistribute(struct engine *e) {
   /* Start by sorting the particles according to their nodes and
      getting the counts. The counts array is indexed as
      count[from * nr_nodes + to]. */
-  int *counts, *dest;
+  int *counts;
+  size_t *dest;
   double ih[3], dim[3];
   ih[0] = s->ih[0];
   ih[1] = s->ih[1];
@@ -162,7 +163,7 @@ void engine_redistribute(struct engine *e) {
   dim[1] = s->dim[1];
   dim[2] = s->dim[2];
   if ((counts = (int *)malloc(sizeof(int) *nr_nodes *nr_nodes)) == NULL ||
-      (dest = (int *)malloc(sizeof(int) * s->nr_parts)) == NULL)
+      (dest = (size_t *)malloc(sizeof(size_t) * s->nr_parts)) == NULL)
     error("Failed to allocate count and dest buffers.");
   bzero(counts, sizeof(int) * nr_nodes * nr_nodes);
   struct part *parts = s->parts;
@@ -594,7 +595,7 @@ void engine_exchange_cells(struct engine *e) {
  * @return The number of arrived parts copied to parts and xparts.
  */
 
-int engine_exchange_strays(struct engine *e, int offset, int *ind, int N) {
+int engine_exchange_strays(struct engine *e, int offset, size_t *ind, size_t N) {
 
 #ifdef WITH_MPI
 
@@ -646,9 +647,9 @@ int engine_exchange_strays(struct engine *e, int offset, int *ind, int N) {
 
   /* Count the total number of incoming particles and make sure we have
      enough space to accommodate them. */
-  int count_in = 0;
+  size_t count_in = 0;
   for (int k = 0; k < e->nr_proxies; k++) count_in += e->proxies[k].nr_parts_in;
-  if (e->verbose) message("sent out %i particles, got %i back.", N, count_in);
+  if (e->verbose) message("sent out %zi particles, got %zi back.", N, count_in);
   if (offset + count_in > s->size_parts) {
     s->size_parts = (offset + count_in) * 1.05;
     struct part *parts_new = NULL;
@@ -1174,7 +1175,7 @@ void engine_print_task_counts(struct engine *e) {
     printf(" %s=%i", taskID_names[k], counts[k]);
   printf(" skipped=%i ]\n", counts[task_type_count]);
   fflush(stdout);
-  message("nr_parts = %i.", e->s->nr_parts);
+  message("nr_parts = %zi.", e->s->nr_parts);
 }
 
 /**
@@ -1773,8 +1774,8 @@ void engine_split(struct engine *e, struct partition *initial_partition) {
 
   /* Re-allocate the local parts. */
   if (e->nodeID == 0)
-    message("Re-allocating parts array from %i to %i.", s->size_parts,
-            (int)(s->nr_parts * 1.2));
+    message("Re-allocating parts array from %zi to %zi.", s->size_parts,
+            (size_t)(s->nr_parts * 1.2));
   s->size_parts = s->nr_parts * 1.2;
   struct part *parts_new = NULL;
   struct xpart *xparts_new = NULL;
diff --git a/src/engine.h b/src/engine.h
index faf48fc09197036ad968a565138237e8d3bcd459..741ae1f553494e435394f529606b4cb794b0e3d2 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -182,7 +182,7 @@ void engine_init_particles(struct engine *e);
 void engine_step(struct engine *e);
 void engine_maketasks(struct engine *e);
 void engine_split(struct engine *e, struct partition *initial_partition);
-int engine_exchange_strays(struct engine *e, int offset, int *ind, int N);
+int engine_exchange_strays(struct engine *e, int offset, size_t *ind, size_t N);
 void engine_rebuild(struct engine *e);
 void engine_repartition(struct engine *e);
 void engine_makeproxies(struct engine *e);
diff --git a/src/space.c b/src/space.c
index cde551ef0a1cf6de969c2712fc8df302141c3d87..5938101d94b17097277d7fc06a1cd4d3c1181727 100644
--- a/src/space.c
+++ b/src/space.c
@@ -311,7 +311,7 @@ void space_rebuild(struct space *s, double cell_max, int verbose) {
   int j, k, cdim[3], nr_parts = s->nr_parts, nr_gparts = s->nr_gparts;
   struct cell *restrict c, *restrict cells;
   struct part *restrict p;
-  int *ind;
+  size_t *ind;
   double ih[3], dim[3];
   ticks tic = getticks();
 
@@ -324,8 +324,8 @@ void space_rebuild(struct space *s, double cell_max, int verbose) {
 
   /* Run through the particles and get their cell index. */
   // tic = getticks();
-  const int ind_size = s->size_parts;
-  if ((ind = (int *)malloc(sizeof(int) * ind_size)) == NULL)
+  const size_t ind_size = s->size_parts;
+  if ((ind = (size_t *)malloc(sizeof(size_t) * ind_size)) == NULL)
     error("Failed to allocate temporary particle indices.");
   ih[0] = s->ih[0];
   ih[1] = s->ih[1];
@@ -376,10 +376,10 @@ void space_rebuild(struct space *s, double cell_max, int verbose) {
 
   /* Re-allocate the index array if needed.. */
   if (s->nr_parts > ind_size) {
-    int *ind_new;
-    if ((ind_new = (int *)malloc(sizeof(int) * s->nr_parts)) == NULL)
+    size_t *ind_new;
+    if ((ind_new = (size_t *)malloc(sizeof(size_t) * s->nr_parts)) == NULL)
       error("Failed to allocate temporary particle indices.");
-    memcpy(ind_new, ind, sizeof(int) * nr_parts);
+    memcpy(ind_new, ind, sizeof(size_t) * nr_parts);
     free(ind);
     ind = ind_new;
   }
@@ -419,7 +419,7 @@ void space_rebuild(struct space *s, double cell_max, int verbose) {
 
   /* Run through the gravity particles and get their cell index. */
   // tic = getticks();
-  if ((ind = (int *)malloc(sizeof(int) * s->size_gparts)) == NULL)
+  if ((ind = (size_t *)malloc(sizeof(size_t) * s->size_gparts)) == NULL)
     error("Failed to allocate temporary particle indices.");
   for (k = 0; k < nr_gparts; k++) {
     struct gpart *gp = &s->gparts[k];
@@ -506,7 +506,7 @@ void space_split(struct space *s, struct cell *cells, int verbose) {
  * @param verbose Are we talkative ?
  */
 
-void space_parts_sort(struct space *s, int *ind, int N, int min, int max,
+void space_parts_sort(struct space *s, size_t *ind, size_t N, int min, int max,
                       int verbose) {
 
   ticks tic = getticks();
@@ -554,7 +554,7 @@ void space_parts_sort(struct space *s, int *ind, int N, int min, int max,
 void space_do_parts_sort() {
 
   /* Pointers to the sorting data. */
-  int *ind = space_sort_struct.ind;
+  size_t *ind = space_sort_struct.ind;
   struct part *parts = space_sort_struct.parts;
   struct xpart *xparts = space_sort_struct.xparts;
 
@@ -570,8 +570,8 @@ void space_do_parts_sort() {
       if (!space_sort_struct.waiting) return;
 
     /* Get the stack entry. */
-    int i = space_sort_struct.stack[qid].i;
-    int j = space_sort_struct.stack[qid].j;
+    ptrdiff_t i = space_sort_struct.stack[qid].i;
+    ptrdiff_t j = space_sort_struct.stack[qid].j;
     int min = space_sort_struct.stack[qid].min;
     int max = space_sort_struct.stack[qid].max;
     space_sort_struct.stack[qid].ready = 0;
@@ -585,13 +585,13 @@ void space_do_parts_sort() {
               i, j, min, max, pivot); */
 
       /* One pass of QuickSort's partitioning. */
-      int ii = i;
-      int jj = j;
+      ptrdiff_t ii = i;
+      ptrdiff_t jj = j;
       while (ii < jj) {
         while (ii <= j && ind[ii] <= pivot) ii++;
         while (jj >= i && ind[jj] > pivot) jj--;
         if (ii < jj) {
-          int temp_i = ind[ii];
+          size_t temp_i = ind[ii];
           ind[ii] = ind[jj];
           ind[jj] = temp_i;
           struct part temp_p = parts[ii];
@@ -676,11 +676,12 @@ void space_do_parts_sort() {
   } /* main loop. */
 }
 
-void space_gparts_sort(struct gpart *gparts, int *ind, int N, int min,
+void space_gparts_sort(struct gpart *gparts, size_t *ind, size_t N, int min,
                        int max) {
 
   struct qstack {
-    volatile int i, j, min, max;
+    volatile size_t i, j;
+    volatile int min, max;
     volatile int ready;
   };
   struct qstack *qstack;
@@ -688,7 +689,8 @@ void space_gparts_sort(struct gpart *gparts, int *ind, int N, int min,
   volatile unsigned int first, last, waiting;
 
   int pivot;
-  int i, ii, j, jj, temp_i, qid;
+  ptrdiff_t i, ii, j, jj, temp_i;
+  int qid;
   struct gpart temp_p;
 
   /* for ( int k = 0 ; k < N ; k++ )
@@ -1186,7 +1188,7 @@ struct cell *space_getcell(struct space *s) {
  * recursively.
  */
 
-void space_init(struct space *s, double dim[3], struct part *parts, int N,
+void space_init(struct space *s, double dim[3], struct part *parts, size_t N,
                 int periodic, double h_max, int verbose) {
 
   /* Store everything in the space. */
diff --git a/src/space.h b/src/space.h
index 8069721a71b9f47664290b6e34eef726efdb47c8..87b618b141b7871aeb5ff76eae5bdc24f063bdef 100644
--- a/src/space.h
+++ b/src/space.h
@@ -20,6 +20,9 @@
 #define SWIFT_SPACE_H
 
 /* Includes. */
+#include <stddef.h>
+
+/* Local includes. */
 #include "cell.h"
 #include "part.h"
 
@@ -85,8 +88,8 @@ struct space {
   struct gpart *gparts;
 
   /* The total number of parts in the space. */
-  int nr_parts, size_parts;
-  int nr_gparts, size_gparts;
+  size_t nr_parts, size_parts;
+  size_t nr_gparts, size_gparts;
 
   /* Is the space periodic? */
   int periodic;
@@ -102,18 +105,19 @@ struct space {
 
   /* Buffers for parts that we will receive from foreign cells. */
   struct part *parts_foreign;
-  int nr_parts_foreign, size_parts_foreign;
+  size_t nr_parts_foreign, size_parts_foreign;
 };
 
 /* Interval stack necessary for parallel particle sorting. */
 struct qstack {
-  volatile int i, j, min, max;
+  volatile ptrdiff_t i, j;
+  volatile int min, max;
   volatile int ready;
 };
 struct parallel_sort {
   struct part *parts;
   struct xpart *xparts;
-  int *ind;
+  size_t *ind;
   struct qstack *stack;
   unsigned int stack_size;
   volatile unsigned int first, last, waiting;
@@ -121,13 +125,13 @@ struct parallel_sort {
 extern struct parallel_sort space_sort_struct;
 
 /* function prototypes. */
-void space_parts_sort(struct space *s, int *ind, int N, int min, int max,
+void space_parts_sort(struct space *s, size_t *ind, size_t N, int min, int max,
                       int verbose);
-void space_gparts_sort(struct gpart *gparts, int *ind, int N, int min, int max);
+void space_gparts_sort(struct gpart *gparts, size_t *ind, size_t N, int min, int max);
 struct cell *space_getcell(struct space *s);
 int space_getsid(struct space *s, struct cell **ci, struct cell **cj,
                  double *shift);
-void space_init(struct space *s, double dim[3], struct part *parts, int N,
+void space_init(struct space *s, double dim[3], struct part *parts, size_t N,
                 int periodic, double h_max, int verbose);
 void space_map_cells_pre(struct space *s, int full,
                          void (*fun)(struct cell *c, void *data), void *data);