# OOP inheritance and memory

**URL:** <https://forum.arduino.cc/t/oop-inheritance-and-memory/1397908>\
**Category:** Programming\
**Created:** [July 25, 2025, 9:59am UTC](https://forum.arduino.cc/t/oop-inheritance-and-memory/1397908 "2025-07-25T09:59:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [July 25, 2025, 9:59am UTC](https://forum.arduino.cc/t/oop-inheritance-and-memory/1397908/1 "2025-07-25T09:59:04Z")

</div>

My question concerns memory usage.  
A base class contains a method baseFunc().  
Three further classes are inheriting from the base class.

When a program instantiates the three further classes, what about memory usage:  
Will there be three times baseFunc() in memory?  
Or will there be only one baseFunc() in memory?

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 25, 2025, 10:03am UTC](https://forum.arduino.cc/t/oop-inheritance-and-memory/1397908/2 "2025-07-25T10:03:24Z")

</div>

No specialist but I think (!!) that baseFunc will become a pointer to the actual baseFunc() function.

When you override the function in one of the derived classes, the pointer will be changed to point to the new baseFunc() function.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [July 25, 2025, 10:54am UTC](https://forum.arduino.cc/t/oop-inheritance-and-memory/1397908/3 "2025-07-25T10:54:41Z")

</div>

> [@SupArdu](#):
>
> Will there be three times baseFunc() in memory?

> [@sterretje](#):
>
> I think (!!) that baseFunc will become a pointer to the actual baseFunc() function.
> 
> When you override the function in one of the derived classes, the pointer will be changed to point to the new baseFunc() function.

No and no. Virtual and polymorphic function calls are handled through [vtables](https://en.wikipedia.org/wiki/Virtual_method_table) and a hidden "this" pointer added to the function's parameter list.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 26, 2025, 6:33am UTC](https://forum.arduino.cc/t/oop-inheritance-and-memory/1397908/4 "2025-07-26T06:33:26Z")

</div>

A bit more info

The compiler places member functions like baseFunc() in a single location in the program’s code (text) segment. This means the function’s machine code exists once in the executable.

When you create objects of the derived classes, the compiler allocates memory only for their instance data (member variables), not for the functions.

At runtime, calling baseFunc() involves using the object’s address (this pointer) passed implicitly to the function, but the actual code executed is the single shared copy stored in memory.

Because the function code does not depend on each object’s memory layout, it does not need to be duplicated per instance. This saves memory and keeps the program efficient.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 22, 2026, 6:34am UTC](https://forum.arduino.cc/t/oop-inheritance-and-memory/1397908/5 "2026-01-22T06:34:17Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
