Accessing a C++ Object in C Code: The Memory Layout of Derived Objects

Accessing a C++ Object in C Code: The Memory Layout of Derived Objects

The Standard does not specify the memory layout of base class subobjects in a derived class. In practice, however, all C++ compilers use the same convention: The base class subobject appears first (in left-to-right order in case of multiple inheritance), and data members of the derived class follow. C code can access derived objects, as long as the derived class abides by the same restrictions that were specified in the Tip “Accessing a C++ Object in C Code.” For example, consider a non-polymorphic class that inherits from Date (the declaration of Date is repeated here for convenience) and has additional data members:

 class Date{public:  int day;  int month;  int year;  Date(); //current date  ~Date();  bool isLeap() const;  bool operator == (const Date& other);};class DateTime: public Date{public:    long time; //additional members  char AM // AM or PM?  DateTime();  ~DateTime();  long getTime() const;};

The two additional data members of DateTime, namely ‘time’ and ‘AM’, are appended after the three members of the base class Date, so the memory layout of a DateTime object is equivalent to this C struct:

 struct POD_DateTime{  int day;  int month;  int year;  long time  char AM;};

As with class Date, the non-polymorphic member functions of DateTime have no effect on the size or memory layout of the object.

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