LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Reporting error when using sizeof to get an integer length

Hello,

 

Using to program a 64-bit application, there is a code phase:

 

"

int a;

 

printf ("%d", sizeof(a));

 

...

"

when run the application, it give such a error message: "Parameter type is mismatch; expecting int but found long long". I am confused maybe the sizeof cannot return a length of  the data type?

 

David

0 Kudos
Message 1 of 14
(4,450 Views)

Hi David,

 

there is a chapter called Porting 32-bit Code to 64-bit Code in the CVI help; the port to 64 bit involves new specifiers for the printf command 

0 Kudos
Message 2 of 14
(4,447 Views)

I don't have a 64 bit system to try this on, but I'd guess that sizeof() is now returning a 64 bit quantity, and that the printf() function parameter %d is expecting a 32 bit one. Perhaps there is an alternative to %d for 64 bit values? (eg %I64d works for older CVI versions.)

 

JR

0 Kudos
Message 3 of 14
(4,446 Views)

I must get hold of the type of keyboard that Wolfgang has - it obviously lets you type a lot quicker!

0 Kudos
Message 4 of 14
(4,445 Views)

upps, sorry, JR. For your comfort, the fastest answer isn't necessarily the best one Smiley Wink Next time I'll wait an extra minute - I am not in a race...

0 Kudos
Message 5 of 14
(4,443 Views)

Hi Wolfgang,

 

yes, your are right, indeed, the cvi 64-bit "printf()" function can not accept the "%d " format string, the format string should use 'z' format modifier, that is "%zd", and the type sign should also using size_t instead.

 

 

David

0 Kudos
Message 6 of 14
(4,435 Views)

If you ever compile a CVI module with, say, one of the newer VCPP compilers, you'll see warnings for type mismatch if you're comparing the return value of any of the string functions in the standard C library to an int, since these are of type size_t.  The CVI native compiler doesn't warn on this, though in 64 bit mode it looks like maybe it does.

 

In fact, there a lots of things the CVI compiler doesn't warn on, even in "strict" warning mode (or whatever it's called) that's supposed to catch the most warnings.  Signed/unsigned mismatch, assigments in conditional expressions, int -> char coercion, etc.

 

Menchar

 

 

0 Kudos
Message 7 of 14
(4,400 Views)

Hi menchar,

 

As to your meaning, if program a 64-bit application with cvi, there will run the risk due to the  compiler's flaw?

 

David

0 Kudos
Message 8 of 14
(4,395 Views)

David -

 

Well, strictly speaking, it's a lack of a compiler warning, not a flaw in the compiler.

 

If I coerce a 32 bit int to a char (8 bit int) then that's likely a problem if the value doesn't fit in the smaller type.  The CVI native compiler assumes that you know what you're doing and doesn't warn you about this, even if you set the compiler options to the most agressive/extensive warning / error levels.

 

To tell the VCPP or Intel compiler (which will warn on this) you know what you're doing you can cast the larger size to the smaller size, so the compiler knows you really mean to do it and that it's not a mistake.

 

In the case of size_t in 32 bit environment,  I believe that turns out to be an unsigned int, and conversion or comparison to a signed int is, strictly speaking, a mistake.  If you somehow know the size_t value will never actually be greater than the largest representable positive number in the singed int type, then it's not a problem.

 

All of the above is for 32 bit.  It looks to me like the 64 bit mode of the native compiler does manage to warn you about coercing a 64 bit size_t value to a smaller int type.

 

Menchar

 

 

 

 

0 Kudos
Message 9 of 14
(4,387 Views)

Menchar,

 

You should get a similar warning with the CVI compiler when compiling for 32-bit. For example, if you assign an __int64 (8 bytes) to a size_t (4 bytes), you get the following warning:

 

Warning: Conversion from '__int64' to 'size_t' might lose data.

 

Luis

0 Kudos
Message 10 of 14
(4,352 Views)