When to convert a 32-bit to 64-bit code in iOS

Asked

Viewed 343 times

1

I know there is a lot of information about converting a 32-bit to 64-bit code on iOS. However, I still have many doubts and I believe that they are the same of other people, especially in the issue of variavies of the type int. For example, how could I verify, int is used for 32-bit and long to 64-bit. In the meantime, I am developing an app that has only variables of the type int, I put the architecture to Standard architectures (including 64-bit) and run in the simulator. The app runs correctly without any Warning or error. Even executed (apparently) correctly, I have to change the variables to the type long?

2 answers

2


No. Maybe I might need to change if any int is used as a pointer. Essentially what actually changes when it goes up to 64 bits is the size of the pointer that in general you don’t have to worry about. Concern is needed only when trying to exchange numerical types with pointers.

  • Ok. Since I didn’t use these variables as pointer, I will keep the code as is. Thank you.

2

The big difference between 32-bit and 64-bit architectures is that the address space is much larger on 64-bit architectures.

Therefore, the main difference in data types between these architectures is in types that store memory addresses, i.e., pointers.

For example, on a 32-bit architecture, sizeof(void*) returns 4 bytes, while in a 64-bit architecture the same command returns 8 bytes. This difference is necessary for the entire address space to be effectively addressed.

There are other issues like setjmp and longjmp, but these are more closely associated with the instructions (ISA).

Anyway, you must change from int for long if you will use values that the int do not behave, otherwise there is nothing to worry about in this direction.

The int's of life are only problematic if you realize Casts between pointers and integers. Some libraries use this instead of using Opaque pointers. In these cases, the library is expected to take the necessary care to maintain consistency when the library is compiled in 64 bits, and the programmer must make use of the library’s data types, making these changes transparent.

Finally, care should be taken when fixing the size of structures containing pointers. For example:

typedef struct {
    int *a;
} myStruct;

If we execute the command sizeof(myStruct) in both architectures, we will get different sizes for the same structure.

Imagine the code:

myStruct p;
fread(&p, 4, 1, file);

I’m using an absurd example but it will serve to explain the point

On a 64-bit platform, it would not give the expected result, since the structure myStruct has size 8 bytes, but as the structure size has been fixed in the command fread, we will ignore 4 bytes of the file, which may cause absurd results or the crash of the application.

To avoid this is very simple: just use sizeof:

myStruct p;
fread(&p, sizeof(p), 1, file);
  • 1

    Thank you. Very enlightening reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.