 
                            Wiki Markup 
The following is an example of a structure that contains a flexible array member:
...
| Code Block | ||||
|---|---|---|---|---|
| 
 | ||||
| 
struct flexArrayStruct flexStruct;
size_t array_size;
size_t i;
/* Initialize array_size */
/* Initialize structure */
flexStruct.num = 0;
for (i = 0; i < array_size; i++) {
  flexStruct.data[i] = 0;
}
 | 
Wiki Markup flexArrayStruct}}  does   not   actually   reserve   space   for   the   integer   array   data;   it   can't   because   the   size   hasn't   been   specified.   Consequently,   while   initializing   the  {{num}}  member   to   zero   is   allowed,   attempting   to   write   even   one   value   into   data   (that   is,  {{data\[0\]}})   is   likely   to   overwrite   memory   outside   of   the   object's   bounds.
Compliant Solution (Storage Allocation)
...
| Code Block | ||||
|---|---|---|---|---|
| 
 | ||||
| 
struct flexArrayStruct *flexStruct;
size_t array_size;
size_t i;
/* Initialize array_size */
/* Dynamically allocate memory for the structure */
flexStruct = (struct flexArrayStruct *)malloc(
  sizeof(struct flexArrayStruct) + sizeof(int) * array_size
);
if (flexStruct == NULL) {
  /* Handle malloc failure */
}
/* Initialize structure */
flexStruct->num = 0;
for (i = 0; i < array_size; i++) {
  flexStruct->data[i] = 0;
}
 | 
...
The  {{data\[\]}}  member   of  {{flexStruct}}  can   now   be   accessed   as   described   in   C99,   Section   6.7.2.1,   paragraph   16.
Noncompliant Code Example (Copying)
...
Bibliography
Wiki Markup 
...
MEM32-C. Detect and handle memory allocation errors 08. Memory Management (MEM)