views:

385

answers:

9

What considerations do I need to make if I want my code to run correctly on both 32bit and 64bit platforms ?

EDIT: What kind of areas do I need to take care in, e.g. printing strings/characters or using structures ?

A: 

I assume you are still talking about compiling them separately for each individual platform? As running them on both is completely doable by just creating a 32bit binary.

Adam Haile
A: 

The biggest one is making sure you don't put pointers into 32-bit storage locations.

But there's no proper 'language-agnostic' answer to this question, really. You couldn't even get a particularly firm answer if you restricted yourself to something like standard 'C' or 'C++' - the size of data storage, pointers, etc, is all terribly implementation dependant.

Will Dean
+1  A: 

One solution would be to target a virtual environment that runs on both platforms (I'm thinking Java, or .Net here).

Or pick an interpreted language.

Do you have other requirements, such as calling existing code or libraries?

samjudson
A: 

The same things you should have been doing all along to ensure you write portable code :)

mozilla guidelines and the C faq are good starting points

sparkes
A: 

It honestly depends on the language, because managed languages like C# and Java or Scripting languages like JavaScript, Python, or PHP are locked in to their current methodology and to get started and to do anything beyond the advanced stuff there is not much to worry about.

But my guess is that you are asking about languages like C++, C, and other lower level languages.

The biggest thing you have to worry about is the size of things, because in the 32-bit world you are limited to the power of 2^32 however in the 64-bit world things get bigger 2^64.

With 64-bit you have a larger space for memory and storage in RAM, and you can compute larger numbers. However if you know you are compiling for both 32 and 64, you need to make sure to limit your expectations of the system to the 32-bit world and limitations of buffers and numbers.

Nick Berardi
+2  A: 

Options:

Code it in some language with a Virtual Machine (such as Java)

Code it in .NET and don't target any specific architecture. The .NET JIT compiler will compile it for you to the right architecture before running it.

Jorge Córdoba
A: 

In C (and maybe C++) always remember to use the sizeof operator when calculating buffer sizes for malloc. This way you will write more portable code anyway, and this will automatically take 64bit datatypes into account.

Dana the Sane
A: 

In most cases the only thing you have to do is just compile your code for both platforms. (And that's assuming that you're using a compiled language; if it's not, then you probably don't need to worry about anything.)

The only thing I can think of that might cause problems is assuming the size of data types, which is something you probably shouldn't be doing anyway. And of course anything written in assembly is going to cause problems.

Evan Shaw
A: 

Keep in mind that many compilers choose the size of integer based on the underlying architecture, given that the "int" should be the fastest number manipulator in the system (according to some theories).

This is why so many programmers use typedefs for their most portable programs - if you want your code to work on everything from 8 bit processors up to 64 bit processors you need to recognize that, in C anyway, int is not rigidly defined.

Pointers are another area to be careful - don't use a long, or long long, or any specific type if you are fiddling with the numeric value of the pointer - use the proper construct, which, unfortunately, varies from compiler to compiler (which is why you have a separate typedef.h file for each compiler you use).

-Adam Davis

Adam Davis