Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2004

Tip: Looking for answers? Try searching our database.

pragma pack and bitfields

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Giovanni Bajo - 24 Sep 2004 17:50 GMT
Hello,

with Visual Studio .NET 2003:

__declspec(align(1))
struct X {
  unsigned int x : 3;
  unsigned int y : 13;
};

sizeof(X) is 4, even with /Zp1 and/or pragma pack(1). I would expect it to
be 2.

Is there any way I can get the behaviour I need? It looks like I'm forced to
use short* and manually extract the bits. FYI, this works with GCC as
expected.

Thanks,
Giovanni Bajo
David Lowndes - 24 Sep 2004 21:07 GMT
>with Visual Studio .NET 2003:
>
[quoted text clipped - 6 lines]
>sizeof(X) is 4, even with /Zp1 and/or pragma pack(1). I would expect it to
>be 2.

unsigned int is 4 bytes.

>Is there any way I can get the behaviour I need?

use unsigned short for the structure member type.

Dave
Signature

MVP VC++ FAQ: http://www.mvps.org/vcfaq

Giovanni Bajo - 25 Sep 2004 11:27 GMT
>> Is there any way I can get the behaviour I need?
>
> use unsigned short for the structure member type.

Thanks! Silly me for not thinking of this. I'll try ASAP.

Then, I have another question:

-----------------------------------------------
enum State {  ZERO, ONE, TWO };

struct A {
   State s : 2;
};

void foo(A a) {
 a.s = TWO;
 if (a.s != TWO)
   abort();
}
-----------------------------------------------

This aborts. The reason has surely to do with the underlying integer type of
the bitfield being signed, so that basically 's' represents "-2" after
assignment, and fails the comparison with two. Do you have a workaround for a
situation like this? GCC works as expected.
---
Giovanni Bajo
David Lowndes - 27 Sep 2004 11:07 GMT
>-----------------------------------------------
>enum State {  ZERO, ONE, TWO };
[quoted text clipped - 14 lines]
>assignment, and fails the comparison with two. Do you have a workaround for a
>situation like this?

Other than not using the enum as the bitfield type or otherwise
fiddling with the value, I'm afraid not.

>GCC works as expected.

I can imagine reasons for both possibilities, but I'm not familiar
enough with the C++ standards to know what it really should be.

Carl, can you comment on this?

Dave
Signature

MVP VC++ FAQ: http://www.mvps.org/vcfaq

Carl Daniel [VC++ MVP] - 27 Sep 2004 14:54 GMT
>> GCC works as expected.
>
> I can imagine reasons for both possibilities, but I'm not familiar
> enough with the C++ standards to know what it really should be.
>
> Carl, can you comment on this?

It's a bug (thatnks for reminding me).  9.6/4 says very plainly:

"If the value of an enumerator is stored into a bitfield of the same
enumeration type and the number of bits in the bitfield is large enough to
hold all the values of that enumeration type, the original enumerator value
and the value of the bitfield shall compare equal."

I don't have a VS 2005 installation handy to test this - if it's still
broken there, go ahead and open a bug report for it.

-cd
David Lowndes - 27 Sep 2004 21:58 GMT
>I don't have a VS 2005 installation handy to test this - if it's still
>broken there, go ahead and open a bug report for it.

It's still wrong in the B1 compiler, so I've created the following
report:

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=f4f95
27b-0c4b-45fc-a4dd-39bd490673f1


Dave
Signature

MVP VC++ FAQ: http://www.mvps.org/vcfaq

Giovanni Bajo - 27 Sep 2004 23:31 GMT
>> I don't have a VS 2005 installation handy to test this - if it's still
>> broken there, go ahead and open a bug report for it.
>
> It's still wrong in the B1 compiler, so I've created the following
> report:

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=f4f95
27b-0c4b-45fc-a4dd-39bd490673f1


Cute, I did not know MS now had a public database for bugs. This is a very
good move.

Thanks for filing this.
Signature

Giovanni Bajo

David Lowndes - 28 Sep 2004 07:26 GMT
>http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=f4f95
27b-0c4b-45fc-a4dd-39bd490673f1

>
>Cute, I did not know MS now had a public database for bugs. This is a very
>good move.

If you've not already done so, you might want to add a "vote" to the
bug report to help it on its way.

Dave
Signature

MVP VC++ FAQ: http://www.mvps.org/vcfaq

Carl Daniel [VC++ MVP] - 24 Sep 2004 21:16 GMT
> Is there any way I can get the behaviour I need? It looks like I'm
> forced to use short* and manually extract the bits. FYI, this works
> with GCC as expected.

I don't think so, not unless you change the element types to unsigned short
as David suggests.

This is all implementation defined according to the standard, so VC's
behavior is completely conforming, if not what you hoped for.

-cd
Giovanni Bajo - 25 Sep 2004 11:31 GMT
>> Is there any way I can get the behaviour I need? It looks like I'm
>> forced to use short* and manually extract the bits. FYI, this works
[quoted text clipped - 5 lines]
> This is all implementation defined according to the standard, so VC's
> behavior is completely conforming, if not what you hoped for.

Yes, I was not questioning VC standard conforming, rather its implementation
defined choices. Besides standard conformance, there are QoI matters: the way
it behaves in things the standard leaves as implementation defined is still
very important.

In this case, there would be a problem if there was no way to do what I
described. Using David's solution fits my needs (having a struct which matches
the layout of memory data which is beyond my control and which I need to
access), so everything is fine.
Signature

Giovanni Bajo


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.