LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Size of enum

I'm a little confused about the size of an enum in CVI. One entry in the Help file says, "The compilers always use 4 bytes to represent the largest enum value." Another entry says, "The size of an enumeration type depends on the value of its enumeration constant." According to sizeof an enum is 4 bytes even when it only has a single constant set to 0.

I would like know if there's some way to enforce that an enum always uses the smallest possible size (e.g., a #pragma or compiler option). I'm writing a CVI program to communicate with an embedded device. There's a communications header file that defines the data in the communciations packet, and the packet includes several enums. The embedded compiler uses 1-byte enums, so its packets don't align with CVI's packets.

I realize that the C standard says that enums are the same size as the platform's native int and that what I want to do is non-standard. I belive that previous versions of CVI did follow the "use the smallest size" rule, but I don't have an old version installed to test that theory (I'm using CVI 7.0.0).
0 Kudos
Message 1 of 4
(3,982 Views)
An enum in CVI is anyways 4 bytes.You cannot change its size by using a pragma in CVI, since I dont think ANSI C89 defined any such pragma. You will probably need to caste it to a char to get the correct alignment. I checked the help for CVI 7.1 and it always mentions 4 bytes as the size of the enum. If you can let me know which topic caused the confusion, I can file a report to make the help more clear.

Hope this helps
Bilal Durrani
NI
0 Kudos
Message 2 of 4
(3,961 Views)
There's a help section titled "C Data Types" that includes a table giving the size of each intrinsic data type. The last entry in the table is enum whose size is given as "8, 16, or 32". Directly beneath the table it says, "The size of an enumeration type depends on the value of its enumeration constant."
0 Kudos
Message 3 of 4
(3,954 Views)
I can clarify the discrepancy in the help. It was inconsistent up until version 7.0. We caught the mistake sometime ago and fixed it for version 7.1. (The two of you are probably using different versions of CVI).

A long time ago, for compatibility with the Watcom compiler, the size of enums did vary in CVI (but only when using the Watcom compatibility mode), and this is why we ended up with this discrepancy in the help. The Watcom compatibility mode hasn't been supported since at least version 5.5.

Luis
NI
0 Kudos
Message 4 of 4
(3,942 Views)