简体   繁体   中英

C++: 32 bit to 64 bit migration

I am working on a project of migrating 32 bit C++ code to work on both 32 and 64-bit mode on Mac OS X . I am confused in certain things. For instance, consider the following lines of code-

long* buffer = new long[VAL];
long var = buffer[0];

Now, I know that sizeof(long) would return 4 in 32-bit and 8 in 64-bit. So, I am confused as to what should be the replacement for the above two lines in general so that these lines work correctly both in 32-bit as well as 64-bit mode.

This may sound like a trivial question but I am just no able to make any progress in such kind issues which are plentiful in the project I am working on.

I am confused as to what should be the replacement for the above two lines in general so that these lines work correctly both in 32-bit as well as 64-bit mode.

The answer depends on what you mean by "working correctly".

If you are looking to use integers with specific size (ie 32 bits on all platforms or 64 bits on all platforms) use definitions from <cstdint> : int32_t for 32 bits, int64_t for 64 bits. This will ensure the exact sizing regardless of the platform.

If all you need is interoperability within your own program, you can leave everything as is: in the absence of overflows on the platform with the shortest long , the program is going to do the same thing on the platform with longer long , except that it would take more memory than is necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM