Data types and C/C++

BlitzMax Forums/BlitzMax Programming/Data types and C/C++

Scaremonger(Posted 2009) [#1]
We all know the BlitzMax datatypes, but when interfacing with C / C++ or an API we need to convert them.

I've been converting things for a while now, but it's come to my attention today that I may have been doing it wrong.

I was looking through some posts and found DWORD (32 bit) being translated to Int (Which is fine) but others translating bool to Int and int to Int which I'm sure are incorrect.

C/C++ only defines void, int, char, float, double and bool of which most can be modified with the signed, unsigned, short and long keywords. The Windows API defines a whole lot more, but are based on these 6, and are not relevant here.

Is the following list a good example of converting data types, or am I missing something...

I am aware that putting signed values in unsigned variable (and visa-versa) will muck up it's value, but I usally deal with that seperately in my code..

Blitzmax Types
Byte	8 bit (Unsigned)	0 to 255
Short	16 bit (Unsigned)	0 to 65535
Int	32 bit (Signed)		-2^31 to +2^31-1
Long	64 bit (Signed)		-2^63 to +2^63-1
Float	32 bit floating point number
Double	64 bit floating point number

C Base Types
void	No data type
int	32 bit (Signed)   -2^31 to +2^31-1	= Blitzmax Int
char	8 bit (Signed)    -128 to +128		= Blitzmax Byte
Bool	32 bit (Signed)  TRUE or FALSE		= Blitzmax Int
float	32 bit single precision			= Blitzmax Float
double	80 bit double precision			= Blitzmax Double

C Extended Types
signed short	16 bit	-32768 to +32768  = Blitzmax Short
unsigned short	16 bit	0 to 65535 	  = Blitzmax Short
signed int	32 bit	-2^31 to +2^31-1  = Blitzmax Int
unsigned int	32 bit	0 to +2^32-1  	  = Blitzmax Int
signed long	32 bit	-2^31 to +2^31-1  = Blitzmax Int
unsigned long	32 bit	0 to +2^32-1      = Blitzmax Int
signed char	8 bit	-128 to +127 	  = Blitzmax Byte
unsigned char	8 bit	0 to 255 	  = Blitzmax Byte


[EDIT] 29/12/2009 - Above information updated as per discussions that follow...


grable(Posted 2009) [#2]
I think you messed up some of the C to BlitzMax types...

shorts are usually 16-bits on most platforms and ints and longs are usually 32-bits.
(this is true for x86 at least, but probably not for some embeded platforms)

This is a more correct mapping:
signed char = Byte
unsigned char = Byte
signed short = Short
unsigned short = Short
signed int = Int
unsigned int = Int
signed long = Int
unsigned long = Int
signed long long = Long
unsigned long long = Long



jpavel(Posted 2009) [#3]
In C, there is no built-in Bool type. It's usually just a typedef to int. See

http://en.wikipedia.org/wiki/Boolean_data_type#C

In C99 and C++, there is a built-in boolean, but its size is implementation defined, so the corresponding Blitzmax type may change depending on what compiler and platform you're on.

~Jesse


Gabriel(Posted 2009) [#4]
I tend to use Ints for Bools, but I also do a bitwise And operation with a value of 1, to eliminate all the other bits. C and C++ do not require you to zero your data, and some external libraries will be lazy and not worry about this. So you can get back very large or very small (including negative) values in data you believe will only be one or zero.


Otus(Posted 2009) [#5]
As garble grable wrote, with the exception of Byte and Long, the data types convert directly (on x86 and PPC, where Blitz runs). You should stick to the basic value types where possible: the headers limits.h, values.h, float.h and/or stdint.h (whichever exist) should contain the limits of the data types available on your compiler.

I am aware that putting signed values in unsigned variable (and visa-versa) will muck up it's value, but I usally deal with that seperately in my code..

You don't really have to care about signedness unless you do comparisons. Arithmetic works the same on a singed int as an unsigned int, since the numbers overflow. (This is true at least on x86.)


Brucey(Posted 2009) [#6]
garble

heh :-)

One thing to note, which is quite important. If you want to pass "bool" between Blitz and a library, you *really* want to define your glue function parameter as int. I cannot stress that enough :-p

I have wrapped one or two libraries in the past, so it may be safe to assume that in this case, I know what I'm talking about. (In general, in any other case, you can probably take what I say with a very large grain of salt :-p )


Scaremonger(Posted 2009) [#7]
Cheers everyone; I got my types all muddled up there. I seem to have got myself confused reading some C++ stuff for a 64 bit architecture. The int is architecture dependant which confused the hell out of me..!

on most platforms and ints and longs are usually 32-bits.
Why are int and long both the same? Shouldn't long be 64 bit?

Merry XMAS to you all...


grable(Posted 2009) [#8]
Why are int and long both the same? Shouldn't long be 64 bit?

Its a relic from the days of old, they are more like storage classifiers.

int is usually the size required by the cpu, so short and long act as size modifiers.

Like short int or long int. Adding the int in todays compilers isnt strictly necessary though.

But with the advent of 64-bit, we are in the same mess as before ;)
(with all the different sizes of int)


Retimer(Posted 2009) [#9]
I have wrapped one or two libraries in the past


Yeah right, name one. =p