Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
 
struct flex_array_struct {
  size_t num;
  int data[];
};
 
void func(void) {
  struct flex_array_struct *flex_struct;
  size_t array_size = 4;

  /* Dynamically allocate memory for the struct */
  flex_struct = (struct flex_array_struct *)malloc(
    sizeof(struct flex_array_struct)
    + sizeof(int) * array_size);
  if (flex_sruct == NULL) {
    /* Handle error */
  }

  /* Initialize structure */
  flex_struct->num = array_size;

  for (size_t i = 0; i < array_size; ++i) {
    flex_struct->data[i] = 0;
  }
}

...

Code Block
bgColor#ccccff
langc
#include <stdio.h>
#include <stdlib.h>
 
struct flex_array_struct {
  size_t num;
  int data[];
};
 
void print_array(struct flex_array_struct *struct_p) {
  puts("Array is: ");
  for (size_t i = 0; i < struct_p->num; ++i) {
    printf("%d ", struct_p->data[i]);
  }
  putchar('\n');
}
 
void func(void) {
  struct flex_array_struct *struct_p;
  size_t array_size = 4;

  /* Space is allocated for the struct */
  struct_P = (struct flex_array_struct *)malloc(
    sizeof(struct flex_array_struct)
    + sizeof(int) * array_size);
  if (struct_p == NULL) {
    /* Handle error */
  }
  struct_p->num = array_size;

  for (size_t i = 0; i < array_size; ++i) {
    struct_p->data[i] = i;
  }and initialized... */

  print_array(struct_p);
}

Risk Assessment

...