08-09-2010 06:31 AM
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
08-09-2010 06:38 AM
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
08-09-2010 06:40 AM
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
08-09-2010 06:41 AM
I must get hold of the type of keyboard that Wolfgang has - it obviously lets you type a lot quicker!
08-09-2010 06:49 AM
upps, sorry, JR. For your comfort, the fastest answer isn't necessarily the best one
Next time I'll wait an extra minute - I am not in a race...
08-09-2010 07:43 AM
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
08-09-2010 03:23 PM
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
08-09-2010 07:56 PM
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
08-09-2010 08:34 PM
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
08-10-2010 11:23 AM
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