VC ++ 6.0 structure defines large traps

zhaozj2021-02-16  46

VC 6.0 structure defines a big trap! ! !

I wrote a program that defines a parameter type TypeDef struct tagdef_param {char name [64]; BOOL NEED; BOOL NEEDBUF; INT TYPE;} DEF_PARAM, * PRT_DEF_PARAM;

For convenience, the parameter content used is maintained by Excel and writes the parameter content through the VBA macro, and then calls VC.

To this end, I define a structure in VBA.

Type VBDef_Param Name As String * 64 NEED AS BYTE NEEDBUF AS BYTE TYPE AS LONGEND TYPE

From the type length described in the MSDN, the structural length defined in the VC should be 70 = char (1) * 64 BOOL (1) BOOL (1) Int (4), and the VBA is defined in the VBA should also be 70 = 64 1 1 4. However, the actual results are not correct. The results derived from the following code in VC and VB, respectively:

/ * VC * / printf ("len =% d", sizeof (def_param)); // output result is LEN = 72

The result of 'vbdim buf as vbdef_parammsgbox (len (buf))' is 70

The result of VC is more than 2 BYTEs, while VB is safe.!

In order to see the VC in the end, I have tested / * vc * / with the following procedure.

PRT_DEF_PARAM PDP = New DEF_PARAM; PDP-> NEED = 0; PDP-> NEEDBUF = 0; PDP-> Type = -1; for (int i = 0; i <64; i ) PDP-> Name [i] = ' * ';

Cfile sf; sf.open (...); sf.write (PDP, SIZEOF (DEF_PARAM); sf.close ();

/ * End * /

Finally, the result is even more convincible, see (UltraEdit 7.0):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16. ---------------------------------------------------------------------------------------------------------------------------------------------- ------------- 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 00 00 00 CD CD FF FF FF --------------- --------------------------------

VC automatically adds two BYTE [CD CD] after the two BOOL type members, starting with the Best, there is no understanding of this content in MSDN, and later guess whether the structure is defined in the VC. Supple with 4Byte's integer to adapt to the 32bit operating system? So I conducted two tests 1: First change the BOOL type to the short type, defined to type to typef struct tagdef_param {char name [64]; short need; short needbuf; int type;} def_param, * prt_def_param; obtained The result is the correct 72

2: Then then the CHAR type Name [64] array should be Name [10], defined to Typedef struct tagdef_param {char name [10]; short need; short needbuf; int type;} DEF_PARAM, * PRT_DEF_PARAM; result Sure enough, it should be 18byte (144bit), and the actual results are 20byte (160bit)! Conclusion: I don't know if this is a Microsoft's BUG or MSDN, it can check it into a technical regulation, but this is really designed to design the system. Bring trouble, especially when transmitting data between code written by different languages ​​(via file or network) is easy to cause problems, and it is very uncomfortable to find the problem. So remind everyone that the total length must define a 4BYTE (32bit) multiple when the type of definition is defined for switching data is defined in the VC.

转载请注明原文地址:https://www.9cbs.com/read-27126.html

New Post(0)