Interop C # using "long" from C ++

On my system: sizeof (long) in C ++ is 4 aka 32bits sizeof (long) in C # is 8 aka 64bits

So, in my Interop method declarations, I am replacing C ++ longs with C # int, but I feel it is not safe?

Why is long the same size as int in C ++? And a long long 64 bit? What's next long long long?

+2


a source to share


3 answers


It is best to assume that you have the latest C library for include <stdint.h>

, and use uint64_t

, int64_t

, uint32_t

, int32_t

. This will work regardless of the underlying platform model.

But in case you're wondering, the main problem is that different systems use different models. On 32 bit systems, both Posix and Windows use ILP32, which means that integer long and dotted are 32 bits long.

For 64 bit, Posix and Windows use different models.



Posix usually uses LP64, which stands for 32-bit integers, 64-bit and 64-bit pointers.

Windows uses LLP64, which stands for 32-bit integers and long, 64-bit long and 64-bit pointers.

+7


a source


Yes, it is safe as long as they are not platform-specific integers (e.g. 32-bit for x86 and 64-bit for x64 platforms), in which case you should use IntPtr

.



Don't forget that at the time C (++) was defined, the default computation size was not 32 bits. Thus, 32 bits were defined as "long". However, new programming languages ​​have adopted new default sizes.

0


a source


On most platforms, C ++ is the long

same as int

and is 4 bytes. It was the remainder of 16 bit days when it int

was 2 bytes and long is a double word - that is, 4 bytes.

So, if your C ++ platform relies on 32 bit longs, converting them to C # is int

safe.

-1


a source







All Articles