LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CVI short data type

Hi,
I found this problem using a "short type" inside a structure: in the
LabWindows/CVI Programmer Reference Manual page 1-5 the type "short" is
declared to have a size of 16 (2 bytes) . I used the function sizeof()
to get the size of a variable short and it returned 2; but when I tried
to use the same function for a member of a structure of type short, it
returned 4! How can this happen? A short is a short...am I wrong?
Example
short var1;

typedef struct
{
short var1;
....
....
} DataType1; DataType1 data1;

sizeof(var1) : returned 2
sizeof(data1.var1) : returned 4 ......mistery of my compiler?

Thanks a lot

Samuele Gazzani
0 Kudos
Message 1 of 4
(3,440 Views)
You might see if the struct is being padded (to get it on a 32 bit
boundary).
This is a function of the processor and the compiler options you are using.
Kevin Kent

samuele gazzani wrote:

> Hi,
> I found this problem using a "short type" inside a structure: in the
> LabWindows/CVI Programmer Reference Manual page 1-5 the type "short" is
> declared to have a size of 16 (2 bytes) . I used the function sizeof()
> to get the size of a variable short and it returned 2; but when I tried
> to use the same function for a member of a structure of type short, it
> returned 4! How can this happen? A short is a short...am I wrong?
> Example
> short var1;
>
> typedef struct
> {
> short var1;
> ....
> ....
> } DataType1; DataType1 data1;
>
> sizeof(var1) : returned 2
>
sizeof(data1.var1) : returned 4 ......mistery of my compiler?
>
> Thanks a lot
>
> Samuele Gazzani
0 Kudos
Message 2 of 4
(3,440 Views)
CVI (and every other compiler) packs structures to boundaries depending
on the processor type and operating system. On windows the default
packing is ( i guess) on 4 byte boundaries. That's why your short
occupies 4 bytes rather than 2. The same will happen if you define a
byte value (char) in a structure, then there are 3 bytes overhead.

CVI offers the possibility to control the packing of data structures by
pre-compiler directives:

#pragma pack(push) // save the default packing (normally 4)
#pragma pack(1) // set new packing to 1 byte

typedef struct // define your "packed" struct
{
short var1;
char var2;
....
....
} DataType1; DataType1 data1;

#pragma pack(pop) // restore the previous packing

Regards,
Torsten

samuele gazzani schri
eb:
>
> Hi,
> I found this problem using a "short type" inside a structure: in the
> LabWindows/CVI Programmer Reference Manual page 1-5 the type "short" is
> declared to have a size of 16 (2 bytes) . I used the function sizeof()
> to get the size of a variable short and it returned 2; but when I tried
> to use the same function for a member of a structure of type short, it
> returned 4! How can this happen? A short is a short...am I wrong?
> Example
> short var1;
>
> typedef struct
> {
> short var1;
> ....
> ....
> } DataType1; DataType1 data1;
>
> sizeof(var1) : returned 2
> sizeof(data1.var1) : returned 4 ......mistery of my compiler?
>
> Thanks a lot
>
> Samuele Gazzani

--
Torsten Levin Tel.: ++49-(0)89-72495-451
Kayser-Threde GmbH Fax: ++49-(0)89-72495-291
Perchtinger Str. 3 mailto:TL@kayser-threde.de
D-81379 München http://www.kayser-threde.de
0 Kudos
Message 3 of 4
(3,440 Views)
Ok,
Thank you so much!i'll follow your suggestion
Regards

Samuele Gazzani

Torsten Levin wrote:

> CVI (and every other compiler) packs structures to boundaries depending
> on the processor type and operating system. On windows the default
> packing is ( i guess) on 4 byte boundaries. That's why your short
> occupies 4 bytes rather than 2. The same will happen if you define a
> byte value (char) in a structure, then there are 3 bytes overhead.
>
> CVI offers the possibility to control the packing of data structures by
> pre-compiler directives:
>
> #pragma pack(push) // save the default packing (normally 4)
> #pragma pack(1) // set new packing to 1 byte
>
> typedef struct // define your "packed" struct
> {
> short var1;
> char
var2;
> ....
> ....
> } DataType1; DataType1 data1;
>
> #pragma pack(pop) // restore the previous packing
>
> Regards,
> Torsten
>
> samuele gazzani schrieb:
> >
> > Hi,
> > I found this problem using a "short type" inside a structure: in the
> > LabWindows/CVI Programmer Reference Manual page 1-5 the type "short" is
> > declared to have a size of 16 (2 bytes) . I used the function sizeof()
> > to get the size of a variable short and it returned 2; but when I tried
> > to use the same function for a member of a structure of type short, it
> > returned 4! How can this happen? A short is a short...am I wrong?
> > Example
> > short var1;
> >
> > typedef struct
> > {
> > short var1;
> > ....
> > ....
> > } DataType1; DataType1 data1;
> >
> > sizeof(var1) : returned 2
> > sizeof(data1.var1) : returned 4 ......mistery of my compiler?
> >
> > Thanks a lot
> >
> > Samuele Gazzani
>
> --
> Torsten Levin Tel.: ++49-(0)89-72495-451
> Kayser-Threde GmbH
Fax: ++49-(0)89-72495-291
> Perchtinger Str. 3 mailto:TL@kayser-threde.de
> D-81379 München http://www.kayser-threde.de
0 Kudos
Message 4 of 4
(3,440 Views)