Equivalent functions for Unchecked Conversion

Equivalent functions for Unchecked Conversion


Are there any functions/libraries that can perform the Unchecked_Conversion function in Ada? We are writing a program that will read a bitstream. Upon determining the type, we want to able to sort of “cast” this bitstream to the correct object in C++.


Sure there is a way to do that. However, perhaps I should explain what Unchecked_Conversion in Ada does, for C++ programmers who aren’t fluent speakers of Ada.

Ada is a strongly-typed language. Therefore, it is does not allow the programmer to assign variables of different types to one another. For example, assigning a variable of type Float to a variable of type Integer requires an explicit cast. In C++, on the other hand, the cast isn’t required in this case; it’s performed implicitly.

Of course, real world programming requires some bit fiddling tricks and brute force casts, especially when dealing with low level functions that de-serialize objects from a stream of bits. For this purpose, Ada has the generic procedure Unchecked_Conversion, which takes as an argument one type, and returns another. The only constraint on this is that they must be the same size.

There are two ways to achieve this in C++. The first consists of using memcpy. However, this is kludgy and requires a function call with its associated overhead.

The second way relies on the built-in operator reinterpret_cast. As the name suggests, this operator casts one type to another by interpreting the bit pattern of the argument.

In general, you can convert non-related types by using reinterpret_cast to coerce the source’s pointer to the target’s pointer, and then dereference the resultant pointer. Thus, to convert a stream of bytes into a Person object, you would do the following:

struct Person{ int age;  char name[20];};void cast(unsigned char * stream){  Person per = *reinterpret_cast < Person* > (stream);}

reinterpret_cast takes the argument stream (which is a pointer to char), coerces it to a pointer to Person, the target type (specified in the angle brackets) and then, the resultant pointer is dereferenced (the asterisk before reinterpret_cast). The operation yields a temporary object of type Person that is assigned to a Person object per. Unlike Ada, C++ does not require that the source and the target types have the same size; rather, C++ is true to the “trust the programmer” idiom.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as