...
This noncompliant code example statically allocates storage for a structure containing 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;
}
|
...
This compliant solution dynamically allocates storage for struct flexArrayStruct.
| 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;
}
|
...
This noncompliant code example attempts to copy an instance of a structure containing a flexible array member (struct flexArrayStruct) by assignment.
| Code Block | ||||
|---|---|---|---|---|
| ||||
struct flexArrayStruct *flexStructA; struct flexArrayStruct *flexStructB; size_t array_size; size_t i; /* Initialize array_size */ /* Allocate memory for flexStructA */ /* Allocate memory for flexStructB */ /* Initialize flexStructA */ /* ... */ *flexStructB = *flexStructA; |
...
This compliant solution uses memcpy() to properly copy the content of flexStructA into flexStructB.
| Code Block | ||||
|---|---|---|---|---|
| ||||
struct flexArrayStruct *flexStructA; struct flexArrayStruct *flexStructB; size_t array_size; size_t i; /* Initialize array_size */ /* Allocate memory for flexStructA */ /* Allocate memory for flexStructB */ /* Initialize flexStructA */ /* ... */ memcpy( flexStructB, flexStructA, (sizeof(struct flexArrayStruct) + sizeof(int) * array_size) ); |
...
In this noncompliant code example, the flexible array structure is passed directly to a function that tries to print the array elements.
| Code Block | ||||
|---|---|---|---|---|
| ||||
void print_array(struct flexArrayStruct structP) {
size_t i;
puts("Array is: ");
for (i = 0; i < structP.num; i++) {
printf("%d", structP.data[i]);
}
puts("\n");
}
struct flexArrayStruct *structP;
size_t array_size;
size_t i;
/* initialize array_size */
/* space is allocated for the struct */
structP = (struct flexArrayStruct *)malloc(
sizeof(struct flexArrayStruct) + sizeof(int) * array_size
);
if (structP == NULL) {
/* Handle malloc failure */
}
structP->num = array_size;
for (i = 0; i < array_size; i++) {
structP->data[i] = i;
}
print_array(*structP);
|
...
In this compliant solution, the print_array() function accepts a pointer to the structure rather than the structure itself.
| Code Block | ||||
|---|---|---|---|---|
| ||||
void print_array(struct flexArrayStruct *structP) {
size_t i;
puts("Array is: ");
for (i = 0; i < structP->num; i++) {
printf("%d", structP->data[i]);
}
puts("\n");
}
struct flexArrayStruct *structP;
size_t array_size;
size_t i;
/* initialize array_size */
/* space is allocated for the struct */
structP = (struct flexArrayStruct *)malloc(
sizeof(struct flexArrayStruct) + sizeof(int) * array_size
);
if (structP == NULL) {
/* Handle malloc failure */
}
structP->num = array_size;
for (i = 0; i < array_size; i++) {
structP->data[i] = i;
}
print_array(structP);
|
...