Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam

>
>
C++Builder, 64-bit software build and V…

C++Builder, 64-bit software build and Viva64 renaissance

Feb 01 2013
Author:

After a long wait C++Builder XE3 Update 1 has finally acquired the capability of building 64-bit applications. It means that developers using this tool will soon face the yet unfamiliar world of 64-bit errors.

C++ Builder support in PVS-Studio had been dropped after version 5.20. If you have any questions, feel free to contact our support.

What is a 64-bit error?

This is just a common error in a program. But it has one peculiar feature: it reveals itself only in 64-bit programs [1]. At the same time, you won't find this error when building the 32-bit version of the program. Here is a very simple example to start with:

float *buf = (float *)malloc(1000 * sizeof(float));
unsigned X = (unsigned)buf;
...
float *p = (float *)X;

The pointer is being stored for some time in a variable of the unsigned int type. The pointer will become 64-bit in the 64-bit program, while the unsigned type will still be 32-bit. As a result, high-order bits will be lost, which will cause unpredictable results of program execution. The error is even more insidious due to the fact that it will rarely occur in the 64-bit program. The problem will be revealed only when memory is allocated outside the first 4 Gbytes of the address space.

The 64-bit error shown above is simple and not of much interest. Any compiler will warn you about a dangerous type conversion like that. Let's study a more interesting issue.

float *Array = (float *)malloc(1000 * sizeof(float));
float *p = Array + 500;
unsigned A = 3;
int B = -5;
p[A + B] = 1.0f;

The code will work in the 32-bit program but cause a crash in the 64-bit one. The reason is that the "A + B" expression has the unsigned int type. It will equal 0xFFFFFFFEu. It is incorrect in any case of course; but in a 32-bit system an overflow will occur when adding the pointer to the number and, as a result, the 32-bit program will continue working despite the error.

This defect is harder to find than the previous one. Most compilers think that the code contains nothing suspicious. They won't notice anything odd in the following code either:

size_t Count = BigValue;
for (unsigned Index = 0; Index != Count; Index++)
{ ... }

If the value of the Count variable is higher than UINT_MAX in the 64-bit program, an infinite loop will occur.

All this is just a tip of the iceberg. There are 64-bit errors caused by using magic numbers in code (4, 32, 0xFFFFFFFF), errors of virtual function declaration, and so on. You can study various patterns of 64-bit errors in detail in the following article "A Collection of Examples of 64-bit Errors".

Subtleties and nuances of 64-bit code optimization

Besides errors you may face another unexpected and unpleasant thing: lower performance of the 64-bit version of your program compared to the 32-bit one. In fact, the 64-bit program should work faster [2]. But it often doesn't happen in practice.

The slow-down may be caused by several reasons:

  • Inappropriate types are used as array indexes. It causes a lot of senseless type conversions.
  • Inappropriate types are used to store data. Because of the increase of array sizes the amount of memory being consumed may grow twice. As a result, there will be more cache misses and the time of data passing through the bus will grow.
  • Increase of structure sizes because of alignment changes. It also causes increase of memory amount being used.

What's to be done?

Now you have learned that preparing the 64-bit version of your program consists not only of code recompilation. You may face strange errors and performance losses.

It's outside the scope of this article to describe methods of avoiding 64-bit errors. The point is that the nuances are too numerous and can't be described in one article. That's why I have to refer you to another source.

Several years ago we wrote a large guide on 64-bit software development. It describes in detail all the patterns of 64-bit errors, ways of optimizing a 64-bit program, and points to consider when creating tests for such programs.

Lessons on development of 64-bit C/C++ applications

Many developers using Visual C++ are familiar with these materials. Now, I believe, they will become useful to those who use the C++Builder compiler too. The section contains information of general kind and will be useful regardless of tools you use.

Viva64

Information given in the guide is enough to help you write correct 64-bit programs. But it won't be of much aid if you need to port a large software project to a 64-bit system. It's impossible to review the whole code and find all the dangerous fragments.

The PVS-Studio static analyzer has a set of diagnostic rules called Viva64 to solve this issue. This is the most complete set for detecting 64-bit errors among those existing nowadays.

Now that PVS-Studio has become available in the C++Builder environment, programmers will spend less time and effort on porting their codes. Yes, it's hard to check all the diagnostic messages, but it's, first, better than spending a lot of time catching strange and badly reproducible errors [3]; and, second, the PVS-Studio analyzer has a highly developed system of suppressing false positives, and we can always give you tips on how to customize it.

Here you can download a full-function demo version of PVS-Studio. By the time of writing this article the analyzer can integrate into Embarcadero RAD Studio XE2 and XE3 (including Update 1). We will perhaps support other versions in future too.

Preparation for code porting

Taking an old project and trying to build its 64-bit version is actually a thankless task. That's why many developers using C++Builder would like to do it gradually. You should prepare your code at first and only then start trying to compile its 64-bit version. The PVS-Studio analyzer will help you eliminate most of 64-bit errors beforehand: it can check 32-bit projects as well as 64-bit ones.

But keep in mind that a truly complete analysis is possible only with a 64-bit project. There are many things that make detection of 64-bit defects in 32-bit projects difficult. Preprocessor directives are the most obvious example. If some code fragment is not compiled in the 32-bit program, it won't therefore be checked and no errors will be found in it.

Conclusion

Besides the links given in the article I would like to recommend you to visit some other sections of our website:

  • Reviews of articles on 64-bit software development.
  • Knowledge base (it mostly concerns 64-bit software development).
  • Twitter with news on the subjects of the C++ language, 64-bit software development, and so on.

I wish you bugless 64-bit software.

References

Popular related articles


Comments (0)

Next comments next comments
close comment form