Complications of Using the Virtual Base Class

In the previous article about How Does Virtual Base Class Works Internally? we have seen address resolution of virtual base class & why it’s needed. But I have not discussed Complications of Using the Virtual Base Class. Which we will see in this article. This is going to be a bit complex & clumsy then learning the internal working of the virtual base class. But if you are not tired, then read forward....

September 12, 2019 · 8 min · 1500 words · Vishal Chovatiya

Part 3: All About Virtual Keyword in C++: How Does Virtual Destructor Works?

Finally, we are in the 3rd & last part of this series. We have already discussed how virtual function & virtual class/inheritance works internally in previous parts. We left one topic i.e. “How Does Virtual Destructor Works?” which we will see now. As usual, before learning anything new I usually start with “Why Do We Need It in the First Place?” Why Do We Need a Virtual Destructor? We will understand this with our earlier example(slightly twisted): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 class protocol_t { private: uint8_t *_type; // storage ....

September 12, 2019 · 6 min · 1194 words · Vishal Chovatiya

When to Use const vs constexpr in C++

While introducing myself to Modern C++ & its new features introduced in C++11 & C++14, I have completely neglected this keyword constexpr. Initially, I was confused about when to use const vs constexpr in C++ & how this constexpr works & differ with const. So, I have studied this from different sources & here is the consolidation of it: Primitive constexpr Variables 1 2 3 int varA = 3; const int varB = 5; constexpr int varC = 7; All of the above variable having a value which is known at compile time....

September 12, 2019 · 5 min · 935 words · Vishal Chovatiya

2 Wrong Way to Learn Copy Assignment Operator in C++ With Example

While I was introducing myself to C++, I was confused about the syntax of the copy assignment operator in C++ & some of its use-cases. I have learned those lessons the hard way. And so I have decided to write this article, where we see 2 wrong way to learn copy assignment operator in C++ with example. And we also see why we need it & why its syntax like that only....

September 11, 2019 · 9 min · 1705 words · Vishal Chovatiya

All About Copy Constructor in C++ With Example

Do not carry away with the title “All about copy constructor in C++ with example”. In this article, I am not going to describe what copy constructor in C++ is. There is plenty of material available for that over the internet. Rather we will discuss why, where & how it used, how compiler synthesizes it for you and in what scenarios it called or not synthesized. Why: Copy Constructor The simple answer is to copy the data within the same data types, but if you want more concrete idea then see below image:...

September 11, 2019 · 5 min · 862 words · Vishal Chovatiya

Inside the C++ Object Model

This article is the collection of concept I have acquired while introducing myself to C++ by googling here & there. This material is also not in order. I have just collected the answer to my quick question. And write it down here. But one thing I can assure you is that once you go through this article. You can connect many broken thought of understanding on what runs “Inside the C++ object model”....

September 11, 2019 · 5 min · 928 words · Vishal Chovatiya

Memory Layout of C++ Object in Different Scenarios

In this article, we will see the memory layout of different C++ Object. And how different storage & access specifiers affect this memory footprint. I am not going to discuss compiler augmented code, name mangling & working of any C++ mechanism related to memory as it is compiler & architecture-dependent. To keep this further simple, I have considered the standard stack growth direction i.e. downwards. And with the same, data members are represented inverted in memory(thanks to Thomas Vermeilh for pointing that out), that too I have ignored for simplicity....

September 11, 2019 · 8 min · 1676 words · Vishal Chovatiya

Part 1: All About Virtual Keyword in C++: How Does Virtual Function Works Internally?

“All About Virtual Keyword in C++” is a series of articles(total of three, PART 1, PART 2, PART 3) describe working of the virtual keyword in different scenarios. This article mostly focuses on “How Does Virtual Function Works Internally?”. In other words, How dynamic dispatch done in C++! Although I am not a compiler writer, but this is what I have learned so far from various sources, courses, books & disassembly of C++ program....

September 11, 2019 · 9 min · 1746 words · Vishal Chovatiya

Part 2: All About Virtual Keyword in C++: How Does Virtual Base Class Works Internally?

In PART 1 of “All About Virtual Keyword in C++” series, we have discussed “How Does Virtual Function Works Internally?”. So, in this article, I will discuss “How Does Virtual Base Class Works Internally?”. I am iterating the same thing which I have mentioned in the earlier article as well that implementation of a virtual mechanism is purely compiler dependent. So, there is no C++ standard is defined for such dynamic dispatch....

September 11, 2019 · 6 min · 1249 words · Vishal Chovatiya

How Do malloc & free Work in C!

As we know, the process can allocate & deallocate memory using malloc & free in C language. But do you ever consider what happens behind the scene ? or How do malloc & free work? Let see Allocating Memory on the Heap A process can allocate memory by increasing the size of the heap. Heap is a variable-size segment of contiguous virtual memory that begins just after the uninitialized data segment of a process and grows & shrinks as memory allocated and freed....

October 8, 2016 · 5 min · 876 words · Vishal Chovatiya

CRT: C Run Time Before Starting main()

There are a lot of functions called before & after the main execution. As an application developer you need not worry about this stuff, but yes! if you are a core developer who works in Linux kernel, Binutils, compiler or embedded system-related development, then you must know these things. Here in “CRT: C run time before starting main”, we will see some pointers related to it. What Is crt? crt stands for C runtime....

September 10, 2016 · 3 min · 568 words · Vishal Chovatiya