Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: add the GNU extension

...

Code Block
void addscalar(int n, int m, double a[n][n*m+300], double x);

int main(void) 
{
  double b[4][308];
  addscalar(4, 2, b, 2.17);
  return 0;
}

void addscalar(int n, int m, double a[n][n*m+300], double x) {
  for (int i = 0; i < n; i++)
  for (int j = 0, k = n*m+300; j < k; j++)
    /* a is a pointer to a VLA with n*m+300 elements */
    a[i][j] += x;
}

...

Code Block
bgColor#ffcccc
langc
void my_memset(char* p, size_t n, char v) 
{
  memset( p, v, n);
}

Noncompliant Code Example

...

Code Block
bgColor#ffcccc
langc
void my_memset(char p[n], size_t n, char v) 
{
  memset( p, v, n);
}

Compliant Solution (GNU extension)

This compliant solution declares uses a GNU extension to forward declare the size_t variable n before using it in the subsequent array declaration. Consequently, this code allows the existing parameter order to be retained and successfully documents the relationship between the array parameter and the size parameter.

Code Block
bgColor#ccccff
langc
void my_memset(size_t n; char p[n], size_t n, char v)
{
  memset(p, v, n);
}

Compliant Solution (breaking change to API)

This compliant solution changes the function's API by moving the size_t variable n to before the subsequent array declaration. Consequently, this code complies with the standard and successfully documents the relationship between the array parameter and the size parameter, but requires all callers to be updated.

Code Block
bgColor#ccccff
langc
void my_memset(size_t n, char p[n], char v) 
{
  memset(p, v, n);
}

Exceptions

...