Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6657251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:44:41+00:00 2026-05-26T01:44:41+00:00

The pimpl idiom is commonly used in order to allow changing code in dynamically

  • 0

The pimpl idiom is commonly used in order to allow changing code in dynamically linked libraries without breaking ABI compatibility and having to recompile all the code that depends on the library.

Most of the explanations I see mention that adding a new private member variable changes the offsets of public and private members in the class. That makes sense to me. What I don’t understand is how in practice this actually breaks the dependent libraries.

I’ve done a lot of reading on ELF files and how dynamic linking actually works, but I still don’t see how changing the class size in the shared lib would break things.

E.g. Here is a test application (a.out) I wrote that uses code (Interface::some_method) from a test shared library (libInterface.so):

aguthrie@ana:~/pimpl$ objdump -d -j .text a.out 
08048874 <main>:
...
 8048891:   e8 b2 fe ff ff          call   8048748 <_ZN9Interface11some_methodEv@plt>

The call to some_method uses the Procedural Linkage Table (PLT):

aguthrie@ana:~/pimpl$ objdump -d -j .plt a.out 

08048748 <_ZN9Interface11some_methodEv@plt>:
 8048748:   ff 25 1c a0 04 08       jmp    *0x804a01c
 804874e:   68 38 00 00 00          push   $0x38
 8048753:   e9 70 ff ff ff          jmp    80486c8 <_init+0x30>

which subsequently goes to the Global Offset Table (GOT) where address 0x804a01c is contained:

aguthrie@ana:~/pimpl$ readelf -x 24 a.out 

Hex dump of section '.got.plt':
  0x08049ff4 089f0408 00000000 00000000 de860408 ................
  0x0804a004 ee860408 fe860408 0e870408 1e870408 ................
  0x0804a014 2e870408 3e870408 4e870408 5e870408 ....>...N...^...
  0x0804a024 6e870408 7e870408 8e870408 9e870408 n...~...........
  0x0804a034 ae870408                            ....

And then this is where the dynamic linker works its magic and looks through all the symbols contained in the shared libs in LD_LIBRARY_PATH, finds Interface::some_method in libInterface.so and loads its code into the GOT so on subsequent calls to some_method, the code in the GOT is actually the code segment from the shared library.

Or something along those lines.

But given the above, I still don’t understand how the shared lib’s class size or its method offsets come into play here. As far as I can tell, the steps above are agnostic to the class size. It looks like only the symbol name of the method in the library is included in a.out. Any changes in class size should just be resolved at runtime when the linker loads the code into the GOT, no?

What am I missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:44:42+00:00Added an answer on May 26, 2026 at 1:44 am

    The main problem is that, when you allocate a new instance of a class (either on the stack, or via new), the calling code needs to know the size of the object. If you later change the size of the object (by adding a private member), this increases the size needed; however your callers are still using the old size. So you end up not allocating enough space to hold the object, and the object’s constructor then proceeds to corrupt the stack (or heap), because it assumes it has enough space.

    Additionally, if you have any inline member functions, their code (including offsets to member variables) may be inlined into the calling code. If you add private members anywhere other than the end, these offsets will be incorrect, also leading to memory corruption (note: even if you add to the end, the size mismatch is still a problem).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Backgrounder: The PIMPL Idiom (Pointer to IMPLementation) is a technique for implementation hiding in
When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of
I am using the pimpl idiom and want to reference one of the methods
I just noticed a new term pimpl idiom, what's the difference between this idiom
As I understand, the pimpl idiom is exists only because C++ forces you to
Pimpl's are a source of boilerplate in a lot of C++ code. They seem
The pImpl idiom in c++ aims to hide the implementation details (=private members) of
While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass()
I'm playing around with creating a utility class for the pimpl idiom, however I
There have been a few questions on SO about the pimpl idiom , but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.