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 7501373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:30:26+00:00 2026-05-29T20:30:26+00:00

I have the following sample code. class bar is derived from the base class

  • 0

I have the following sample code. class bar is derived from the base class foo and allocates memory for ptr_x while read/write access are granted through the base class. This is a toy model for a large code in which read/write functions are the same for all different variants of an object but memory allocation slightly differs in different variants.

#include <iostream>

class foo{
protected:
    int *ptr_x;

public:
    foo(){
        std::cout << " 1) Inside foo constructor: " << ptr_x << std::endl;
    }

    void read() 
    {
        std::cout << " 2) Inside read function: " <<  ptr_x << std::endl;    
        std::cout << " 3) Inside read function: " << *ptr_x << std::endl;
    }
    void operator=(const int rhs)
    {
        std::cout << " 4) Inside operator= : " <<  ptr_x << std::endl;
        *ptr_x = rhs;
    }        
};
class bar: public foo{

public:
    bar(int a)
    : foo()
    {
        std::cout << " 5) Inside bar constructor: " << ptr_x << std::endl;
        ptr_x = new int;
        std::cout << " 6) Inside bar constructor: " << ptr_x << std::endl;
        *ptr_x = a;
    }
    ~bar()
    {
        std::cout << " 7) Inside bar destructor: " << ptr_x << std::endl;
        if (ptr_x != NULL) {delete ptr_x; ptr_x = NULL;}
    }
};
int main (){
    bar a(20);
    a.read();
    a = 40;
    a.read();
    return 0;
}

When I run the code, I get:

 1) Inside foo constructor: 0
 5) Inside bar constructor: 0
 6) Inside bar constructor: 0x1d8f010
 2) Inside read function: 0x1d8f010
 3) Inside read function: 20
 1) Inside foo constructor: 0x7f40c11e3b68
 5) Inside bar constructor: 0x7f40c11e3b68
 6) Inside bar constructor: 0x1d8f030
 7) Inside bar destructor: 0x1d8f030
 2) Inside read function: 0x1d8f030
 3) Inside read function: 0
 7) Inside bar destructor: 0x1d8f030
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0000000001d8f030 ***

I have the following questions: 1) Why does not code enter operator= in the base class? 2) Why is there a second call made to the constructor/destructor ? 3) why is there a double free problem?

what am I doing wrong here?

EDIT: Ok double free problem is obvious, but why is there two instances of same memory locations? This somehow has to do with operator= since when I comment it out everything is fine.

Thanks

  • 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-29T20:30:28+00:00Added an answer on May 29, 2026 at 8:30 pm

    Reasoning for the observed behavior:

    The compiler uses the conversion constructor in derived class,

      bar(int a)
    

    for evaluating:

      a = 40;
    

    It takes in the integer 40 and creates a bar object using the conversion constructor and then assigns the created bar object to a using the implicit copy assignment operator(=) generated by the compiler for class bar.

    This is the reason you see additional calls to bar constructor and that Base class overloaded = is never called. Also, the reason for double free lies in here, You have multiple bar objects which keep pointing to the dynamic memory allocated for ptr_x and when one of the object goes out of scope the destructor is called which deallocates the memory but leaves the other objects member pointer in a dangling state.

    How to solve these problems:
    You should mark the conversion constructor explicit if you don’t want the compiler to use it for implicit conversions like this.

    You should follow the Rule of Three for your class bar.

    Caveat:
    Note that the = operator in the base classes is always hidden by the implicitly generated = operator for the derived class.

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

Sidebar

Related Questions

Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
I have downloaded sample code from Apple Center.I also have gone through following question:
I have the following sample piece of code, public class test implements Runnable{ public
I have the following code class sample { public void update(List l) { l
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following sample code in a VB.NET console application. It compiles and
I have the following sample code which doesn't seem to want to run. import
I have the following code sample that im trying to wrap my head around
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
I am new to Emacs, and I have the following code as a sample.

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.