# telecom • networking • design

## an EventHelix.com blog ## C++ Internals: Mapping from C++ classes and methods to C structs and functions C++ internals can be better understood by mapping them to corresponding C code.

Refer to the article mapping from C++ classes and methods to C structs and functions.

The contents of the article are summarized below.

## This pointer maps to an additional parameter

A C++ method maps to a C method with an additional this pointer. For example, the `UpdateX` method C++ method…

```void A::UpdateX(int newX)
{
if (g_y != 0 && m_x < newX)
{
m_x = newX;
}
}```

…translates to the following C code:

```void A_UpdateX(A *this_ptr, int newX)
{
if (g_y != 0 && this_ptr->m_x < newX)
{
this_ptr->m_x = newX;
}
}```

Complete article: Mapping from C++ classes and methods to C structs and functions.

## A class maps to a struct with associated methods

A C++ class is equivalent to a C structure with a class methods. The following class…

```class A
{
private:
int m_x;
static int g_y;
int m_z;

// Should be invoked when the object ends
void InformEnd();

public:
A(int x);
~A();
void UpdateX(int newX);
static void UpdateY(int newY);
};```

…is represented as the C structure with functions.

• Methods have a corresponding this pointer.
• Static methods are functions that do not get an additional this pointer.
• Static variables map to global variables.
```struct A
{
int m_x;
int m_z;
};
int g_y = 0; /* Represents static variable */
A *A_Constructor(A *this_ptr, int x);
void A_Destructor(A *this_ptr, BOOLEAN dynamic);
void A_UpdateX(A *this_ptr, int newX);
void A_UpdateY(int newY); /* Represents a static method. */```

Complete article: Mapping from C++ classes and methods to C structs and functions.

## Constructor representation in C

In C++, objects may be allocated on the heap or the stack. This has implications in the mapping from a C++ constructor to an initialization function in C.

```A::A(int x)
{
m_x = x;
m_z = 0;
}```

The mapped constructor takes an additional this pointer. A NULL is passed as the this pointer when the object has to be allocated on the heap. A valid stack address is passed when an object is on the stack.

If a NULL pointer is passed to the construction function, it allocates memory before performing the initialization.

```A *A_Constructor(A *this_ptr, int x)
{
/*Check if memory has been allocated for struct A. */
if (this_ptr == NULL)
{
/*Allocate memory of size A. */
this_ptr = (A *) malloc(sizeof(A));
}

/* Once the memory has been allocated for A, initialize members of A. */
if (this_ptr)
{
this_ptr->m_x = x;
this_ptr->m_z = 0;
}
return this_ptr;
}```

Complete article: Mapping from C++ classes and methods to C structs and functions.

## Destructor representation in C

Destructors also deal with deletion of stack and heap objects.

```A::~A()
{
InformEnd();
}```

If the object was allocated on the heap, a boolean is passed to signal to the construction function that the memory was allocated dynamically and needs to be freed.

```void A_Destructor(A *this_ptr, BOOLEAN dynamic)
{
InformEnd(this_ptr);

/* If the memory was dynamically allocated for A, explicitly free it. */
if (dynamic)
{
free(this_ptr);
}
}```

Complete article: Mapping from C++ classes and methods to C structs and functions.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7847099,"math_prob":0.86614656,"size":3069,"snap":"2019-51-2020-05","text_gpt3_token_len":734,"char_repetition_ratio":0.14225122,"word_repetition_ratio":0.10576923,"special_character_ratio":0.26653633,"punctuation_ratio":0.14160839,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9793287,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,6,null,6,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-23T15:27:38Z\",\"WARC-Record-ID\":\"<urn:uuid:d8ac37c1-5ee4-4d53-aa36-bb6e899b78a0>\",\"Content-Length\":\"73710\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:595c8516-0192-4dbd-9ff9-745cbc63c1f4>\",\"WARC-Concurrent-To\":\"<urn:uuid:e7f916cd-6950-4d55-8752-022b380eb731>\",\"WARC-IP-Address\":\"104.28.21.56\",\"WARC-Target-URI\":\"http://blog.eventhelix.com/tag/heap/\",\"WARC-Payload-Digest\":\"sha1:Y4APJXNJKLXZLEAFPDS66UEOLWMXPSH5\",\"WARC-Block-Digest\":\"sha1:UEXGNX6FD3TRJPSES5GD23HFO5ZLVYYG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250610919.33_warc_CC-MAIN-20200123131001-20200123160001-00401.warc.gz\"}"}