I'm attempting to unify an ARM project's (specifically, the i.MX27 CPU running Linux 2.6.33.3, being compiled with GCC 4.3.2) approach to its SQLite interactions. As part of that, I've created a structure with a union that gets used to hold values to be bound to prepared statements.
#define SQLITE_DATA_CHARACTER_STRING_MAX 1024
typedef struct
{
int data_type;
union
{
int integer;
double floating_point;
unsigned char character_string[SQLITE_DATA_CHARACTER_STRING_MAX];
};
}sqlite_data;
Originally, this was int
, float
, char
. I wanted to use long long
, double
, and char
. However, that seems to cause a problem. As typed above, the following code produces predictable output:
int data_fields = 15;
int data_fields_index = 0;
sqlite_data data[data_fields];
LogMsg(LOG_INFO, "%s: Assigning", __FUNCTION__);
for(data_fields_index = 0; data_fields_index < data_fields; data_fields_index++)
{
data[data_fields_index].data_type = (100 + data_fields_index);
data[data_fields_index].integer = (1000 + data_fields_index);
LogMsg(LOG_INFO, "%s: data[%d] - %d; type - %d", __FUNCTION__, data_fields_index, data[data_fields_index].integer, data[data_fields_index].data_type);
}
The output of which is this:
Assigning
data[0] - 1000; type - 100
data[1] - 1001; type - 101
data[2] - 1002; type - 102
data[3] - 1003; type - 103
data[4] - 1004; type - 104
data[5] - 1005; type - 105
data[6] - 1006; type - 106
data[7] - 1007; type - 107
data[8] - 1008; type - 108
data[9] - 1009; type - 109
data[10] - 1010; type - 110
data[11] - 1011; type - 111
data[12] - 1012; type - 112
data[13] - 1013; type - 113
data[14] - 1014; type - 114
However, if I make only one change (giving integer
the type long long
) it all falls apart. So, the following change:
typedef struct
{
int data_type;
union
{
long long integer;
double floating_point;
unsigned char character_string[SQLITE_DATA_CHARACTER_STRING_MAX];
};
}sqlite_data;
Produces this ouput:
Assigning
data[0] - 1000; type - 0
data[1] - 1001; type - 0
data[2] - 1002; type - 0
data[3] - 1003; type - 0
data[4] - 1004; type - 0
data[5] - 1005; type - 0
data[6] - 1006; type - 0
data[7] - 1007; type - 0
data[8] - 1008; type - 0
data[9] - 1009; type - 0
data[10] - 1010; type - 0
data[11] - 1011; type - 0
data[12] - 1012; type - 0
data[13] - 1013; type - 0
data[14] - 1014; type - 0
I've tried deunionizing them, using #pragma pack(6)
, and putting that array on the heap, all with identical results: int
works, long long
doesn't.
What's going on here?