NO

Author Topic: [FIXED] Bitfields initialization  (Read 4312 times)

iZzz32

  • Guest
[FIXED] Bitfields initialization
« on: October 12, 2011, 10:37:56 PM »
Code: [Select]
typedef struct Foo
{
  bool              foo:1;
  bool              bar:1;
  bool              baz:1;
  unsigned          qux;
 
} Foo;

Foo f = { true, .bar = 1, 1, 12345 };

The code above does not initialize .bar and .baz to 1, however GCC behaves as expected. Is this a bug?  ;)

(Pelles C 6.50 RC 4, pocc.exe is 6.50.35.0), 7.00 RC 1 too
« Last Edit: June 22, 2012, 02:20:53 PM by iZzz32 »

CommonTater

  • Guest
Re: [BUG?] Bitfields initialization
« Reply #1 on: October 13, 2011, 03:12:59 AM »
http://tigcc.ticalc.org/doc/gnuexts.html#SEC82

I think you will find that the .bar=1 syntax is not standard C, but is infact a GCC specific extension to the C language.



Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: [BUG?] Bitfields initialization
« Reply #2 on: October 13, 2011, 08:20:03 AM »
http://tigcc.ticalc.org/doc/gnuexts.html#SEC82

I think you will find that the .bar=1 syntax is not standard C, but is infact a GCC specific extension to the C language.
I am not so sure, as the reference you posted says it's standard in C99 but GCC allows it as an extensio to C89.

Pelles C however claims to be C99 compliant...   ???

Ralf

iZzz32

  • Guest
Re: [BUG?] Bitfields initialization
« Reply #3 on: October 13, 2011, 12:20:13 PM »
Quote
I think you will find that the .bar=1 syntax is not standard C
This syntax is standard C99 (ISO/IEC 9899:1999, 6.7.8, paragraph 7) and Pelles C supports it (according to the reference in the help file).

But it does not matter, because Foo f = { 1, 1, 1, 12345 }; does not work either.

iZzz32

  • Guest
Re: [BUG?] Bitfields initialization
« Reply #4 on: May 28, 2012, 04:10:37 PM »
Still present in RC3. Is it an error to initialize bit-fields like this? Then why does it work when I change bool to unsigned?
Code: [Select]
#include <stdbool.h>
#include <assert.h>
 
typedef struct Foo
{
  bool              a:1;
  bool              b:1;
} Foo;
 
Foo f = { true, true };
 
int main(void)
{
  assert(f.a && f.b);
  return 0;
}

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: [BUG?] Bitfields initialization
« Reply #5 on: June 03, 2012, 05:15:50 PM »
It looks like a bug, but I have to check. I think the problem is that _Bool is full byte, but you are not allowed to use more than one bit (so you can't pack several bits into a _Bool). At some point I think there was some cunfusion if, for example, "_Bool x:3" was allowed, and if so, what it meant...
/Pelle