title: Understanding HDF5's slabs
body: <p>First of all, forgive my very basic question: I'm going through the tutorials and I've successfully written my first HDF5 dataset. Now, I'm moving to slabs, but I'm finding difficulties in doing this.</p>
  
  <p>As far as I understand, I need to obtain a valid memory space, select a slab, and then write my data. But obviously I'm doing something wrong, since I get errors:</p>
  
  <pre><code>HDF5-DIAG: Error detected in HDF5 (1.8.14) thread 0:
    #000: H5Dio.c line 271 in H5Dwrite(): can't prepare for writing data
      major: Dataset
      minor: Write failed
    #001: H5Dio.c line 352 in H5D__pre_write(): can't write data
      major: Dataset
      minor: Write failed
    #002: H5Dio.c line 690 in H5D__write(): src and dest data spaces have different sizes
      major: Invalid arguments to routine
      minor: Bad value
  </code></pre>
  
  <p>Obviously I'm trying to write data with wrong dimensions, but I don't know how to correct that. My goal is to create a 20x3 matrix, and set the <em>second</em> row to <code>{ 10, 20, 30 }</code>:</p>
  
  <pre><code> 0  0  0
  10 20 30
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
   0  0  0
  </code></pre>
  
  <p>Can you help me in understanding this issue? My buggy code is below!</p>
  
  <p>Thanks!</p>
  
  <pre><code>#include "hdf5.h"
  
  #define FILE "dset.h5"
  #define DSET "/dset"
  
  int main() {
  
    hid_t        file_id, dataset_id, dataspace_id, filespace, memspace;
    hsize_t      dims[2], offset[2], count[2];
    herr_t       status;
  
    unsigned int dset_data[3] = { 10, 20, 30 };
  
    /* Create a new file using default properties. */
    file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  
    /* Create the data space for the dataset. */
    dims[0] = 20; /* ROWS */
    dims[1] = 3;    /* COLS */
    dataspace_id = H5Screate_simple(2, dims, NULL);
  
    /* Create the dataset. */
    dataset_id = H5Dcreate2(file_id, DSET, H5T_STD_I32BE, dataspace_id,
                            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  
    /* Get the memory space */
    memspace = H5Dget_space (dataset_id);
  
    offset[0] = 1; /* ROWS */
    offset[1] = 0; /* COLS */
  
    count[0]  = 1; /* ROWS */
    count[1]  = 3; /* COLS */
  
    /* Select the slab */
    status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET,
                                 offset, NULL, count, NULL);
  
    /* Write the dataset. */
    status = H5Dwrite(dataset_id, H5T_NATIVE_INT,
                      memspace, H5S_ALL, H5P_DEFAULT,
                      dset_data);
  
    /* End access to the dataset and release resources used by it. */
    status = H5Dclose(dataset_id);
  
    /* Terminate access to the data space. */
    status = H5Sclose(dataspace_id);
  
    /* Close the file. */
    status = H5Fclose(file_id);
  } 
  </code></pre>