diff --git a/src/engine.c b/src/engine.c
index 5bbc0711658b94ee8f87acd22a05a6138ce3876c..5655e4612d8a9dfdaf26ec921927d33b9ac3349f 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -3573,16 +3573,18 @@ void engine_config(int restart, int fof, struct engine *e,
   if (nr_cores > CPU_SETSIZE) /* Unlikely, except on e.g. SGI UV. */
     error("must allocate dynamic cpu_set_t (too many cores per node)");
 
-  char *buf = (char *)malloc((nr_cores + 1) * sizeof(char));
-  buf[nr_cores] = '\0';
-  for (int j = 0; j < nr_cores; ++j) {
-    /* Reversed bit order from convention, but same as e.g. Intel MPI's
-     * I_MPI_PIN_DOMAIN explicit mask: left-to-right, LSB-to-MSB. */
-    buf[j] = CPU_ISSET(j, entry_affinity) ? '1' : '0';
+  if (verbose && with_aff) {
+    char *buf = (char *)malloc((nr_cores + 1) * sizeof(char));
+    buf[nr_cores] = '\0';
+    for (int j = 0; j < nr_cores; ++j) {
+      /* Reversed bit order from convention, but same as e.g. Intel MPI's
+       * I_MPI_PIN_DOMAIN explicit mask: left-to-right, LSB-to-MSB. */
+      buf[j] = CPU_ISSET(j, entry_affinity) ? '1' : '0';
+    }
+    message("Affinity at entry: %s", buf);
+    free(buf);
   }
 
-  if (verbose && with_aff) message("Affinity at entry: %s", buf);
-
   int *cpuid = NULL;
   cpu_set_t cpuset;
 
@@ -4176,7 +4178,6 @@ void engine_config(int restart, int fof, struct engine *e,
   if (with_aff) {
     free(cpuid);
   }
-  free(buf);
 #endif
 
   /* Wait for the runner threads to be in place. */