﻿# Lesson 5\. Building a 64\-bit application

We would like to warn the readers right away that it is impossible to describe the process of building a 64\-bit application in every detail\. Any project has its own unique settings, so you must be very attentive when adapting them for a 64\-bit system\. The lesson discusses only the common steps important for any project\. These steps will tell you where to begin\.

## Libraries

Before trying to build your 64\-bit application, make sure that all the necessary versions of 64\-bit libraries are installed and paths to them are correct\. For example, 32\-bit and 64\-bit library files with "lib" extension usually differ and are situated in different catalogues\. Fix the bugs if any\.

_Note\. If libraries are presented in the form of the source code, there must be the 64\-bit configuration of the project\. Keep in mind that you are risking to infringe license agreements when modifying a library to build its 64\-bit version by yourself\._

## Assembler

Visual C\+\+ does not support the 64\-bit inline assembler\. You must either use an external 64\-bit assembler \(for example, MASM\) or rewrite the assembler code in C/C\+\+\.

## Examples of compilation errors and warnings

On starting to build the project you will encounter many compilation errors and warnings related to explicit and implicit type conversions\. We would like to show you an example of such an error\. Here is a code:

```cpp
void foo(unsigned char) {}
void foo(unsigned int) {}
void a(const char *str)
{
  foo(strlen(str));
}
```

This code successfully compiles in the 32\-bit mode, but in the 64\-bit mode, Visual C\+\+ compiler will generate the warning:

```cpp
error C2668: 'foo' : ambiguous call to overloaded function
   .\xxxx.cpp(16): could be 'void foo(unsigned int)'
   .\xxxx.cpp(15): or 'void foo(unsigned char)'
   while trying to match the argument list '(size_t)'
```

The function strlen\(\) returns the type [size\_t](https://pvs-studio.com/en/blog/terms/0044/)\. On a 32\-bit system, the type size\_t coincides with the type unsigned int and the compiler chooses the function "void foo\(unsigned int\)" to call\. In the 64\-bit mode, the types size\_t and unsigned int do not coincide\. The type size\_t becomes 64\-bit while the type unsigned int remains 32\-bit\. As a result, the compiler does not know which of the foo\(\) functions to prefer\.

Now consider an example of a warning generated by Visual C\+\+ compiler when building code in the 64\-bit mode:

```cpp
CArray<char, char> v;
int len = v.GetSize();
warning C4244: 'initializing' : conversion from 'INT_PTR' to 'int',
                possible loss of data
```

The function GetSize\(\) returns the type [INT\_PTR](https://pvs-studio.com/en/blog/terms/0024/) that coincides with the type int in a 32\-bit code\. In a 64\-bit code, the type INT\_PTR is 64\-bit and it is implicitly converted to the 32\-bit int type\. The values of more significant bits get lost during this process and the compiler warns you about it\. An implicit type conversion may cause an error if the number of the array items exceeds INT\_MAX\. To eliminate the warning and the possible error you should assign the type INT\_PTR or [ptrdiff\_t](https://pvs-studio.com/en/blog/terms/0041/) to "len" variable\.

**Do not correct warnings** until you have learned the 64\-bit error patterns\. You might accidentally hide an error failing to correct it and make it more difficult to detect further\. You will learn about the patterns of 64\-bit errors and methods of detecting and correcting them in the next lessons\. You may also see the following articles: "[20 issues of porting C\+\+ code on the 64\-bit platform](https://pvs-studio.com/en/blog/posts/cpp/a0004/)", "[A 64\-bit horse that can count](https://pvs-studio.com/en/blog/posts/cpp/a0043/)"\.

## size\_t and ptrdiff\_t types

As most compilation errors and warnings are related to data type incompatibility, we should consider two types \- size\_t and ptrdiff\_t \- which are most relevant to us regarding the process of 64\-bit code creation\. If you are using Visual C\+\+ compiler, these types are integrated into it and you will not need the library files\. But if you are using GCC, you will need the header file "stddef\.h"\.

**size\_t** is a C/C\+\+ base unsigned integer type\. It is the type of the result returned by sizeof operator\. The size of the type is chosen so that it could store the maximum size of a theoretically possible array of any type\. For example, size\_t is 32\-bit on a 32\-bit system and 64\-bit on a 64\-bit one\. In other words, you may safely store a pointer in a variable of size\_t type\. Pointers to class functions are an exception but this is a different topic\. The type size\_t is usually used in loop counters, to index arrays, to store sizes and in address arithmetic\. The following types are analogous to size\_t: SIZE\_T, DWORD\_PTR, WPARAM, ULONG\_PTR\. Although you may store a pointer in size\_t, it is better to use another unsigned integer type uintptr\_t for that \- its name reflects its capability\. The types size\_t and uintptr\_t are synonyms\.

**ptrdiff\_t** is a C/C\+\+ base signed integer type\. Its size is chosen so that it could store the maximum size of a theoretically possible array of any type\. This type will be 32\-bit on a 32\-bit system and 64\-bit on a 64\-bit one\. Like size\_t, a variable of ptrdiff\_t type can safely store a pointer except for a pointer to a class function\. The type ptrdiff\_t is also the result of an expression where one pointer is subtracted from another "ptr1\-ptr2"\. The type ptrdiff\_t is usually used in loop counters, to index arrays, to store sizes and in address arithmetic\. Its analogues are: SSIZE\_T, LPARAM, INT\_PTR, LONG\_PTR\. The type ptrdiff\_t has a synonym intptr\_t whose name reflects it more clearly that it can store a pointer\.

The sizes size\_t and ptrdiff\_t were created to perform correct [address arithmetic](https://pvs-studio.com/en/blog/terms/0005/)\. It has been considered for a long time that the size of int coincides with the size of the machine word \(processor capacity\) and it can be used as indexes and to store sizes of objects and pointers\. So, address arithmetic was also built with int and unsigned types\. The type int is used in most education materials on C and C\+\+ programming in loop bodies and as indexes\. The following example is almost a canon:

```cpp
for (int i = 0; i < n; i++)
  a[i] = 0;
```

As processors were developing and their capacity increasing, it became unreasonable to further increase the capacities of int type\. There are a lot of reasons for that: the purposes of saving memory being used, maximum compatibility, etc\. As a result, several [data models](https://pvs-studio.com/en/blog/terms/0012/) appeared describing the relations of the base C and C\+\+ types\. So it is not so easy now to choose a type for a variable to store a pointer or object size\. size\_t and ptrdiff\_t types appeared to become the smartest solution of this problem\. They can certainly be used in address arithmetic\. Now, the following code must become a canon:

```cpp
for (ptrdiff_t i = 0; i < n; i++)
  a[i] = 0;
```

It is this code that can provide safety, good portability and performance\. You will learn from the next lessons why\.

The types size\_t and ptrdiff\_t we have described may be called [memsize](https://pvs-studio.com/en/blog/terms/0030/)\-types\. The term "memsize" appeared as an attempt to briefly name all the types that can store sizes of pointers or indexes of the largest arrays\. By memsize\-types you should understand all the simple C/C\+\+ data types that are 32\-bit on a 32\-bit architecture and 64\-bit on a 64\-bit one\. Here are examples of memsize\-types: size\_t, ptrdiff\_t, pointers, SIZE\_T, LPARAM\.

_The course authors: Andrey Karpov \([karpov@viva64\.com](mailto:karpov@viva64.com)\), Evgeniy Ryzhkov \([evg@viva64\.com](mailto:evg@viva64.com)\)\._

_The rightholder of the course "Lessons on development of 64\-bit C/C\+\+ applications" is OOO "Program Verification Systems"\. The company develops software in the sphere of source program code analysis\. [The company's site](https://pvs-studio.com/en/)\._