Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pcci-cc: handling of array type of unknown size #105

Open
tstreiff opened this issue Jul 6, 2020 · 0 comments
Open

pcci-cc: handling of array type of unknown size #105

tstreiff opened this issue Jul 6, 2020 · 0 comments
Labels
bug C frontend Issues related to the C language compiler

Comments

@tstreiff
Copy link
Contributor

tstreiff commented Jul 6, 2020

A subtle one...

#include <stdio.h>
#include <assert.h>

typedef int A[];      // array of unknow size (incomplete type)

A a1 = { 1, 2, 3, };  // initialized array of 3 ints
A a2 = { 1, 2, };     // initialized array of 2 ints
A a3;                 // illegal: cannot determine size

int main() {
  assert((sizeof(a1) / sizeof(int)) == 3);
  assert(a1[0] == 1);
  assert(a1[1] == 2);
  assert(a1[2] == 3);

  assert((sizeof(a2) / sizeof(int)) == 2);
  assert(a2[0] == 1);
  assert(a2[1] == 2);

  printf("sizeof(a3) = %d\n", sizeof(a3));

  return 0;
}
  • a1: is correctly allocated and initialized.

  • a2: the assert on a2 size fails: a2 contains 3 ints instead of 2.

  • a3: surprinsingly, no error is reported and a3 contains also 3 ints.

When a1 is processed, its type is formed from the incomplete type A, completed with the size 3, deduced from its initializer.
But this seems to update A type itself, so that all further declarations using A are impacted.
Type A becomes a 3 ints array type.

This would explain why a2 contains 3 ints (instead of 2) and declaration of a3 is accepted (and it also contains 3 ints)

There is a similar example in the C standard in the "Initialization" section, that explains the expected behaviour:

EXAMPLE 7 One form of initialization that completes array types involves typedef names. Given the
declaration
typedef int A[]; // OK - declared with block scope
the declaration
A a = { 1, 2 }, b = { 3, 4, 5 };
is identical to
int a[] = { 1, 2 }, b[] = { 3, 4, 5 };
due to the rules for incomplete types.

tstreiff added a commit to tstreiff/ppci-mirror that referenced this issue Jul 7, 2020
…rray type size when it is completed by an initializer for a variable. Ensure that an array type is complete at the end of an array variable declaration.
tstreiff added a commit to tstreiff/ppci-mirror that referenced this issue Jul 7, 2020
@windelbouwman windelbouwman added bug C frontend Issues related to the C language compiler labels Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug C frontend Issues related to the C language compiler
Projects
None yet
Development

No branches or pull requests

2 participants