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

This is the forum to discuss the Journal's content, article suggestions, etc.

Moderator: 2ffat

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

Postby Andrey2013 » Wed Feb 13, 2013 2:04 am

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.

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:

Code: Select all
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.

Code: Select all
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:

Code: Select all
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 2009, 2010, XE, XE2 and XE3.

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

  1. Terminology. 64-bit error.
  2. Knowledge Base. Why do 64-bit applications work faster than 32-bit ones?
  3. Andrey Karpov. A 64-bit horse that can count.
Andrey2013
 
Posts: 4
Joined: Tue Feb 05, 2013 2:42 am

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

Postby 2ffat » Wed Feb 13, 2013 6:44 am

Why not contact Damon about putting this in the Journal?
James P. Cottingham

Look at me still talking
when there is science to do.
User avatar
2ffat
Forum Mod
Forum Mod
 
Posts: 426
Joined: Wed Jun 23, 2004 7:07 am
Location: South Hill, VA

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

Postby Andrey2013 » Wed Feb 13, 2013 7:07 am

2ffat wrote:Why not contact Damon about putting this in the Journal?

As practice shows, often letters remained unanswered. I do not know about this magazine, but in other situations, such. I decided to write in the articles section on the forum.
Andrey2013
 
Posts: 4
Joined: Tue Feb 05, 2013 2:42 am


Return to Articles

Who is online

Users browsing this forum: No registered users and 1 guest