SlideShare a Scribd company logo
Lesson 14. Pattern 6. Changing an array's
type
Sometimes it is necessary (or simply convenient) to present array items in the form of items of another
type. The following code example shows dangerous and safe type conversions:

int array[4] = { 1, 2, 3, 4 };

enum ENumbers { ZERO, ONE, TWO, THREE, FOUR };

//safe cast (for MSVC)

ENumbers *enumPtr = (ENumbers *)(array);

cout << enumPtr[1] << " ";

//unsafe cast

size_t *sizetPtr = (size_t *)(array);

cout << sizetPtr[1] << endl;

//Output on 32-bit system: 2 2

//Output on 64-bit system: 2 17179869187

As you may see, the output result of the program differs in the 32-bit and 64-bit versions. On the 32-bit
system, the access to the array items is correct because the sizes of the types size_t and "int" coincide,
so we see the result "2 2".

On the 64-bit system, the output result is "2 17179869187" because it is the value 17179869187 which
is located in the 1-st item of the array sizetPtr (see Figure 1). Sometimes it is this behavior you need but
usually it is considered an error.

Note. The type enum in Visual C++ compiler by default coincides with the type int, i.e. it is a 32-bit type.
You may use enum of another size only with the help of an extension considered non-standard in Visual
C++. So the example above is correct from the viewpoint of Visual C++ compiler but from the viewpoint of
other compilers conversion of a pointer to int items to a pointer to enum items may be also incorrect.
Figure 1 - Arrangement of array items in memory

To get rid of this incorrectness you should refuse to use unsafe type conversions and modify the
program. Another way is to create a new array and copy the values from the original array into it.

You may encounter the described error pattern most often in the code fragments where programmers
try to use pointer values as unique 32-bit identifiers.


Diagnosis
Unsafe changes of an array's type are diagnosed by the tool PVS-Studio. The analyzer warns you about
potentially dangerous type conversions with the diagnostic warning V114. Accordingly, the analyzer
responds only to those constructs that may cause an error on a 64-bit system. For example, the
following code sample is correct and the analyzer will not call the programmer's attention to it:

void **pointersArray;

ptrdiff_t correctID = ((ptrdiff_t *)pointersArray)[index];

The course authors: Andrey Karpov (karpov@viva64.com), Evgeniy Ryzhkov (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: http://www.viva64.com.

Contacts: e-mail: support@viva64.com, Tula, 300027, PO box 1800.

More Related Content

PDF
Lesson 9. Pattern 1. Magic numbers
PPTX
Array & Exception Handling in C# (CSharp)
PDF
Lesson 21. Pattern 13. Data alignment
DOCX
10 ipt python exam breakdown
PDF
Development of a static code analyzer for detecting errors of porting program...
PPTX
Error correction-and-type-of-error-in-c
PDF
Lesson 10. Pattern 2. Functions with variable number of arguments
PDF
Session 5-exersice
Lesson 9. Pattern 1. Magic numbers
Array & Exception Handling in C# (CSharp)
Lesson 21. Pattern 13. Data alignment
10 ipt python exam breakdown
Development of a static code analyzer for detecting errors of porting program...
Error correction-and-type-of-error-in-c
Lesson 10. Pattern 2. Functions with variable number of arguments
Session 5-exersice

What's hot (19)

PDF
C programming part4
PDF
C programming session8
PDF
C programming day#2.
PDF
Flag Waiving
DOCX
Bitstuffing
PDF
The c++coreguidelinesforsavercode
PDF
Variables and data types IN SWIFT
PPTX
Ch8 Arrays
PDF
Undefined behavior is closer than you think
PPT
Ppt lesson 08
PDF
Format string
PPTX
oop lecture 2
PPTX
Python Lecture 2
PPT
Ppt lesson 07
PPTX
FUNDAMENTAL OF C
PDF
Program errors occurring while porting C++ code from 32-bit platforms on 64-b...
PDF
20 issues of porting C++ code on the 64-bit platform
PDF
Serial comm matlab
ODP
(6) collections algorithms
C programming part4
C programming session8
C programming day#2.
Flag Waiving
Bitstuffing
The c++coreguidelinesforsavercode
Variables and data types IN SWIFT
Ch8 Arrays
Undefined behavior is closer than you think
Ppt lesson 08
Format string
oop lecture 2
Python Lecture 2
Ppt lesson 07
FUNDAMENTAL OF C
Program errors occurring while porting C++ code from 32-bit platforms on 64-b...
20 issues of porting C++ code on the 64-bit platform
Serial comm matlab
(6) collections algorithms
Ad

Viewers also liked (20)

PDF
Of complicacy of programming, or won't C# save us?
PDF
Static code analysis and the new language standard C++0x
PPTX
Static analysis of C++ source code
PDF
Lesson 1. What 64-bit systems are
PDF
Development of resource-intensive applications in Visual C++
PDF
Explanations to the article on Copy-Paste
PDF
Safety of 64-bit code
PDF
The reasons why 64-bit programs require more stack memory
PDF
Lesson 26. Optimization of 64-bit programs
PDF
Detection of vulnerabilities in programs with the help of code analyzers
PDF
An eternal question of timing
PDF
Analysis of the Ultimate Toolbox project
PDF
Comparing capabilities of PVS-Studio and Visual Studio 2010 in detecting defe...
PDF
The forgotten problems of 64-bit programs development
PDF
How we test the code analyzer
PDF
Optimization of 64-bit programs
PDF
Introduction into 64 bits for the beginners or where's again the 64-bit world?
PDF
The essence of the VivaCore code analysis library
PDF
Driver Development for Windows 64-bit
PDF
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Of complicacy of programming, or won't C# save us?
Static code analysis and the new language standard C++0x
Static analysis of C++ source code
Lesson 1. What 64-bit systems are
Development of resource-intensive applications in Visual C++
Explanations to the article on Copy-Paste
Safety of 64-bit code
The reasons why 64-bit programs require more stack memory
Lesson 26. Optimization of 64-bit programs
Detection of vulnerabilities in programs with the help of code analyzers
An eternal question of timing
Analysis of the Ultimate Toolbox project
Comparing capabilities of PVS-Studio and Visual Studio 2010 in detecting defe...
The forgotten problems of 64-bit programs development
How we test the code analyzer
Optimization of 64-bit programs
Introduction into 64 bits for the beginners or where's again the 64-bit world?
The essence of the VivaCore code analysis library
Driver Development for Windows 64-bit
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Ad

Similar to Lesson 14. Pattern 6. Changing an array's type (20)

PDF
64-Bit Code in 2015: New in the Diagnostics of Possible Issues
PDF
Lesson 17. Pattern 9. Mixed arithmetic
PDF
Monitoring a program that monitors computer networks
PDF
20 issues of porting C++ code on the 64-bit platform
PDF
About size_t and ptrdiff_t
PDF
Lesson 13. Pattern 5. Address arithmetic
PDF
Lesson 11. Pattern 3. Shift operations
PDF
A Collection of Examples of 64-bit Errors in Real Programs
PDF
Optimization in the world of 64-bit errors
PDF
Rechecking TortoiseSVN with the PVS-Studio Code Analyzer
PDF
Monitoring a program that monitors computer networks
PDF
Comparison of analyzers' diagnostic possibilities at checking 64-bit code
PDF
Lesson 22. Pattern 14. Overloaded functions
PDF
Lesson 24. Phantom errors
PPTX
Patterns of 64-bit errors in games
PDF
A Collection of Examples of 64-bit Errors in Real Programs
PDF
How to avoid bugs using modern C++
PDF
Lesson 6. Errors in 64-bit code
PDF
(4) cpp automatic arrays_pointers_c-strings
PDF
How to make fewer errors at the stage of code writing. Part N1.
64-Bit Code in 2015: New in the Diagnostics of Possible Issues
Lesson 17. Pattern 9. Mixed arithmetic
Monitoring a program that monitors computer networks
20 issues of porting C++ code on the 64-bit platform
About size_t and ptrdiff_t
Lesson 13. Pattern 5. Address arithmetic
Lesson 11. Pattern 3. Shift operations
A Collection of Examples of 64-bit Errors in Real Programs
Optimization in the world of 64-bit errors
Rechecking TortoiseSVN with the PVS-Studio Code Analyzer
Monitoring a program that monitors computer networks
Comparison of analyzers' diagnostic possibilities at checking 64-bit code
Lesson 22. Pattern 14. Overloaded functions
Lesson 24. Phantom errors
Patterns of 64-bit errors in games
A Collection of Examples of 64-bit Errors in Real Programs
How to avoid bugs using modern C++
Lesson 6. Errors in 64-bit code
(4) cpp automatic arrays_pointers_c-strings
How to make fewer errors at the stage of code writing. Part N1.

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Cloud computing and distributed systems.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
cuic standard and advanced reporting.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPT
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Cloud computing and distributed systems.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
cuic standard and advanced reporting.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Teaching material agriculture food technology

Lesson 14. Pattern 6. Changing an array's type

  • 1. Lesson 14. Pattern 6. Changing an array's type Sometimes it is necessary (or simply convenient) to present array items in the form of items of another type. The following code example shows dangerous and safe type conversions: int array[4] = { 1, 2, 3, 4 }; enum ENumbers { ZERO, ONE, TWO, THREE, FOUR }; //safe cast (for MSVC) ENumbers *enumPtr = (ENumbers *)(array); cout << enumPtr[1] << " "; //unsafe cast size_t *sizetPtr = (size_t *)(array); cout << sizetPtr[1] << endl; //Output on 32-bit system: 2 2 //Output on 64-bit system: 2 17179869187 As you may see, the output result of the program differs in the 32-bit and 64-bit versions. On the 32-bit system, the access to the array items is correct because the sizes of the types size_t and "int" coincide, so we see the result "2 2". On the 64-bit system, the output result is "2 17179869187" because it is the value 17179869187 which is located in the 1-st item of the array sizetPtr (see Figure 1). Sometimes it is this behavior you need but usually it is considered an error. Note. The type enum in Visual C++ compiler by default coincides with the type int, i.e. it is a 32-bit type. You may use enum of another size only with the help of an extension considered non-standard in Visual C++. So the example above is correct from the viewpoint of Visual C++ compiler but from the viewpoint of other compilers conversion of a pointer to int items to a pointer to enum items may be also incorrect.
  • 2. Figure 1 - Arrangement of array items in memory To get rid of this incorrectness you should refuse to use unsafe type conversions and modify the program. Another way is to create a new array and copy the values from the original array into it. You may encounter the described error pattern most often in the code fragments where programmers try to use pointer values as unique 32-bit identifiers. Diagnosis Unsafe changes of an array's type are diagnosed by the tool PVS-Studio. The analyzer warns you about potentially dangerous type conversions with the diagnostic warning V114. Accordingly, the analyzer responds only to those constructs that may cause an error on a 64-bit system. For example, the following code sample is correct and the analyzer will not call the programmer's attention to it: void **pointersArray; ptrdiff_t correctID = ((ptrdiff_t *)pointersArray)[index]; The course authors: Andrey Karpov (karpov@viva64.com), Evgeniy Ryzhkov (evg@viva64.com).
  • 3. 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: http://www.viva64.com. Contacts: e-mail: support@viva64.com, Tula, 300027, PO box 1800.