Convert size_t to long, is there a flaw?

Is there a drawback in converting size_t to long? Because, I am writing a program that supports bind_list in a file. Therefore, I am moving to another node based on size_t, and I also track the total number of lists as size_t. Therefore, it is obvious that there will be some conversion or addition of long and size_t. Is there a drawback to this? If there is, then I will do it all for so long, not size_t, even sizes. Please advise.

+1
c ++ long-integer sizeof type-conversion size-t
source share
3 answers

Now this is not a problem, but it may be in the future, depending on where you will transfer the application. This is because size_t is defined as large enough to hold pointer offsets, so if you have a 64-bit pointer, size_t will also be 64 bits. Now, long ones may or may not be 64 bits, because size rules for basic types in C / C ++ give place to some changes.

But if you want to write these values ​​to a file, you still have to choose a specific size, so there is no other option than converting to long (or long if necessary). Even better, use one of the new types, such as int32_t.

My advice: somewhere in the header of your file, store sizeof for the type in which you converted size_t. By doing this, if in the future you decide to use a larger one, you can still maintain the old size. And for the current version of the program, you can check whether the size is supported or not, and give an error if not.

+1
source share

Is there a drawback in converting size_t to long?

Theoretically, a long one might be less than size_t. In addition, long signed. size_t has no sign. Therefore, if you start using them in a single expression, a compiler like g ++ will complain about it. Lot. Theoretically, this can lead to unforeseen errors due to appointments without subscribers.

obviously there will be some conversion or adding a long

I do not understand why there should be some kind of conversion or addition to duty. You can continue to use size_t for all arithmetic operations. You can print it as "ListIndex" or something else and continue to use it throughout the code. If you mix types (long and size_t), g ++ / mignw will come to your death.

Alternatively, you can choose a specific type that has a guaranteed size. Newer compilers have a cstdint header that includes types such as uint64_t (it is highly unlikely that you will encounter a file larger than 2 ^ 64, for example). If your compiler does not have a header, it should be available in boost.

+2
source share

The "long" type, unfortunately, does not have a good theoretical basis. It was originally introduced on 32-bit Unix ports to distinguish it from the 16-bit "int" accepted by existing PDP11 software. Then a later version of β€œint” was changed to 32 bits on these platforms (and β€œshort” was introduced), and β€œlong” and β€œint” became synonyms that they have been for a very long time.

Now, on 64-bit Unix-like platforms (Linux, BSD, OS X, iOS, and any unregistered unixes that may still bother), "long" is a 64-bit amount. But, unfortunately, not on the windows: in the existing headers there were too many outdated "codes" that made the assumption sizeof (int) == sizeof (long), so they went with an abomination called "LLP64" and left longer than 32 bits. Sigh.

But "size_t" is wrong. This always meant only one thing: it is an unsigned type that stores its own pointer size in the address space. If you have an unsigned (! - use ssize_t or ptrdiff_t, if you need a signed arithmetic) pointer that needs an integer representation (i.e. you need to save the memory size for the object), this is what you are using.

+2
source share

All Articles