Why is the size of my enum larger than yours?

I’ve been doing some coding with GSK, system SSL, and had problems getting the data to match.

There are some definitions and my code

typedef enum {
gskdb_rectype_unknown = 0,
gskdb_rectype_keyPair = 1, /* Request key pair */
gskdb_rectype_certificate = 2, /* Certificate */
gskdb_rectype_certKey = 3, /* Certificate with key */
gskdb_rectype_maximum = 32767
} gskdb_record_type;
typedef struct _gskdb_record {
gskdb_record_type recordType;
....
} gskdb_record;
...
gskdb_record ccp;
printf("Length %i\n",sizeof(ccp .recordType));

It prints out a length of 4.

If I display the data in the control block, the value of ccp.recordType is 0x0003C000.

This had me scratching my head. It needed a trip to the shops, and lunch before I spotted the problem

In the options section of the compiler list it had

 *ENUMSIZE(INT)

This says treat all enums as integer. The default is enums(SMALL).

The smallest size of gskdb_record_type is 2 bytes, but I had specified use 4 bytes. The true value of the field is 0x003 (Certificate with key) rather than an undefined 0003c000!

I removed the -qenum=int from my compiler switches.

When I recompiled, and reran the program – it all worked, giving the result 3 -> certificate with key!

Leave a comment