SlideShare a Scribd company logo
1 of 3
Download to read offline
C++Builder,	64-bit	software	build	and	
Viva64	renaissance
Author: Andrey Karpov
Date: 01.02.2013
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:
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
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 .

More Related Content

Recently uploaded

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

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

  • 1. C++Builder, 64-bit software build and Viva64 renaissance Author: Andrey Karpov Date: 01.02.2013 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: 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.
  • 2. 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.
  • 3. 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 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 .