diff --git a/src/stream_cdf_i.c b/src/stream_cdf_i.c
index 7cbecd140855d4dde0d92e5888ecc9a57ff1c954..b599c5f49aef56faed5d91d43a1a8e074654ead2 100644
--- a/src/stream_cdf_i.c
+++ b/src/stream_cdf_i.c
@@ -1155,7 +1155,7 @@ set_vars_chunks(int ncid, int ncvarid, int nvdims, ncvar_t *ncvar)
   ncvar->hasFilter = cdf_get_var_filter(ncid, ncvarid, ncvar->filterSpec, CDI_MAX_NAME);
   // if (ncvar->hasFilter) printf("filterSpec: %s=%s\n", ncvar->name, ncvar->filterSpec);
 
-  size_t chunks[nvdims];
+  size_t* chunks = malloc(sizeof(size_t) * nvdims);
   int storageIn;
   if (nc_inq_var_chunking(ncid, ncvarid, &storageIn, chunks) == NC_NOERR)
     {
@@ -1171,6 +1171,7 @@ set_vars_chunks(int ncid, int ncvarid, int nvdims, ncvar_t *ncvar)
             }
         }
     }
+  free (chunks);
 
   size_t size;
   size_t nelems;