M
M
Michael.2012-01-17 10:49:57
Qt
Michael., 2012-01-17 10:49:57

Qt data types?

When saving to a structure file, for example:

struct TEST_STRUCT <br/>
{<br/>
 char descriptor[16];<br/>
 DWORD val1;<br/>
 BYTE val2;<br/>
};<br/>

I assumed it would be 16 + 4 + 1 = 21 bytes, but sizeof(TEST_STRUCT) gives 24 bytes.
1. Why so?
I need to save the structure to a file, which will then be read by an application built on MFC.
I decided to use the WinAPI functions (CreateFile, WriteFile), but still the byte sequence that I am passing does not match the one expected by the MFC application.
How can you store a structure with the "correct" byte sequence?
Windows XP, MinGW, QtCreator

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BaJlepa, 2012-01-17
@kulinich

#pragma pack (push, 1)
struct TEST_STRUCT
{
char descriptor[16];
DWORD val1;
BYTE val2;
};
#pragma pack (pop)

M
Maxim Pelevin, 2012-01-17
@maxpelevin

The answer to “why?”: Structures and classes in C ++ are aligned in memory to a multiple of the machine word by default, in this case 21 was rounded up to 24.
How to solve the problem has already been answered.

H
hybridcattt, 2012-01-18
@hybridcattt

About "why" - not really. each variable will be rounded to the machine word. that is, you have 16 + 4 + 4.
And if there is BYTE, DOUBLE, BYTE, then the result will not be 1 + 8 + 1 = 10 -> 12, but 4 + 8 + 4 = 16!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question