Please, help us to better know about our user community by answering the following short survey:
HDF5  1.8.23
C-API Reference


Creating "Large" HDF5 Attributes

You would like to use HDF5 attributes the size of whose values exceeds a few dozen kilobytes
A file format change in the HDF5 1.8.x family of library releases made it possible to have attributes larger than about 64 KiB. Ensure that the lower library version bound for new HDF5 item creation is at least 1.8.x, and create larger attributes as usual.
In the example below, we create an attribute whose value occupies 4 MiB.
This feature is only supported in HDF5 1.8.x+
15 {
16 __label__ fail_attr, fail_aspace, fail_fapl, fail_file;
17 hid_t fapl, file, aspace, attr;
19 if ((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) {
20 ret_val = EXIT_FAILURE;
21 goto fail_fapl;
22 }
23#if H5_VERSION_GE(1, 8, 0)
26#error Only HDF5 1.8.x and later supported.
28 ret_val = EXIT_FAILURE;
29 goto fail_file;
30 }
31 if ((file = H5Fcreate("large_attribute.h5", H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) {
32 ret_val = EXIT_FAILURE;
33 goto fail_file;
34 }
36 if ((aspace = H5Screate_simple(1, (hsize_t[]){1024 * 1024}, NULL)) < 0) {
37 ret_val = EXIT_FAILURE;
38 goto fail_aspace;
39 }
40 if ((attr = H5Acreate(file, "4MiB", H5T_IEEE_F32LE, aspace, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
41 ret_val = EXIT_FAILURE;
42 goto fail_attr;
43 }
45 H5Aclose(attr);
47 H5Sclose(aspace);
49 H5Fclose(file);
51 H5Pclose(fapl);
53 }
Definition: H5Fpublic.h:195
#define H5F_ACC_TRUNC
Definition: H5Fpublic.h:90
int hid_t
Definition: H5Ipublic.h:60
Definition: H5Ppublic.h:215
#define H5P_DEFAULT
Definition: H5Ppublic.h:255
unsigned long long hsize_t
Definition: H5public.h:334
herr_t H5Pset_libver_bounds(hid_t plist_id, H5F_libver_t low, H5F_libver_t high)
Controls the range of library release versions used when creating objects in a file.
herr_t H5Pclose(hid_t plist_id)
Terminates access to a property list.
hid_t H5Pcreate(hid_t cls_id)
Creates a new property list as an instance of a property list class.
#define H5Acreate
Definition: H5version.h:156
herr_t H5Aclose(hid_t attr_id)
Closes the specified attribute.
herr_t H5Fclose(hid_t file_id)
Terminates access to an HDF5 file.
hid_t H5Fcreate(const char *filename, unsigned flags, hid_t fcpl_id, hid_t fapl_id)
Creates an HDF5 file.
herr_t H5Sclose(hid_t space_id)
Releases and terminates access to a dataspace.
hid_t H5Screate_simple(int rank, const hsize_t dims[], const hsize_t maxdims[])
Creates a new simple dataspace and opens it for access.
#define H5T_IEEE_F32LE
Definition: H5Tpublic.h:413
Large attributes are supported only in HDF5 versions 1.8.x and higher. This has implications for the accessibility of your HDF5 files and is your call.
Since there are no size limitations for large attributes, it might seem tempting to mistake them for dataset stand-ins. This is not the case, for at least two reasons:
  1. Attributes decorate HDF5 objects, have their own local namespace, and can't be link targets.
  2. Attribute I/O treats the attribute value as atomic, i.e., there is no support for partial I/O. A large attribute will always be read or written in its entirety.
See Also
See Maintaining Compatibility with other HDF5 Library Versions for HDF5 compatibility implications.